View Javadoc

1   // ========================================================================
2   // Copyright (c) Webtide LLC
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   //
8   // The Eclipse Public License is available at
9   // http://www.eclipse.org/legal/epl-v10.html
10  //
11  // The Apache License v2.0 is available at
12  // http://www.apache.org/licenses/LICENSE-2.0.txt
13  //
14  // You may elect to redistribute this code under either of these licenses.
15  // ========================================================================
16  
17  package org.eclipse.jetty.util.log;
18  
19  import java.util.logging.Level;
20  
21  /**
22   * <p>
23   * Implementation of Jetty {@link Logger} based on {@link java.util.logging.Logger}.
24   * </p>
25   * 
26   * <p>
27   * Honors the standard jetty system property <code>"org.eclipse.jetty.util.log.DEBUG"</code> to set logger into debug
28   * mode (defaults to false, set to "true" to enable)
29   * </p>
30   * 
31   * <p>
32   * You can also set the logger level using <a href="http://java.sun.com/j2se/1.5.0/docs/guide/logging/overview.html">
33   * standard java.util.logging configuration</a> against the name <code>"org.eclipse.jetty.util.log"</code>.
34   * </p>
35   */
36  public class JavaUtilLog implements Logger
37  {
38      private java.util.logging.Logger _logger;
39  
40      public JavaUtilLog()
41      {
42          this("org.eclipse.jetty.util.log");
43      }
44  
45      public JavaUtilLog(String name)
46      {
47          _logger = java.util.logging.Logger.getLogger(name);
48          if (Boolean.getBoolean("org.eclipse.jetty.util.log.DEBUG"))
49          {
50              _logger.setLevel(Level.FINE);
51          }
52      }
53      
54      public String getName()
55      {
56          return _logger.getName();
57      }
58  
59      public void debug(String msg)
60      {
61          _logger.log(Level.FINE,msg);
62      }
63  
64      public void debug(String msg, Throwable th)
65      {
66          _logger.log(Level.FINE,msg,th);
67      }
68  
69      public void debug(String msg, Object arg0, Object arg1)
70      {
71          _logger.log(Level.FINE,format(msg,arg0,arg1));
72      }
73  
74      public Logger getLogger(String name)
75      {
76          return new JavaUtilLog(name);
77      }
78  
79      public void info(String msg)
80      {
81          _logger.log(Level.INFO,msg);
82      }
83  
84      public void info(String msg, Object arg0, Object arg1)
85      {
86          _logger.log(Level.INFO,format(msg,arg0,arg1));
87      }
88  
89      public boolean isDebugEnabled()
90      {
91          return _logger.isLoggable(Level.FINE);
92      }
93  
94      public void setDebugEnabled(boolean enabled)
95      {
96          _logger.setLevel(Level.FINE);
97      }
98  
99      public void warn(String msg)
100     {
101         _logger.log(Level.WARNING,msg);
102     }
103 
104     public void warn(String msg, Object arg0, Object arg1)
105     {
106         _logger.log(Level.WARNING,format(msg,arg0,arg1));
107     }
108 
109     public void warn(String msg, Throwable th)
110     {
111         _logger.log(Level.WARNING,msg,th);
112     }
113 
114     private String format(String msg, Object arg0, Object arg1)
115     {
116         int i0 = msg.indexOf("{}");
117         int i1 = i0 < 0?-1:msg.indexOf("{}",i0 + 2);
118 
119         if (arg1 != null && i1 >= 0)
120             msg = msg.substring(0,i1) + arg1 + msg.substring(i1 + 2);
121         if (arg0 != null && i0 >= 0)
122             msg = msg.substring(0,i0) + arg0 + msg.substring(i0 + 2);
123         return msg;
124     }
125 }