View Javadoc

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      private String logName = Log4jNotificationLoggerAgent.class.getName();
45      private String logFile = null;
46      private String logConfigFile = null;
47      private String chainsawHost = "localhost";
48      private int chainsawPort = -1;
49      private Map levelMappings = new HashMap();
50  
51  
52      public Log4jNotificationLoggerAgent()
53      {
54          super("log4j-notifications");
55      }
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          StringBuffer buf = new StringBuffer(DEFAULT_DESCRIPTION_BUFFER_SIZE);
65          if (StringUtils.isNotBlank(logFile))
66          {
67              buf.append("Logging notifications to: ").append(logFile);
68          }
69          if (chainsawPort > -1)
70          {
71              buf.append(" Chainsaw: ").append(chainsawHost).append(":").append(chainsawPort);
72          }
73          if (buf.length() == 0)
74          {
75              buf.append("No logging or event forwarding is configured");
76          }
77          return getName() + ": " + buf.toString();
78      }
79  
80      public String getLogName()
81      {
82          return logName;
83      }
84  
85      public void setLogName(String logName)
86      {
87          this.logName = logName;
88      }
89  
90      protected void doInitialise() throws InitialisationException
91      {
92          if (logConfigFile != null)
93          {
94              if (logConfigFile.endsWith(".xml"))
95              {
96                  DOMConfigurator.configure(logConfigFile);
97              }
98              else
99              {
100                 PropertyConfigurator.configure(logConfigFile);
101             }
102         }
103         else
104         {
105             try
106             {
107                 eventLogger = Logger.getLogger(logName);
108                 if (logFile != null)
109                 {
110                     File f = FileUtils.newFile(logFile);
111                     if (!f.exists())
112                     {
113                         FileUtils.createFile(logFile);
114                     }
115                     Appender file = new RollingFileAppender(new PatternLayout("%5p %m%n"), logFile, true);
116                     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             catch (IOException e)
127             {
128                 throw new InitialisationException(
129                     CoreMessages.failedToLoad("Log4j configuration"), e, this);
130             }
131         }
132     }
133 
134     protected void logEvent(ServerNotification e)
135     {
136         if (eventLogger != null)
137         {
138             String actionKey = e.EVENT_NAME + "." + e.getActionName();
139             String level = MapUtils.getString(levelMappings, actionKey, e.getType());
140 
141             eventLogger.log(Level.toLevel(level, Level.INFO), e);
142         }
143     }
144 
145     public String getLogFile()
146     {
147         return logFile;
148     }
149 
150     public void setLogFile(String logFile)
151     {
152         this.logFile = logFile;
153     }
154 
155     public String getLogConfigFile()
156     {
157         return logConfigFile;
158     }
159 
160     public void setLogConfigFile(String logConfigFile)
161     {
162         this.logConfigFile = logConfigFile;
163     }
164 
165     public String getChainsawHost()
166     {
167         return chainsawHost;
168     }
169 
170     public void setChainsawHost(String chainsawHost)
171     {
172         this.chainsawHost = chainsawHost;
173     }
174 
175     public int getChainsawPort()
176     {
177         return chainsawPort;
178     }
179 
180     public void setChainsawPort(int chainsawPort)
181     {
182         this.chainsawPort = chainsawPort;
183     }
184 
185     public Map getLevelMappings()
186     {
187         return levelMappings;
188     }
189 
190     public void setLevelMappings(Map levelMappings)
191     {
192         this.levelMappings.putAll(levelMappings);
193     }
194 }