1 package org.apache.velocity.runtime.log;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.commons.lang.StringUtils;
28 import org.apache.log.Hierarchy;
29 import org.apache.log.LogTarget;
30 import org.apache.log.Logger;
31 import org.apache.log.Priority;
32 import org.apache.log.output.io.FileTarget;
33 import org.apache.velocity.runtime.RuntimeConstants;
34 import org.apache.velocity.runtime.RuntimeServices;
35
36
37
38
39
40
41
42
43
44 public class AvalonLogChute implements LogChute
45 {
46 public static final String AVALON_LOGGER = "runtime.log.logsystem.avalon.logger";
47
48 public static final String AVALON_LOGGER_FORMAT = "runtime.log.logsystem.avalon.format";
49
50 public static final String AVALON_LOGGER_LEVEL = "runtime.log.logsystem.avalon.level";
51
52 private Logger logger = null;
53 private RuntimeServices rsvc = null;
54
55 private static final Map logLevels = new HashMap();
56
57 static
58 {
59 logLevels.put("trace", Priority.DEBUG);
60 logLevels.put("debug", Priority.DEBUG);
61 logLevels.put("info", Priority.INFO);
62 logLevels.put("warn", Priority.WARN);
63 logLevels.put("error", Priority.ERROR);
64 }
65
66
67
68
69 public void init(RuntimeServices rs) throws Exception
70 {
71 this.rsvc = rs;
72
73
74 String name = (String)rsvc.getProperty(AVALON_LOGGER);
75 if (name != null)
76 {
77 this.logger = Hierarchy.getDefaultHierarchy().getLoggerFor(name);
78 }
79 else
80 {
81
82 logger = Hierarchy.getDefaultHierarchy().getLoggerFor(rsvc.toString());
83
84
85 String file = (String)rsvc.getProperty(RuntimeConstants.RUNTIME_LOG);
86 if (StringUtils.isNotEmpty(file))
87 {
88 initTarget(file, rsvc);
89 }
90 }
91 }
92
93
94 private void initTarget(final String file, final RuntimeServices rsvc) throws Exception
95 {
96 try
97 {
98 String format = null;
99 Priority level = null;
100 if (rsvc != null)
101 {
102 format = rsvc.getString(AVALON_LOGGER_FORMAT, "%{time} %{message}\\n%{throwable}");
103 level = (Priority) logLevels.get(rsvc.getString(AVALON_LOGGER_LEVEL, "warn"));
104 }
105
106 VelocityFormatter vf = new VelocityFormatter(format);
107
108
109 FileTarget target = new FileTarget(new File(file), false, vf);
110
111 logger.setPriority(level);
112 logger.setLogTargets(new LogTarget[] { target });
113 log(DEBUG_ID, "AvalonLogChute initialized using file '"+file+'\'');
114 }
115 catch (IOException ioe)
116 {
117 rsvc.getLog().warn("Unable to create log file for AvalonLogChute", ioe);
118 throw new Exception("Error configuring AvalonLogChute : " + ioe);
119 }
120 }
121
122
123
124
125
126
127
128
129 public void init(String file) throws Exception
130 {
131 logger = Hierarchy.getDefaultHierarchy().getLoggerFor(rsvc.toString());
132 initTarget(file, null);
133
134 log(WARN_ID, "You shouldn't be using the init(String file) method!");
135 }
136
137
138
139
140
141
142
143 public void log(int level, String message)
144 {
145
146
147
148
149 switch (level)
150 {
151 case WARN_ID:
152 logger.warn(WARN_PREFIX + message );
153 break;
154 case INFO_ID:
155 logger.info(INFO_PREFIX + message);
156 break;
157 case DEBUG_ID:
158 logger.debug(DEBUG_PREFIX + message);
159 break;
160 case TRACE_ID:
161 logger.debug(TRACE_PREFIX + message);
162 break;
163 case ERROR_ID:
164 logger.error(ERROR_PREFIX + message);
165 break;
166 default:
167 logger.info(message);
168 break;
169 }
170 }
171
172
173
174
175
176
177
178
179 public void log(int level, String message, Throwable t)
180 {
181 switch (level)
182 {
183 case WARN_ID:
184 logger.warn(WARN_PREFIX + message, t);
185 break;
186 case INFO_ID:
187 logger.info(INFO_PREFIX + message, t);
188 break;
189 case DEBUG_ID:
190 logger.debug(DEBUG_PREFIX + message, t);
191 break;
192 case TRACE_ID:
193 logger.debug(TRACE_PREFIX + message, t);
194 break;
195 case ERROR_ID:
196 logger.error(ERROR_PREFIX + message, t);
197 break;
198 default:
199 logger.info(message, t);
200 break;
201 }
202 }
203
204
205
206
207
208
209 public boolean isLevelEnabled(int level)
210 {
211 switch (level)
212 {
213
214 case TRACE_ID:
215 case DEBUG_ID:
216 return logger.isDebugEnabled();
217 case INFO_ID:
218 return logger.isInfoEnabled();
219 case WARN_ID:
220 return logger.isWarnEnabled();
221 case ERROR_ID:
222 return logger.isErrorEnabled();
223 default:
224 return true;
225 }
226 }
227
228
229
230
231
232 protected void finalize() throws Throwable
233 {
234 shutdown();
235 }
236
237
238 public void shutdown()
239 {
240 logger.unsetLogTargets();
241 }
242
243 }