]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/util/log/JavaUtilLog.java
Merge "Update notes about password security"
[gigi.git] / lib / jetty / org / eclipse / jetty / util / log / JavaUtilLog.java
1 //
2 //  ========================================================================
3 //  Copyright (c) 1995-2016 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(String msg, long arg)
115     {
116         if (_logger.isLoggable(Level.FINE))
117             _logger.log(Level.FINE,format(msg, arg));
118     }
119
120     public void debug(Throwable thrown)
121     {
122         debug("", thrown);
123     }
124
125     public void debug(String msg, Throwable thrown)
126     {
127         _logger.log(Level.FINE, msg, thrown);
128     }
129
130     /**
131      * Create a Child Logger of this Logger.
132      */
133     protected Logger newLogger(String fullname)
134     {
135         return new JavaUtilLog(fullname);
136     }
137
138     public void ignore(Throwable ignored)
139     {
140         if (Log.isIgnored())
141         {
142             warn(Log.IGNORED, ignored);
143         }
144     }
145
146     private String format(String msg, Object... args)
147     {
148         msg = String.valueOf(msg); // Avoids NPE
149         String braces = "{}";
150         StringBuilder builder = new StringBuilder();
151         int start = 0;
152         for (Object arg : args)
153         {
154             int bracesIndex = msg.indexOf(braces, start);
155             if (bracesIndex < 0)
156             {
157                 builder.append(msg.substring(start));
158                 builder.append(" ");
159                 builder.append(arg);
160                 start = msg.length();
161             }
162             else
163             {
164                 builder.append(msg.substring(start, bracesIndex));
165                 builder.append(String.valueOf(arg));
166                 start = bracesIndex + braces.length();
167             }
168         }
169         builder.append(msg.substring(start));
170         return builder.toString();
171     }
172 }