View Javadoc

1   /*
2    * Copyright 2011 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.util;
18  
19  import org.apache.velocity.runtime.RuntimeServices;
20  import org.apache.velocity.runtime.log.LogChute;
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  /**
25   * Redirects Velocity's LogChute messages to SLF4J.
26   *
27   * <p>To use, first set up SLF4J, then tell Velocity to use
28   * this class for logging by adding the following to your velocity.properties:
29   *
30   * <code>
31   * runtime.log.logsystem.class = edu.internet2.middleware.shibboleth.common.util.Slf4jLogChute
32   * </code>
33   * </p>
34   *
35   * <p>You may also set this property to specify what log/name Velocity's
36   * messages should be logged to (example below is default).
37   * <code>
38   * runtime.log.logsystem.slf4j.name = org.apache.velocity
39   * </code>
40   * </p>
41   */
42  public class Slf4JLogChute implements LogChute {
43  
44      /** Property key for specifying the name for the log instance */
45      public static final String LOGCHUTE_SLF4J_NAME = "runtime.log.logsystem.slf4j.name";
46      
47      /** Default name for the commons-logging instance */
48      public static final String DEFAULT_LOG_NAME = "org.apache.velocity";
49      
50      /** The Slf4J Logger instance. */
51      protected Logger log;
52      
53      /** {@inheritDoc} */
54      public void init(RuntimeServices rs) throws Exception {
55          String name = (String) rs.getProperty(LOGCHUTE_SLF4J_NAME);
56          if (name == null) {
57              name = DEFAULT_LOG_NAME;
58          }
59          log = LoggerFactory.getLogger(name);
60          log(LogChute.DEBUG_ID, "Slf4JLogChute name is '" + name + "'");
61      }
62  
63      /** {@inheritDoc} */
64      public boolean isLevelEnabled(int level) {
65          switch (level) {
66              case LogChute.DEBUG_ID:
67                  return log.isDebugEnabled();
68              case LogChute.INFO_ID:
69                  return log.isInfoEnabled();
70              case LogChute.TRACE_ID:
71                  return log.isTraceEnabled();
72              case LogChute.WARN_ID:
73                  return log.isWarnEnabled();
74              case LogChute.ERROR_ID:
75                  return log.isErrorEnabled();
76              default:
77                  return true;
78          }
79      }
80  
81      /** {@inheritDoc} */
82      public void log(int level, String message) {
83          switch (level) {
84              case LogChute.ERROR_ID:
85                  log.error(message);
86              case LogChute.WARN_ID:
87                  log.warn(message);
88                  break;
89              case LogChute.INFO_ID:
90                  log.info(message);
91                  break;
92              case LogChute.TRACE_ID:
93                  log.trace(message);
94                  break;
95              case LogChute.DEBUG_ID:
96              default:
97                  log.debug(message);
98          }
99      }
100 
101     /** {@inheritDoc} */
102     public void log(int level, String message, Throwable t) {
103         switch (level) {
104             case LogChute.ERROR_ID:
105                 log.error(message, t);
106             case LogChute.WARN_ID:
107                 log.warn(message, t);
108                 break;
109             case LogChute.INFO_ID:
110                 log.info(message, t);
111                 break;
112             case LogChute.TRACE_ID:
113                 log.trace(message, t);
114                 break;
115             case LogChute.DEBUG_ID:
116             default:
117                 log.debug(message, t);
118         }
119     }
120 
121 }