1
2
3
4
5
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
23
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
45
46
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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 }