View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.util.log;
15  
16  
17  
18  /**
19   * Slf4jLog Logger
20   */
21  public class Slf4jLog implements Logger
22  {
23      private final org.slf4j.Logger _logger;
24  
25      public Slf4jLog() throws Exception
26      {
27          this("org.eclipse.jetty.util.log");
28  
29      }
30  
31      public Slf4jLog(String name)
32      {
33          // This checks to make sure that an slf4j implementation is present.
34          // It is needed because slf4j-api 1.6.x defaults to using NOPLogger.
35          try
36          {
37              Class.forName("org.slf4j.impl.StaticLoggerBinder");
38          }
39          catch (ClassNotFoundException ex)
40          {
41              throw new NoClassDefFoundError("org.slf4j.impl.StaticLoggerBinder");
42          }
43  
44          org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger( name );
45          if (logger instanceof org.slf4j.spi.LocationAwareLogger)
46          {
47              _logger = new JettyAwareLogger((org.slf4j.spi.LocationAwareLogger)logger);
48          }
49          else
50          {
51              _logger = logger;
52          }
53      }
54  
55      public String getName()
56      {
57          return _logger.getName();
58      }
59  
60      public void warn(String msg, Object... args)
61      {
62          _logger.warn(msg, args);
63      }
64  
65      public void warn(Throwable thrown)
66      {
67          warn("", thrown);
68      }
69  
70      public void warn(String msg, Throwable thrown)
71      {
72          _logger.warn(msg, thrown);
73      }
74  
75      public void info(String msg, Object... args)
76      {
77          _logger.info(msg, args);
78      }
79  
80      public void info(Throwable thrown)
81      {
82          info("", thrown);
83      }
84  
85      public void info(String msg, Throwable thrown)
86      {
87          _logger.info(msg, thrown);
88      }
89  
90      public void debug(String msg, Object... args)
91      {
92          _logger.debug(msg, args);
93      }
94  
95      public void debug(Throwable thrown)
96      {
97          debug("", thrown);
98      }
99  
100     public void debug(String msg, Throwable thrown)
101     {
102         _logger.debug(msg, thrown);
103     }
104 
105     public boolean isDebugEnabled()
106     {
107         return _logger.isDebugEnabled();
108     }
109 
110     public void setDebugEnabled(boolean enabled)
111     {
112         warn("setDebugEnabled not implemented",null,null);
113     }
114 
115     public Logger getLogger(String name)
116     {
117         return new Slf4jLog(name);
118     }
119 
120     public void ignore(Throwable ignored)
121     {
122         if (Log.isIgnored())
123         {
124             warn(Log.IGNORED, ignored);
125         }
126     }
127 
128     @Override
129     public String toString()
130     {
131         return _logger.toString();
132     }
133 }