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