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   * You can also set the logger level using <a href="http://java.sun.com/j2se/1.5.0/docs/guide/logging/overview.html">
28   * standard java.util.logging configuration</a>.
29   * </p>
30   */
31  public class JavaUtilLog implements Logger
32  {
33      private Level configuredLevel;
34      private java.util.logging.Logger _logger;
35  
36      public JavaUtilLog()
37      {
38          this("org.eclipse.jetty.util.log");
39      }
40  
41      public JavaUtilLog(String name)
42      {
43          _logger = java.util.logging.Logger.getLogger(name);
44          if (Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.DEBUG", "false")))
45          {
46              _logger.setLevel(Level.FINE);
47          }
48          configuredLevel = _logger.getLevel();
49      }
50  
51      public String getName()
52      {
53          return _logger.getName();
54      }
55  
56      public void warn(String msg, Object... args)
57      {
58          _logger.log(Level.WARNING, format(msg, args));
59      }
60  
61      public void warn(Throwable thrown)
62      {
63          warn("", thrown);
64      }
65  
66      public void warn(String msg, Throwable thrown)
67      {
68          _logger.log(Level.WARNING, msg, thrown);
69      }
70  
71      public void info(String msg, Object... args)
72      {
73          _logger.log(Level.INFO, format(msg, args));
74      }
75  
76      public void info(Throwable thrown)
77      {
78          info("", thrown);
79      }
80  
81      public void info(String msg, Throwable thrown)
82      {
83          _logger.log(Level.INFO, msg, thrown);
84      }
85  
86      public boolean isDebugEnabled()
87      {
88          return _logger.isLoggable(Level.FINE);
89      }
90  
91      public void setDebugEnabled(boolean enabled)
92      {
93          if (enabled)
94          {
95              configuredLevel = _logger.getLevel();
96              _logger.setLevel(Level.FINE);
97          }
98          else
99          {
100             _logger.setLevel(configuredLevel);
101         }
102     }
103 
104     public void debug(String msg, Object... args)
105     {
106         _logger.log(Level.FINE, format(msg, args));
107     }
108 
109     public void debug(Throwable thrown)
110     {
111         debug("", thrown);
112     }
113 
114     public void debug(String msg, Throwable thrown)
115     {
116         _logger.log(Level.FINE, msg, thrown);
117     }
118 
119     public Logger getLogger(String name)
120     {
121         return new JavaUtilLog(name);
122     }
123 
124     public void ignore(Throwable ignored)
125     {
126         if (Log.isIgnored())
127         {
128             warn(Log.IGNORED, ignored);
129         }
130     }
131 
132     private String format(String msg, Object... args)
133     {
134         msg = String.valueOf(msg); // Avoids NPE
135         String braces = "{}";
136         StringBuilder builder = new StringBuilder();
137         int start = 0;
138         for (Object arg : args)
139         {
140             int bracesIndex = msg.indexOf(braces, start);
141             if (bracesIndex < 0)
142             {
143                 builder.append(msg.substring(start));
144                 builder.append(" ");
145                 builder.append(arg);
146                 start = msg.length();
147             }
148             else
149             {
150                 builder.append(msg.substring(start, bracesIndex));
151                 builder.append(String.valueOf(arg));
152                 start = bracesIndex + braces.length();
153             }
154         }
155         builder.append(msg.substring(start));
156         return builder.toString();
157     }
158 }