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 java.util.logging.Level;
23 import java.util.logging.Logger;
24
25 import org.apache.velocity.runtime.RuntimeServices;
26
27 /**
28 * Implementation of a simple java.util.logging LogChute.
29 *
30 * @author <a href="mailto:nbubna@apache.org>Nathan Bubna</a>
31 * @version $Id: JdkLogChute.java 522566 2007-03-26 16:42:00Z nbubna $
32 * @since Velocity 1.5
33 */
34 public class JdkLogChute implements LogChute
35 {
36 /** Property key for specifying the name for the logger instance */
37 public static final String RUNTIME_LOG_JDK_LOGGER =
38 "runtime.log.logsystem.jdk.logger";
39
40 public static final String RUNTIME_LOG_JDK_LOGGER_LEVEL =
41 "runtime.log.logsystem.jdk.logger.level";
42
43 /** Default name for the JDK logger instance */
44 public static final String DEFAULT_LOG_NAME = "org.apache.velocity";
45
46 /**
47 *
48 */
49 protected Logger logger = null;
50
51 /**
52 * @see org.apache.velocity.runtime.log.LogChute#init(org.apache.velocity.runtime.RuntimeServices)
53 */
54 public void init(RuntimeServices rs)
55 {
56 String name = (String)rs.getProperty(RUNTIME_LOG_JDK_LOGGER);
57 if (name == null)
58 {
59 name = DEFAULT_LOG_NAME;
60 }
61 logger = Logger.getLogger(name);
62
63 /* get and set specified level for this logger, default to WARN */
64 String lvl = rs.getString(RUNTIME_LOG_JDK_LOGGER_LEVEL, "WARN");
65 Level level = Level.parse(lvl);
66 logger.setLevel(level);
67
68 log(LogChute.DEBUG_ID, "JdkLogChute will use logger '"+name+'\''+" at level '"+level+'\'');
69 }
70
71 /**
72 * Returns the java.util.logging.Level that matches
73 * to the specified LogChute level.
74 * @param level
75 * @return The current log level of the JDK Logger.
76 */
77 protected Level getJdkLevel(int level)
78 {
79 switch (level)
80 {
81 case LogChute.WARN_ID:
82 return Level.WARNING;
83 case LogChute.INFO_ID:
84 return Level.INFO;
85 case LogChute.DEBUG_ID:
86 return Level.FINE;
87 case LogChute.TRACE_ID:
88 return Level.FINEST;
89 case LogChute.ERROR_ID:
90 return Level.SEVERE;
91 default:
92 return Level.FINER;
93 }
94 }
95
96 /**
97 * Logs messages
98 *
99 * @param level severity level
100 * @param message complete error message
101 */
102 public void log(int level, String message)
103 {
104 log(level, message, null);
105 }
106
107 /**
108 * Send a log message from Velocity along with an exception or error
109 * @param level
110 * @param message
111 * @param t
112 */
113 public void log(int level, String message, Throwable t)
114 {
115 Level jdkLevel = getJdkLevel(level);
116 if (t == null)
117 {
118 logger.log(jdkLevel, message);
119 }
120 else
121 {
122 logger.log(jdkLevel, message, t);
123 }
124 }
125
126 /**
127 * @see org.apache.velocity.runtime.log.LogChute#isLevelEnabled(int)
128 */
129 public boolean isLevelEnabled(int level)
130 {
131 Level jdkLevel = getJdkLevel(level);
132 return logger.isLoggable(jdkLevel);
133 }
134
135 }