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 java.util.logging.Level;
023: import java.util.logging.Logger;
024:
025: import org.apache.velocity.runtime.RuntimeServices;
026:
027: /**
028: * Implementation of a simple java.util.logging LogChute.
029: *
030: * @author <a href="mailto:nbubna@apache.org>Nathan Bubna</a>
031: * @version $Id: JdkLogChute.java 463298 2006-10-12 16:10:32Z henning $
032: * @since Velocity 1.5
033: */
034: public class JdkLogChute implements LogChute {
035: /** Property key for specifying the name for the logger instance */
036: public static final String RUNTIME_LOG_JDK_LOGGER = "runtime.log.logsystem.jdk.logger";
037:
038: /** Default name for the JDK logger instance */
039: public static final String DEFAULT_LOG_NAME = "org.apache.velocity";
040:
041: /**
042: *
043: */
044: protected Logger logger = null;
045:
046: /**
047: * @see org.apache.velocity.runtime.log.LogChute#init(org.apache.velocity.runtime.RuntimeServices)
048: */
049: public void init(RuntimeServices rs) {
050: String name = (String) rs.getProperty(RUNTIME_LOG_JDK_LOGGER);
051: if (name == null) {
052: name = DEFAULT_LOG_NAME;
053: }
054: logger = Logger.getLogger(name);
055: log(LogChute.DEBUG_ID, "JdkLogChute will use logger '" + name
056: + '\'');
057: }
058:
059: /**
060: * Returns the java.util.logging.Level that matches
061: * to the specified LogChute level.
062: * @param level
063: * @return The current log level of the JDK Logger.
064: */
065: protected Level getJdkLevel(int level) {
066: switch (level) {
067: case LogChute.WARN_ID:
068: return Level.WARNING;
069: case LogChute.INFO_ID:
070: return Level.INFO;
071: case LogChute.DEBUG_ID:
072: return Level.FINE;
073: case LogChute.TRACE_ID:
074: return Level.FINEST;
075: case LogChute.ERROR_ID:
076: return Level.SEVERE;
077: default:
078: return Level.FINER;
079: }
080: }
081:
082: /**
083: * Logs messages
084: *
085: * @param level severity level
086: * @param message complete error message
087: */
088: public void log(int level, String message) {
089: log(level, message, null);
090: }
091:
092: /**
093: * Send a log message from Velocity along with an exception or error
094: * @param level
095: * @param message
096: * @param t
097: */
098: public void log(int level, String message, Throwable t) {
099: Level jdkLevel = getJdkLevel(level);
100: if (t == null) {
101: logger.log(jdkLevel, message);
102: } else {
103: logger.log(jdkLevel, message, t);
104: }
105: }
106:
107: /**
108: * @see org.apache.velocity.runtime.log.LogChute#isLevelEnabled(int)
109: */
110: public boolean isLevelEnabled(int level) {
111: Level jdkLevel = getJdkLevel(level);
112: return logger.isLoggable(jdkLevel);
113: }
114:
115: }
|