View Javadoc
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      private String logName = Log4jNotificationLoggerAgent.class.getName();
32      private String logFile = null;
33      private String logConfigFile = null;
34      private String chainsawHost = "localhost";
35      private int chainsawPort = -1;
36      private final Map<?, ?> levelMappings = new HashMap<Object, Object>();
37  
38      public Log4jNotificationLoggerAgent()
39      {
40          super("log4j-notifications");
41      }
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          StringBuffer buf = new StringBuffer(DEFAULT_DESCRIPTION_BUFFER_SIZE);
52  
53          if (StringUtils.isNotBlank(logName))
54          {
55              buf.append("Logging notifications to logger: ").append(logName);
56          }
57  
58          if (StringUtils.isNotBlank(logFile))
59          {
60              buf.append("Logging notifications to: ").append(logFile);
61          }
62  
63          if (chainsawPort > -1)
64          {
65              buf.append(" Chainsaw: ").append(chainsawHost).append(":").append(chainsawPort);
66          }
67  
68          if (buf.length() == 0)
69          {
70              buf.append("No logging or event forwarding is configured");
71          }
72  
73          return getName() + ": " + buf.toString();
74      }
75  
76      public String getLogName()
77      {
78          return logName;
79      }
80  
81      public void setLogName(String logName)
82      {
83          this.logName = logName;
84      }
85  
86      @Override
87      protected void doInitialise() throws InitialisationException
88      {
89          if (logConfigFile != null)
90          {
91              if (logConfigFile.endsWith(".xml"))
92              {
93                  DOMConfigurator.configure(logConfigFile);
94              }
95              else
96              {
97                  PropertyConfigurator.configure(logConfigFile);
98              }
99          }
100         else
101         {
102             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     }
136 
137     @Override
138     protected void logEvent(ServerNotification e)
139     {
140         if (eventLogger != null)
141         {
142             String actionKey = e.EVENT_NAME + "." + e.getActionName();
143             String level = MapUtils.getString(levelMappings, actionKey, e.getType());
144 
145             eventLogger.log(Level.toLevel(level, Level.INFO), e);
146         }
147     }
148 
149     public String getLogFile()
150     {
151         return logFile;
152     }
153 
154     public void setLogFile(String logFile)
155     {
156         this.logFile = logFile;
157     }
158 
159     public String getLogConfigFile()
160     {
161         return logConfigFile;
162     }
163 
164     public void setLogConfigFile(String logConfigFile)
165     {
166         this.logConfigFile = logConfigFile;
167     }
168 
169     public String getChainsawHost()
170     {
171         return chainsawHost;
172     }
173 
174     public void setChainsawHost(String chainsawHost)
175     {
176         this.chainsawHost = chainsawHost;
177     }
178 
179     public int getChainsawPort()
180     {
181         return chainsawPort;
182     }
183 
184     public void setChainsawPort(int chainsawPort)
185     {
186         this.chainsawPort = chainsawPort;
187     }
188 
189     public Map<?, ?> getLevelMappings()
190     {
191         return levelMappings;
192     }
193 
194     public void setLevelMappings(Map levelMappings)
195     {
196         this.levelMappings.putAll(levelMappings);
197     }
198 }