1 | package org.apache.velocity.runtime.log; |
2 | |
3 | /* |
4 | * Copyright 2001,2004 The Apache Software Foundation. |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * You may obtain a copy of the License at |
9 | * |
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | * |
12 | * Unless required by applicable law or agreed to in writing, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | */ |
18 | |
19 | import java.util.Enumeration; |
20 | |
21 | import org.apache.log4j.*; |
22 | import org.apache.log4j.net.*; |
23 | |
24 | import org.apache.velocity.runtime.RuntimeConstants; |
25 | import org.apache.velocity.runtime.RuntimeServices; |
26 | |
27 | /** |
28 | * Implementation of a Log4J logger. |
29 | * |
30 | * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a> |
31 | * @version $Id: Log4JLogSystem.java,v 1.7.4.1 2004/03/03 23:22:56 geirm Exp $ |
32 | * |
33 | * @deprecated As of v1.3. Use |
34 | * {@link SimpleLog4jLogSystem} |
35 | */ |
36 | public class Log4JLogSystem implements LogSystem |
37 | { |
38 | private RuntimeServices rsvc = null; |
39 | |
40 | /** log4java logging interface */ |
41 | protected Category logger = null; |
42 | |
43 | /** logging layout */ |
44 | protected Layout layout = null; |
45 | |
46 | /** the runtime.log property value */ |
47 | private String logfile = ""; |
48 | |
49 | /** |
50 | * default CTOR. Initializes itself using the property RUNTIME_LOG |
51 | * from the Velocity properties |
52 | */ |
53 | public Log4JLogSystem() |
54 | { |
55 | } |
56 | |
57 | public void init( RuntimeServices rs ) |
58 | { |
59 | rsvc = rs; |
60 | |
61 | /* |
62 | * since this is a Velocity-provided logger, we will |
63 | * use the Runtime configuration |
64 | */ |
65 | logfile = rsvc.getString( RuntimeConstants.RUNTIME_LOG ); |
66 | |
67 | /* |
68 | * now init. If we can't, panic! |
69 | */ |
70 | try |
71 | { |
72 | internalInit(); |
73 | |
74 | logVelocityMessage( 0, |
75 | "Log4JLogSystem initialized using logfile " + logfile ); |
76 | } |
77 | catch( Exception e ) |
78 | { |
79 | System.out.println( |
80 | "PANIC : error configuring Log4JLogSystem : " + e ); |
81 | } |
82 | } |
83 | |
84 | /** |
85 | * initializes the log system using the logfile argument |
86 | * |
87 | * @param logFile file for log messages |
88 | */ |
89 | private void internalInit() |
90 | throws Exception |
91 | { |
92 | logger = Category.getInstance(""); |
93 | logger.setAdditivity(false); |
94 | |
95 | /* |
96 | * Priority is set for DEBUG becouse this implementation checks |
97 | * log level. |
98 | */ |
99 | logger.setPriority(Priority.DEBUG); |
100 | |
101 | String pattern = rsvc.getString( RuntimeConstants.LOGSYSTEM_LOG4J_PATTERN ); |
102 | |
103 | if (pattern == null || pattern.length() == 0) |
104 | { |
105 | pattern = "%d - %m%n"; |
106 | } |
107 | |
108 | layout = new PatternLayout(pattern); |
109 | |
110 | configureFile(); |
111 | configureRemote(); |
112 | configureSyslog(); |
113 | configureEmail(); |
114 | } |
115 | |
116 | /** |
117 | * Configures the logging to a file. |
118 | */ |
119 | private void configureFile() |
120 | throws Exception |
121 | { |
122 | int backupFiles = |
123 | rsvc.getInt(RuntimeConstants.LOGSYSTEM_LOG4J_FILE_BACKUPS, 1); |
124 | int fileSize = |
125 | rsvc.getInt(RuntimeConstants.LOGSYSTEM_LOG4J_FILE_SIZE, 100000); |
126 | |
127 | Appender appender = new RollingFileAppender(layout,logfile,true); |
128 | |
129 | ((RollingFileAppender)appender).setMaxBackupIndex(backupFiles); |
130 | |
131 | /* finding file size */ |
132 | if (fileSize > -1) |
133 | { |
134 | ((RollingFileAppender)appender).setMaximumFileSize(fileSize); |
135 | } |
136 | logger.addAppender(appender); |
137 | } |
138 | |
139 | /** |
140 | * Configures the logging to a remote server |
141 | */ |
142 | private void configureRemote() |
143 | throws Exception |
144 | { |
145 | String remoteHost = |
146 | rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_REMOTE_HOST); |
147 | int remotePort = |
148 | rsvc.getInt(RuntimeConstants.LOGSYSTEM_LOG4J_REMOTE_PORT, 1099); |
149 | |
150 | if (remoteHost == null || remoteHost.trim().equals("") || |
151 | remotePort <= 0) |
152 | { |
153 | return; |
154 | } |
155 | |
156 | Appender appender=new SocketAppender(remoteHost,remotePort); |
157 | |
158 | logger.addAppender(appender); |
159 | } |
160 | |
161 | /** |
162 | * Configures the logging to syslogd |
163 | */ |
164 | private void configureSyslog() |
165 | throws Exception |
166 | { |
167 | String syslogHost = |
168 | rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_SYSLOGD_HOST); |
169 | String syslogFacility = |
170 | rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_SYSLOGD_FACILITY); |
171 | |
172 | if (syslogHost == null || syslogHost.trim().equals("") || |
173 | syslogFacility == null ) |
174 | { |
175 | return; |
176 | } |
177 | |
178 | Appender appender = new SyslogAppender(); |
179 | |
180 | ((SyslogAppender)appender).setLayout(layout); |
181 | ((SyslogAppender)appender).setSyslogHost(syslogHost); |
182 | ((SyslogAppender)appender).setFacility(syslogFacility); |
183 | |
184 | logger.addAppender(appender); |
185 | } |
186 | |
187 | /** |
188 | * Configures the logging to email |
189 | */ |
190 | private void configureEmail() |
191 | throws Exception |
192 | { |
193 | String smtpHost = |
194 | rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_SERVER); |
195 | String emailFrom = |
196 | rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_FROM); |
197 | String emailTo = |
198 | rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_TO); |
199 | String emailSubject = |
200 | rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_SUBJECT); |
201 | String bufferSize = |
202 | rsvc.getString(RuntimeConstants.LOGSYSTEM_LOG4J_EMAIL_BUFFER_SIZE); |
203 | |
204 | if (smtpHost == null || smtpHost.trim().equals("") |
205 | || emailFrom == null || smtpHost.trim().equals("") |
206 | || emailTo == null || emailTo.trim().equals("") |
207 | || emailSubject == null || emailSubject.trim().equals("") |
208 | || bufferSize == null || bufferSize.trim().equals("") ) |
209 | { |
210 | return; |
211 | } |
212 | |
213 | SMTPAppender appender = new SMTPAppender(); |
214 | |
215 | appender.setSMTPHost( smtpHost ); |
216 | appender.setFrom( emailFrom ); |
217 | appender.setTo( emailTo ); |
218 | appender.setSubject( emailSubject ); |
219 | |
220 | appender.setBufferSize( Integer.parseInt(bufferSize) ); |
221 | |
222 | appender.setLayout(layout); |
223 | appender.activateOptions(); |
224 | logger.addAppender(appender); |
225 | } |
226 | |
227 | /** |
228 | * logs messages |
229 | * |
230 | * @param level severity level |
231 | * @param message complete error message |
232 | */ |
233 | public void logVelocityMessage(int level, String message) |
234 | { |
235 | switch (level) |
236 | { |
237 | case LogSystem.WARN_ID: |
238 | logger.warn( message ); |
239 | break; |
240 | case LogSystem.INFO_ID: |
241 | logger.info(message); |
242 | break; |
243 | case LogSystem.DEBUG_ID: |
244 | logger.debug(message); |
245 | break; |
246 | case LogSystem.ERROR_ID: |
247 | logger.error(message); |
248 | break; |
249 | default: |
250 | logger.debug(message); |
251 | break; |
252 | } |
253 | } |
254 | |
255 | /** |
256 | * Also do a shutdown if the object is destroy()'d. |
257 | */ |
258 | protected void finalize() throws Throwable |
259 | { |
260 | shutdown(); |
261 | } |
262 | |
263 | /** Close all destinations*/ |
264 | public void shutdown() |
265 | { |
266 | Enumeration appenders = logger.getAllAppenders(); |
267 | while (appenders.hasMoreElements()) |
268 | { |
269 | Appender appender = (Appender)appenders.nextElement(); |
270 | appender.close(); |
271 | } |
272 | } |
273 | } |