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 703541 2008-10-10 18:09:42Z nbubna $
32 * @since 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, */
64 String lvl = rs.getString(RUNTIME_LOG_JDK_LOGGER_LEVEL);
65 if (lvl != null)
66 {
67 Level level = Level.parse(lvl);
68 logger.setLevel(level);
69 log(LogChute.DEBUG_ID, "JdkLogChute will use logger '"
70 +name+'\''+" at level '"+level+'\'');
71 }
72
73 }
74
75 /**
76 * Returns the java.util.logging.Level that matches
77 * to the specified LogChute level.
78 * @param level
79 * @return The current log level of the JDK Logger.
80 */
81 protected Level getJdkLevel(int level)
82 {
83 switch (level)
84 {
85 case LogChute.WARN_ID:
86 return Level.WARNING;
87 case LogChute.INFO_ID:
88 return Level.INFO;
89 case LogChute.DEBUG_ID:
90 return Level.FINE;
91 case LogChute.TRACE_ID:
92 return Level.FINEST;
93 case LogChute.ERROR_ID:
94 return Level.SEVERE;
95 default:
96 return Level.FINER;
97 }
98 }
99
100 /**
101 * Logs messages
102 *
103 * @param level severity level
104 * @param message complete error message
105 */
106 public void log(int level, String message)
107 {
108 log(level, message, null);
109 }
110
111 /**
112 * Send a log message from Velocity along with an exception or error
113 * @param level
114 * @param message
115 * @param t
116 */
117 public void log(int level, String message, Throwable t)
118 {
119 Level jdkLevel = getJdkLevel(level);
120 if (t == null)
121 {
122 logger.log(jdkLevel, message);
123 }
124 else
125 {
126 logger.log(jdkLevel, message, t);
127 }
128 }
129
130 /**
131 * @see org.apache.velocity.runtime.log.LogChute#isLevelEnabled(int)
132 */
133 public boolean isLevelEnabled(int level)
134 {
135 Level jdkLevel = getJdkLevel(level);
136 return logger.isLoggable(jdkLevel);
137 }
138
139 }