1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.impl.internal.admin; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.umo.lifecycle.InitialisationException; |
15 | |
import org.mule.umo.manager.UMOServerNotification; |
16 | |
import org.mule.util.FileUtils; |
17 | |
import org.mule.util.StringUtils; |
18 | |
|
19 | |
import java.io.File; |
20 | |
import java.io.IOException; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import org.apache.commons.collections.MapUtils; |
25 | |
import org.apache.log4j.Appender; |
26 | |
import org.apache.log4j.Level; |
27 | |
import org.apache.log4j.Logger; |
28 | |
import org.apache.log4j.PatternLayout; |
29 | |
import org.apache.log4j.PropertyConfigurator; |
30 | |
import org.apache.log4j.RollingFileAppender; |
31 | |
import org.apache.log4j.net.SocketAppender; |
32 | |
import org.apache.log4j.xml.DOMConfigurator; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class Log4jNotificationLoggerAgent extends AbstractNotificationLoggerAgent |
40 | |
{ |
41 | |
|
42 | |
protected static final int DEFAULT_DESCRIPTION_BUFFER_SIZE = 64; |
43 | |
|
44 | |
protected Logger eventLogger; |
45 | 0 | private String logName = Log4jNotificationLoggerAgent.class.getName(); |
46 | 0 | private String logFile = null; |
47 | 0 | private String logConfigFile = null; |
48 | 0 | private String chainsawHost = "localhost"; |
49 | 0 | private int chainsawPort = -1; |
50 | 0 | private Map levelMappings = new HashMap(); |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
public String getDescription() |
58 | |
{ |
59 | 0 | StringBuffer buf = new StringBuffer(DEFAULT_DESCRIPTION_BUFFER_SIZE); |
60 | 0 | if (StringUtils.isNotBlank(logFile)) |
61 | |
{ |
62 | 0 | buf.append("Logging notifications to: ").append(logFile); |
63 | |
} |
64 | 0 | if (chainsawPort > -1) |
65 | |
{ |
66 | 0 | buf.append(" Chainsaw: ").append(chainsawHost).append(":").append(chainsawPort); |
67 | |
} |
68 | 0 | if (buf.length() == 0) |
69 | |
{ |
70 | 0 | buf.append("No logging or event forwarding is configured"); |
71 | |
} |
72 | 0 | return getName() + ": " + buf.toString(); |
73 | |
} |
74 | |
|
75 | |
public String getLogName() |
76 | |
{ |
77 | 0 | return logName; |
78 | |
} |
79 | |
|
80 | |
public void setLogName(String logName) |
81 | |
{ |
82 | 0 | this.logName = logName; |
83 | 0 | } |
84 | |
|
85 | |
protected void doInitialise() throws InitialisationException |
86 | |
{ |
87 | 0 | if (logConfigFile != null) |
88 | |
{ |
89 | 0 | if (logConfigFile.endsWith(".xml")) |
90 | |
{ |
91 | 0 | DOMConfigurator.configure(logConfigFile); |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | PropertyConfigurator.configure(logConfigFile); |
96 | |
} |
97 | |
} |
98 | |
else |
99 | |
{ |
100 | |
try |
101 | |
{ |
102 | 0 | eventLogger = Logger.getLogger(logName); |
103 | 0 | if (logFile != null) |
104 | |
{ |
105 | 0 | File f = FileUtils.newFile(logFile); |
106 | 0 | if (!f.exists()) |
107 | |
{ |
108 | 0 | FileUtils.createFile(logFile); |
109 | |
} |
110 | 0 | Appender file = new RollingFileAppender(new PatternLayout("%5p %m%n"), logFile, true); |
111 | 0 | eventLogger.addAppender(file); |
112 | |
} |
113 | 0 | if (chainsawPort > -1) |
114 | |
{ |
115 | 0 | Appender chainsaw = new SocketAppender(chainsawHost, chainsawPort); |
116 | 0 | eventLogger.addAppender(chainsaw); |
117 | |
} |
118 | |
} |
119 | 0 | catch (IOException e) |
120 | |
{ |
121 | 0 | throw new InitialisationException( |
122 | |
CoreMessages.failedToLoad("Log4j configuration"), e, this); |
123 | 0 | } |
124 | |
} |
125 | 0 | } |
126 | |
|
127 | |
protected void logEvent(UMOServerNotification e) |
128 | |
{ |
129 | 0 | if (eventLogger != null) |
130 | |
{ |
131 | 0 | String actionKey = e.EVENT_NAME + "." + e.getActionName(); |
132 | 0 | String level = MapUtils.getString(levelMappings, actionKey, e.getType()); |
133 | |
|
134 | 0 | eventLogger.log(Level.toLevel(level, Level.INFO), e); |
135 | |
} |
136 | 0 | } |
137 | |
|
138 | |
public String getLogFile() |
139 | |
{ |
140 | 0 | return logFile; |
141 | |
} |
142 | |
|
143 | |
public void setLogFile(String logFile) |
144 | |
{ |
145 | 0 | this.logFile = logFile; |
146 | 0 | } |
147 | |
|
148 | |
public String getLogConfigFile() |
149 | |
{ |
150 | 0 | return logConfigFile; |
151 | |
} |
152 | |
|
153 | |
public void setLogConfigFile(String logConfigFile) |
154 | |
{ |
155 | 0 | this.logConfigFile = logConfigFile; |
156 | 0 | } |
157 | |
|
158 | |
public String getChainsawHost() |
159 | |
{ |
160 | 0 | return chainsawHost; |
161 | |
} |
162 | |
|
163 | |
public void setChainsawHost(String chainsawHost) |
164 | |
{ |
165 | 0 | this.chainsawHost = chainsawHost; |
166 | 0 | } |
167 | |
|
168 | |
public int getChainsawPort() |
169 | |
{ |
170 | 0 | return chainsawPort; |
171 | |
} |
172 | |
|
173 | |
public void setChainsawPort(int chainsawPort) |
174 | |
{ |
175 | 0 | this.chainsawPort = chainsawPort; |
176 | 0 | } |
177 | |
|
178 | |
public Map getLevelMappings() |
179 | |
{ |
180 | 0 | return levelMappings; |
181 | |
} |
182 | |
|
183 | |
public void setLevelMappings(Map levelMappings) |
184 | |
{ |
185 | 0 | this.levelMappings.putAll(levelMappings); |
186 | 0 | } |
187 | |
} |