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              _logger.setLevel(Level.FINE);
50      }
51  
52      public String getName()
53      {
54          return _logger.getName();
55      }
56  
57      public void warn(String msg, Object... args)
58      {
59          _logger.log(Level.WARNING, format(msg, args));
60      }
61  
62      public void warn(Throwable thrown)
63      {
64          warn("", thrown);
65      }
66  
67      public void warn(String msg, Throwable thrown)
68      {
69          _logger.log(Level.WARNING, msg, thrown);
70      }
71  
72      public void info(String msg, Object... args)
73      {
74          _logger.log(Level.INFO, format(msg, args));
75      }
76  
77      public void info(Throwable thrown)
78      {
79          info("", thrown);
80      }
81  
82      public void info(String msg, Throwable thrown)
83      {
84          _logger.log(Level.INFO, msg, thrown);
85      }
86  
87      public boolean isDebugEnabled()
88      {
89          return _logger.isLoggable(Level.FINE);
90      }
91  
92      public void setDebugEnabled(boolean enabled)
93      {
94          _logger.setLevel(Level.FINE);
95      }
96  
97      public void debug(String msg, Object... args)
98      {
99          _logger.log(Level.FINE, format(msg, args));
100     }
101 
102     public void debug(Throwable thrown)
103     {
104         debug("", thrown);
105     }
106 
107     public void debug(String msg, Throwable thrown)
108     {
109         _logger.log(Level.FINE, msg, thrown);
110     }
111 
112     public Logger getLogger(String name)
113     {
114         return new JavaUtilLog(name);
115     }
116 
117     public void ignore(Throwable ignored)
118     {
119         if (Log.isIgnored())
120         {
121             warn(Log.IGNORED, ignored);
122         }
123     }
124 
125     private String format(String msg, Object... args)
126     {
127         msg = String.valueOf(msg); // Avoids NPE
128         String braces = "{}";
129         StringBuilder builder = new StringBuilder();
130         int start = 0;
131         for (Object arg : args)
132         {
133             int bracesIndex = msg.indexOf(braces, start);
134             if (bracesIndex < 0)
135             {
136                 builder.append(msg.substring(start));
137                 builder.append(" ");
138                 builder.append(arg);
139                 start = msg.length();
140             }
141             else
142             {
143                 builder.append(msg.substring(start, bracesIndex));
144                 builder.append(String.valueOf(arg));
145                 start = bracesIndex + braces.length();
146             }
147         }
148         builder.append(msg.substring(start));
149         return builder.toString();
150     }
151 }