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