View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.util.log;
20  
21  import java.util.logging.Level;
22  
23  /**
24   * <p>
25   * Implementation of Jetty {@link Logger} based on {@link java.util.logging.Logger}.
26   * </p>
27   *
28   * <p>
29   * You can also set the logger level using <a href="http://java.sun.com/j2se/1.5.0/docs/guide/logging/overview.html">
30   * standard java.util.logging configuration</a>.
31   * </p>
32   */
33  public class JavaUtilLog extends AbstractLogger
34  {
35      private Level configuredLevel;
36      private java.util.logging.Logger _logger;
37  
38      public JavaUtilLog()
39      {
40          this("org.eclipse.jetty.util.log");
41      }
42  
43      public JavaUtilLog(String name)
44      {
45          _logger = java.util.logging.Logger.getLogger(name);
46          if (Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.DEBUG", "false")))
47          {
48              _logger.setLevel(Level.FINE);
49          }
50          configuredLevel = _logger.getLevel();
51      }
52  
53      public String getName()
54      {
55          return _logger.getName();
56      }
57  
58      public void warn(String msg, Object... args)
59      {
60          if (_logger.isLoggable(Level.WARNING))
61              _logger.log(Level.WARNING,format(msg,args));
62      }
63  
64      public void warn(Throwable thrown)
65      {
66          warn("", thrown);
67      }
68  
69      public void warn(String msg, Throwable thrown)
70      {
71          _logger.log(Level.WARNING, msg, thrown);
72      }
73  
74      public void info(String msg, Object... args)
75      {
76          if (_logger.isLoggable(Level.INFO))
77              _logger.log(Level.INFO, format(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.log(Level.INFO, msg, thrown);
88      }
89  
90      public boolean isDebugEnabled()
91      {
92          return _logger.isLoggable(Level.FINE);
93      }
94  
95      public void setDebugEnabled(boolean enabled)
96      {
97          if (enabled)
98          {
99              configuredLevel = _logger.getLevel();
100             _logger.setLevel(Level.FINE);
101         }
102         else
103         {
104             _logger.setLevel(configuredLevel);
105         }
106     }
107 
108     public void debug(String msg, Object... args)
109     {
110         if (_logger.isLoggable(Level.FINE))
111             _logger.log(Level.FINE,format(msg, args));
112     }
113 
114     public void debug(Throwable thrown)
115     {
116         debug("", thrown);
117     }
118 
119     public void debug(String msg, Throwable thrown)
120     {
121         _logger.log(Level.FINE, msg, thrown);
122     }
123 
124     /**
125      * Create a Child Logger of this Logger.
126      */
127     protected Logger newLogger(String fullname)
128     {
129         return new JavaUtilLog(fullname);
130     }
131 
132     public void ignore(Throwable ignored)
133     {
134         if (Log.isIgnored())
135         {
136             warn(Log.IGNORED, ignored);
137         }
138     }
139 
140     private String format(String msg, Object... args)
141     {
142         msg = String.valueOf(msg); // Avoids NPE
143         String braces = "{}";
144         StringBuilder builder = new StringBuilder();
145         int start = 0;
146         for (Object arg : args)
147         {
148             int bracesIndex = msg.indexOf(braces, start);
149             if (bracesIndex < 0)
150             {
151                 builder.append(msg.substring(start));
152                 builder.append(" ");
153                 builder.append(arg);
154                 start = msg.length();
155             }
156             else
157             {
158                 builder.append(msg.substring(start, bracesIndex));
159                 builder.append(String.valueOf(arg));
160                 start = bracesIndex + braces.length();
161             }
162         }
163         builder.append(msg.substring(start));
164         return builder.toString();
165     }
166 }