Coverage Report - org.mule.agent.Log4jNotificationLoggerAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
Log4jNotificationLoggerAgent
0%
0/47
0%
0/14
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.agent;
 8  
 
 9  
 import java.util.HashMap;
 10  
 import java.util.Map;
 11  
 
 12  
 import org.apache.log4j.Level;
 13  
 import org.apache.log4j.Logger;
 14  
 import org.apache.log4j.PropertyConfigurator;
 15  
 import org.apache.log4j.xml.DOMConfigurator;
 16  
 import org.mule.api.context.notification.ServerNotification;
 17  
 import org.mule.api.lifecycle.InitialisationException;
 18  
 import org.mule.util.MapUtils;
 19  
 import org.mule.util.StringUtils;
 20  
 
 21  
 /**
 22  
  * <code>AbstractNotificationLoggerAgent</code> Receives Mule server notifications
 23  
  * and logs them and can optionally route them to an endpoint
 24  
  */
 25  
 public class Log4jNotificationLoggerAgent extends AbstractNotificationLoggerAgent
 26  
 {
 27  
 
 28  
     protected static final int DEFAULT_DESCRIPTION_BUFFER_SIZE = 64;
 29  
 
 30  
     protected Logger eventLogger;
 31  0
     private String logName = Log4jNotificationLoggerAgent.class.getName();
 32  0
     private String logFile = null;
 33  0
     private String logConfigFile = null;
 34  0
     private String chainsawHost = "localhost";
 35  0
     private int chainsawPort = -1;
 36  0
     private final Map<?, ?> levelMappings = new HashMap<Object, Object>();
 37  
 
 38  
     public Log4jNotificationLoggerAgent()
 39  
     {
 40  0
         super("log4j-notifications");
 41  0
     }
 42  
 
 43  
     /**
 44  
      * Should be a 1 line description of the agent
 45  
      * 
 46  
      * @return the description of this Agent
 47  
      */
 48  
     @Override
 49  
     public String getDescription()
 50  
     {
 51  0
         StringBuffer buf = new StringBuffer(DEFAULT_DESCRIPTION_BUFFER_SIZE);
 52  
 
 53  0
         if (StringUtils.isNotBlank(logName))
 54  
         {
 55  0
             buf.append("Logging notifications to logger: ").append(logName);
 56  
         }
 57  
 
 58  0
         if (StringUtils.isNotBlank(logFile))
 59  
         {
 60  0
             buf.append("Logging notifications to: ").append(logFile);
 61  
         }
 62  
 
 63  0
         if (chainsawPort > -1)
 64  
         {
 65  0
             buf.append(" Chainsaw: ").append(chainsawHost).append(":").append(chainsawPort);
 66  
         }
 67  
 
 68  0
         if (buf.length() == 0)
 69  
         {
 70  0
             buf.append("No logging or event forwarding is configured");
 71  
         }
 72  
 
 73  0
         return getName() + ": " + buf.toString();
 74  
     }
 75  
 
 76  
     public String getLogName()
 77  
     {
 78  0
         return logName;
 79  
     }
 80  
 
 81  
     public void setLogName(String logName)
 82  
     {
 83  0
         this.logName = logName;
 84  0
     }
 85  
 
 86  
     @Override
 87  
     protected void doInitialise() throws InitialisationException
 88  
     {
 89  0
         if (logConfigFile != null)
 90  
         {
 91  0
             if (logConfigFile.endsWith(".xml"))
 92  
             {
 93  0
                 DOMConfigurator.configure(logConfigFile);
 94  
             }
 95  
             else
 96  
             {
 97  0
                 PropertyConfigurator.configure(logConfigFile);
 98  
             }
 99  
         }
 100  
         else
 101  
         {
 102  0
             eventLogger = Logger.getLogger(logName);
 103  
 
 104  
             /*
 105  
              * TODO PAX Logging's Log4J version does not have the method
 106  
              * Logger.addAppender() and does not export the package
 107  
              * org.apache.log4j.net
 108  
              */
 109  
             // try
 110  
             // {
 111  
             // if (logFile != null)
 112  
             // {
 113  
             // File f = FileUtils.newFile(logFile);
 114  
             // if (!f.exists())
 115  
             // {
 116  
             // FileUtils.createFile(logFile);
 117  
             // }
 118  
             // Appender file = new RollingFileAppender(new PatternLayout("%5p %m%n"),
 119  
             // logFile, true);
 120  
             // eventLogger.addAppender(file);
 121  
             // }
 122  
             // if (chainsawPort > -1)
 123  
             // {
 124  
             // Appender chainsaw = new SocketAppender(chainsawHost, chainsawPort);
 125  
             // eventLogger.addAppender(chainsaw);
 126  
             // }
 127  
             // }
 128  
             // catch (IOException e)
 129  
             // {
 130  
             // throw new InitialisationException(
 131  
             // CoreMessages.failedToLoad("Log4j configuration"), e, this);
 132  
             // }
 133  
         }
 134  
 
 135  0
     }
 136  
 
 137  
     @Override
 138  
     protected void logEvent(ServerNotification e)
 139  
     {
 140  0
         if (eventLogger != null)
 141  
         {
 142  0
             String actionKey = e.EVENT_NAME + "." + e.getActionName();
 143  0
             String level = MapUtils.getString(levelMappings, actionKey, e.getType());
 144  
 
 145  0
             eventLogger.log(Level.toLevel(level, Level.INFO), e);
 146  
         }
 147  0
     }
 148  
 
 149  
     public String getLogFile()
 150  
     {
 151  0
         return logFile;
 152  
     }
 153  
 
 154  
     public void setLogFile(String logFile)
 155  
     {
 156  0
         this.logFile = logFile;
 157  0
     }
 158  
 
 159  
     public String getLogConfigFile()
 160  
     {
 161  0
         return logConfigFile;
 162  
     }
 163  
 
 164  
     public void setLogConfigFile(String logConfigFile)
 165  
     {
 166  0
         this.logConfigFile = logConfigFile;
 167  0
     }
 168  
 
 169  
     public String getChainsawHost()
 170  
     {
 171  0
         return chainsawHost;
 172  
     }
 173  
 
 174  
     public void setChainsawHost(String chainsawHost)
 175  
     {
 176  0
         this.chainsawHost = chainsawHost;
 177  0
     }
 178  
 
 179  
     public int getChainsawPort()
 180  
     {
 181  0
         return chainsawPort;
 182  
     }
 183  
 
 184  
     public void setChainsawPort(int chainsawPort)
 185  
     {
 186  0
         this.chainsawPort = chainsawPort;
 187  0
     }
 188  
 
 189  
     public Map<?, ?> getLevelMappings()
 190  
     {
 191  0
         return levelMappings;
 192  
     }
 193  
 
 194  
     public void setLevelMappings(Map levelMappings)
 195  
     {
 196  0
         this.levelMappings.putAll(levelMappings);
 197  0
     }
 198  
 }