View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2014 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          _logger.log(Level.WARNING, format(msg, args));
61      }
62  
63      public void warn(Throwable thrown)
64      {
65          warn("", thrown);
66      }
67  
68      public void warn(String msg, Throwable thrown)
69      {
70          _logger.log(Level.WARNING, msg, thrown);
71      }
72  
73      public void info(String msg, Object... args)
74      {
75          _logger.log(Level.INFO, format(msg, args));
76      }
77  
78      public void info(Throwable thrown)
79      {
80          info("", thrown);
81      }
82  
83      public void info(String msg, Throwable thrown)
84      {
85          _logger.log(Level.INFO, msg, thrown);
86      }
87  
88      public boolean isDebugEnabled()
89      {
90          return _logger.isLoggable(Level.FINE);
91      }
92  
93      public void setDebugEnabled(boolean enabled)
94      {
95          if (enabled)
96          {
97              configuredLevel = _logger.getLevel();
98              _logger.setLevel(Level.FINE);
99          }
100         else
101         {
102             _logger.setLevel(configuredLevel);
103         }
104     }
105 
106     public void debug(String msg, Object... args)
107     {
108         _logger.log(Level.FINE, format(msg, args));
109     }
110 
111     public void debug(Throwable thrown)
112     {
113         debug("", thrown);
114     }
115 
116     public void debug(String msg, Throwable thrown)
117     {
118         _logger.log(Level.FINE, msg, thrown);
119     }
120 
121     /**
122      * Create a Child Logger of this Logger.
123      */
124     protected Logger newLogger(String fullname)
125     {
126         return new JavaUtilLog(fullname);
127     }
128 
129     public void ignore(Throwable ignored)
130     {
131         if (Log.isIgnored())
132         {
133             warn(Log.IGNORED, ignored);
134         }
135     }
136 
137     private String format(String msg, Object... args)
138     {
139         msg = String.valueOf(msg); // Avoids NPE
140         String braces = "{}";
141         StringBuilder builder = new StringBuilder();
142         int start = 0;
143         for (Object arg : args)
144         {
145             int bracesIndex = msg.indexOf(braces, start);
146             if (bracesIndex < 0)
147             {
148                 builder.append(msg.substring(start));
149                 builder.append(" ");
150                 builder.append(arg);
151                 start = msg.length();
152             }
153             else
154             {
155                 builder.append(msg.substring(start, bracesIndex));
156                 builder.append(String.valueOf(arg));
157                 start = bracesIndex + braces.length();
158             }
159         }
160         builder.append(msg.substring(start));
161         return builder.toString();
162     }
163 }