KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > internal > admin > Log4jNotificationLoggerAgent


1 /*
2  * $Id: Log4jNotificationLoggerAgent.java 3937 2006-11-20 16:04:25Z lajos $
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 MuleSource MPL
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.apache.commons.collections.MapUtils;
14 import org.apache.commons.lang.StringUtils;
15 import org.apache.log4j.Appender;
16 import org.apache.log4j.Level;
17 import org.apache.log4j.Logger;
18 import org.apache.log4j.PatternLayout;
19 import org.apache.log4j.PropertyConfigurator;
20 import org.apache.log4j.RollingFileAppender;
21 import org.apache.log4j.net.SocketAppender;
22 import org.apache.log4j.xml.DOMConfigurator;
23 import org.mule.config.i18n.Message;
24 import org.mule.config.i18n.Messages;
25 import org.mule.umo.lifecycle.InitialisationException;
26 import org.mule.umo.manager.UMOServerNotification;
27 import org.mule.util.FileUtils;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.Map JavaDoc;
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 Logger eventLogger;
43     private String JavaDoc logName = Log4jNotificationLoggerAgent.class.getName();
44     private String JavaDoc logFile = null;
45     private String JavaDoc logConfigFile = null;
46     private String JavaDoc chainsawHost = "localhost";
47     private int chainsawPort = -1;
48     private Map JavaDoc levelMappings = new HashMap JavaDoc();
49
50     /**
51      * Should be a 1 line description of the agent
52      *
53      * @return the description of this Agent
54      */

55     public String JavaDoc getDescription()
56     {
57         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(64);
58         if (StringUtils.isNotBlank(logFile))
59         {
60             buf.append("Logging notifications to: ").append(logFile);
61         }
62         if (chainsawPort > -1)
63         {
64             buf.append(" Chainsaw: ").append(chainsawHost).append(":").append(chainsawPort);
65         }
66         if (buf.length() == 0)
67         {
68             buf.append("No logging or event forwarding is configured");
69         }
70         return getName() + ": " + buf.toString();
71     }
72
73     public String JavaDoc getLogName()
74     {
75         return logName;
76     }
77
78     public void setLogName(String JavaDoc logName)
79     {
80         this.logName = logName;
81     }
82
83     public void doInitialise() throws InitialisationException
84     {
85         if (logConfigFile != null)
86         {
87             if (logConfigFile.endsWith(".xml"))
88             {
89                 DOMConfigurator.configure(logConfigFile);
90             }
91             else
92             {
93                 PropertyConfigurator.configure(logConfigFile);
94             }
95         }
96         else
97         {
98             try
99             {
100                 eventLogger = Logger.getLogger(logName);
101                 if (logFile != null)
102                 {
103                     File JavaDoc f = FileUtils.newFile(logFile);
104                     if (!f.exists())
105                     {
106                         FileUtils.createFile(logFile);
107                     }
108                     Appender file = new RollingFileAppender(new PatternLayout("%5p %m%n"), logFile, true);
109                     eventLogger.addAppender(file);
110                 }
111                 if (chainsawPort > -1)
112                 {
113                     Appender chainsaw = new SocketAppender(chainsawHost, chainsawPort);
114                     eventLogger.addAppender(chainsaw);
115                 }
116             }
117             catch (IOException JavaDoc e)
118             {
119                 throw new InitialisationException(new Message(Messages.FAILED_LOAD_X, "Log4j configuration"),
120                     e, this);
121             }
122         }
123     }
124
125     protected void logEvent(UMOServerNotification e)
126     {
127         if (eventLogger != null)
128         {
129             String JavaDoc actionKey = e.EVENT_NAME + "." + e.getActionName();
130             String JavaDoc level = MapUtils.getString(levelMappings, actionKey, e.getType());
131
132             eventLogger.log(Level.toLevel(level, Level.INFO), e);
133         }
134     }
135
136     public String JavaDoc getLogFile()
137     {
138         return logFile;
139     }
140
141     public void setLogFile(String JavaDoc logFile)
142     {
143         this.logFile = logFile;
144     }
145
146     public String JavaDoc getLogConfigFile()
147     {
148         return logConfigFile;
149     }
150
151     public void setLogConfigFile(String JavaDoc logConfigFile)
152     {
153         this.logConfigFile = logConfigFile;
154     }
155
156     public String JavaDoc getChainsawHost()
157     {
158         return chainsawHost;
159     }
160
161     public void setChainsawHost(String JavaDoc chainsawHost)
162     {
163         this.chainsawHost = chainsawHost;
164     }
165
166     public int getChainsawPort()
167     {
168         return chainsawPort;
169     }
170
171     public void setChainsawPort(int chainsawPort)
172     {
173         this.chainsawPort = chainsawPort;
174     }
175
176     public Map JavaDoc getLevelMappings()
177     {
178         return levelMappings;
179     }
180
181     public void setLevelMappings(Map JavaDoc levelMappings)
182     {
183         this.levelMappings.putAll(levelMappings);
184     }
185 }
186
Popular Tags