1
2
3
4
5
6
7
8
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
27
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
49
50
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
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 }
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 }