1 package org.apache.velocity.runtime.log;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import org.apache.log4j.Category;
23 import org.apache.log4j.Level;
24 import org.apache.log4j.PatternLayout;
25 import org.apache.log4j.RollingFileAppender;
26 import org.apache.velocity.runtime.RuntimeConstants;
27 import org.apache.velocity.runtime.RuntimeServices;
28
29 /**
30 * <p><em>This class is deprecated in favor of the new {@link Log4JLogChute},
31 * which makes use of Log4J's <code>Logger</code> rather than its deprecated
32 * <code>Category</code> class.</em></p>
33 *
34 * Implementation of a simple log4j system that will either
35 * latch onto an existing category, or just do a simple
36 * rolling file log. Derived from Jon's 'complicated'
37 * version :)
38 *
39 * @author <a href="mailto:geirm@apache.org>Geir Magnusson Jr.</a>
40 * @version $Id: SimpleLog4JLogSystem.java 463298 2006-10-12 16:10:32Z henning $
41 * @deprecated Use Log4JLogChute instead.
42 */
43 public class SimpleLog4JLogSystem implements LogSystem
44 {
45 private RuntimeServices rsvc = null;
46 private RollingFileAppender appender = null;
47
48 /** log4java logging interface */
49 protected Category logger = null;
50
51 /**
52 *
53 */
54 public SimpleLog4JLogSystem()
55 {
56 }
57
58 /**
59 * @see org.apache.velocity.runtime.log.LogSystem#init(org.apache.velocity.runtime.RuntimeServices)
60 */
61 public void init( RuntimeServices rs )
62 {
63 rsvc = rs;
64
65 /*
66 * first see if there is a category specified and just use that - it allows
67 * the application to make us use an existing logger
68 */
69
70 String categoryname = (String) rsvc.getProperty("runtime.log.logsystem.log4j.category");
71
72 if ( categoryname != null )
73 {
74 logger = Category.getInstance( categoryname );
75
76 logVelocityMessage( 0,
77 "SimpleLog4JLogSystem using category '" + categoryname + "'");
78
79 return;
80 }
81
82 /*
83 * if not, use the file...
84 */
85
86 String logfile = rsvc.getString( RuntimeConstants.RUNTIME_LOG );
87
88 /*
89 * now init. If we can't, panic!
90 */
91 try
92 {
93 internalInit( logfile );
94
95 logVelocityMessage( 0,
96 "SimpleLog4JLogSystem initialized using logfile '" + logfile + "'" );
97 }
98 catch( Exception e )
99 {
100 System.err.println(
101 "PANIC : error configuring SimpleLog4JLogSystem : " + e );
102 }
103 }
104
105 /**
106 * initializes the log system using the logfile argument
107 */
108 private void internalInit( String logfile )
109 throws Exception
110 {
111 /*
112 * do it by our classname to avoid conflicting with anything else
113 * that might be used...
114 */
115
116 logger = Category.getInstance(this.getClass().getName());
117 logger.setAdditivity(false);
118
119 /*
120 * Priority is set for DEBUG becouse this implementation checks
121 * log level.
122 */
123 logger.setLevel(Level.DEBUG);
124
125 appender = new RollingFileAppender( new PatternLayout( "%d - %m%n"), logfile, true);
126
127 appender.setMaxBackupIndex( 1 );
128
129 appender.setMaximumFileSize( 100000 );
130
131 logger.addAppender(appender);
132 }
133
134 /**
135 * logs messages
136 *
137 * @param level severity level
138 * @param message complete error message
139 */
140 public void logVelocityMessage(int level, String message)
141 {
142 switch (level)
143 {
144 case LogSystem.WARN_ID:
145 logger.warn( message );
146 break;
147 case LogSystem.INFO_ID:
148 logger.info(message);
149 break;
150 case LogSystem.DEBUG_ID:
151 logger.debug(message);
152 break;
153 case LogSystem.ERROR_ID:
154 logger.error(message);
155 break;
156 default:
157 logger.debug(message);
158 break;
159 }
160 }
161
162 /**
163 * Also do a shutdown if the object is destroy()'d.
164 * @throws Throwable
165 */
166 protected void finalize() throws Throwable
167 {
168 shutdown();
169 }
170
171 /** Close all destinations*/
172 public void shutdown()
173 {
174 if (appender != null)
175 {
176 logger.removeAppender(appender);
177 appender.close();
178 appender = null;
179 }
180 }
181 }