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