View Javadoc

1   /*
2    * $Id: Log4jNotificationLoggerAgent.java 7963 2007-08-21 08:53:15Z dirk.olmes $
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.impl.internal.admin;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.umo.lifecycle.InitialisationException;
15  import org.mule.umo.manager.UMOServerNotification;
16  import org.mule.util.FileUtils;
17  import org.mule.util.StringUtils;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.collections.MapUtils;
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.net.SocketAppender;
32  import org.apache.log4j.xml.DOMConfigurator;
33  
34  /**
35   * <code>AbstractNotificationLoggerAgent</code> Receives Mule server notifications
36   * and logs them and can optionally route them to an endpoint
37   * 
38   */
39  public class Log4jNotificationLoggerAgent extends AbstractNotificationLoggerAgent
40  {
41  
42      protected static final int DEFAULT_DESCRIPTION_BUFFER_SIZE = 64;
43  
44      protected Logger eventLogger;
45      private String logName = Log4jNotificationLoggerAgent.class.getName();
46      private String logFile = null;
47      private String logConfigFile = null;
48      private String chainsawHost = "localhost";
49      private int chainsawPort = -1;
50      private Map levelMappings = new HashMap();
51  
52      /**
53       * Should be a 1 line description of the agent
54       * 
55       * @return the description of this Agent
56       */
57      public String getDescription()
58      {
59          StringBuffer buf = new StringBuffer(DEFAULT_DESCRIPTION_BUFFER_SIZE);
60          if (StringUtils.isNotBlank(logFile))
61          {
62              buf.append("Logging notifications to: ").append(logFile);
63          }
64          if (chainsawPort > -1)
65          {
66              buf.append(" Chainsaw: ").append(chainsawHost).append(":").append(chainsawPort);
67          }
68          if (buf.length() == 0)
69          {
70              buf.append("No logging or event forwarding is configured");
71          }
72          return getName() + ": " + buf.toString();
73      }
74  
75      public String getLogName()
76      {
77          return logName;
78      }
79  
80      public void setLogName(String logName)
81      {
82          this.logName = logName;
83      }
84  
85      protected void doInitialise() throws InitialisationException
86      {
87          if (logConfigFile != null)
88          {
89              if (logConfigFile.endsWith(".xml"))
90              {
91                  DOMConfigurator.configure(logConfigFile);
92              }
93              else
94              {
95                  PropertyConfigurator.configure(logConfigFile);
96              }
97          }
98          else
99          {
100             try
101             {
102                 eventLogger = Logger.getLogger(logName);
103                 if (logFile != null)
104                 {
105                     File f = FileUtils.newFile(logFile);
106                     if (!f.exists())
107                     {
108                         FileUtils.createFile(logFile);
109                     }
110                     Appender file = new RollingFileAppender(new PatternLayout("%5p %m%n"), logFile, true);
111                     eventLogger.addAppender(file);
112                 }
113                 if (chainsawPort > -1)
114                 {
115                     Appender chainsaw = new SocketAppender(chainsawHost, chainsawPort);
116                     eventLogger.addAppender(chainsaw);
117                 }
118             }
119             catch (IOException e)
120             {
121                 throw new InitialisationException(
122                     CoreMessages.failedToLoad("Log4j configuration"), e, this);
123             }
124         }
125     }
126 
127     protected void logEvent(UMOServerNotification e)
128     {
129         if (eventLogger != null)
130         {
131             String actionKey = e.EVENT_NAME + "." + e.getActionName();
132             String level = MapUtils.getString(levelMappings, actionKey, e.getType());
133 
134             eventLogger.log(Level.toLevel(level, Level.INFO), e);
135         }
136     }
137 
138     public String getLogFile()
139     {
140         return logFile;
141     }
142 
143     public void setLogFile(String logFile)
144     {
145         this.logFile = logFile;
146     }
147 
148     public String getLogConfigFile()
149     {
150         return logConfigFile;
151     }
152 
153     public void setLogConfigFile(String logConfigFile)
154     {
155         this.logConfigFile = logConfigFile;
156     }
157 
158     public String getChainsawHost()
159     {
160         return chainsawHost;
161     }
162 
163     public void setChainsawHost(String chainsawHost)
164     {
165         this.chainsawHost = chainsawHost;
166     }
167 
168     public int getChainsawPort()
169     {
170         return chainsawPort;
171     }
172 
173     public void setChainsawPort(int chainsawPort)
174     {
175         this.chainsawPort = chainsawPort;
176     }
177 
178     public Map getLevelMappings()
179     {
180         return levelMappings;
181     }
182 
183     public void setLevelMappings(Map levelMappings)
184     {
185         this.levelMappings.putAll(levelMappings);
186     }
187 }