View Javadoc

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