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