View Javadoc

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