001: package org.apache.velocity.runtime.log;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import org.apache.log4j.Category;
023: import org.apache.log4j.Level;
024: import org.apache.log4j.PatternLayout;
025: import org.apache.log4j.RollingFileAppender;
026: import org.apache.velocity.runtime.RuntimeConstants;
027: import org.apache.velocity.runtime.RuntimeServices;
028:
029: /**
030: * <p><em>This class is deprecated in favor of the new {@link Log4JLogChute},
031: * which makes use of Log4J's <code>Logger</code> rather than its deprecated
032: * <code>Category</code> class.</em></p>
033: *
034: * Implementation of a simple log4j system that will either
035: * latch onto an existing category, or just do a simple
036: * rolling file log. Derived from Jon's 'complicated'
037: * version :)
038: *
039: * @author <a href="mailto:geirm@apache.org>Geir Magnusson Jr.</a>
040: * @version $Id: SimpleLog4JLogSystem.java 463298 2006-10-12 16:10:32Z henning $
041: * @deprecated Use Log4JLogChute instead.
042: */
043: public class SimpleLog4JLogSystem implements LogSystem {
044: private RuntimeServices rsvc = null;
045: private RollingFileAppender appender = null;
046:
047: /** log4java logging interface */
048: protected Category logger = null;
049:
050: /**
051: *
052: */
053: public SimpleLog4JLogSystem() {
054: }
055:
056: /**
057: * @see org.apache.velocity.runtime.log.LogSystem#init(org.apache.velocity.runtime.RuntimeServices)
058: */
059: public void init(RuntimeServices rs) {
060: rsvc = rs;
061:
062: /*
063: * first see if there is a category specified and just use that - it allows
064: * the application to make us use an existing logger
065: */
066:
067: String categoryname = (String) rsvc
068: .getProperty("runtime.log.logsystem.log4j.category");
069:
070: if (categoryname != null) {
071: logger = Category.getInstance(categoryname);
072:
073: logVelocityMessage(0,
074: "SimpleLog4JLogSystem using category '"
075: + categoryname + "'");
076:
077: return;
078: }
079:
080: /*
081: * if not, use the file...
082: */
083:
084: String logfile = rsvc.getString(RuntimeConstants.RUNTIME_LOG);
085:
086: /*
087: * now init. If we can't, panic!
088: */
089: try {
090: internalInit(logfile);
091:
092: logVelocityMessage(0,
093: "SimpleLog4JLogSystem initialized using logfile '"
094: + logfile + "'");
095: } catch (Exception e) {
096: System.err
097: .println("PANIC : error configuring SimpleLog4JLogSystem : "
098: + e);
099: }
100: }
101:
102: /**
103: * initializes the log system using the logfile argument
104: */
105: private void internalInit(String logfile) throws Exception {
106: /*
107: * do it by our classname to avoid conflicting with anything else
108: * that might be used...
109: */
110:
111: logger = Category.getInstance(this .getClass().getName());
112: logger.setAdditivity(false);
113:
114: /*
115: * Priority is set for DEBUG becouse this implementation checks
116: * log level.
117: */
118: logger.setLevel(Level.DEBUG);
119:
120: appender = new RollingFileAppender(new PatternLayout(
121: "%d - %m%n"), logfile, true);
122:
123: appender.setMaxBackupIndex(1);
124:
125: appender.setMaximumFileSize(100000);
126:
127: logger.addAppender(appender);
128: }
129:
130: /**
131: * logs messages
132: *
133: * @param level severity level
134: * @param message complete error message
135: */
136: public void logVelocityMessage(int level, String message) {
137: switch (level) {
138: case LogSystem.WARN_ID:
139: logger.warn(message);
140: break;
141: case LogSystem.INFO_ID:
142: logger.info(message);
143: break;
144: case LogSystem.DEBUG_ID:
145: logger.debug(message);
146: break;
147: case LogSystem.ERROR_ID:
148: logger.error(message);
149: break;
150: default:
151: logger.debug(message);
152: break;
153: }
154: }
155:
156: /**
157: * Also do a shutdown if the object is destroy()'d.
158: * @throws Throwable
159: */
160: protected void finalize() throws Throwable {
161: shutdown();
162: }
163:
164: /** Close all destinations*/
165: public void shutdown() {
166: if (appender != null) {
167: logger.removeAppender(appender);
168: appender.close();
169: appender = null;
170: }
171: }
172: }
|