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.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util.log;
21 import java.util.logging.Level;
25 * Implementation of Jetty {@link Logger} based on {@link java.util.logging.Logger}.
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>.
33 public class JavaUtilLog extends AbstractLogger
35 private Level configuredLevel;
36 private java.util.logging.Logger _logger;
40 this("org.eclipse.jetty.util.log");
43 public JavaUtilLog(String name)
45 _logger = java.util.logging.Logger.getLogger(name);
46 if (Boolean.parseBoolean(Log.__props.getProperty("org.eclipse.jetty.util.log.DEBUG", "false")))
48 _logger.setLevel(Level.FINE);
50 configuredLevel = _logger.getLevel();
53 public String getName()
55 return _logger.getName();
58 public void warn(String msg, Object... args)
60 if (_logger.isLoggable(Level.WARNING))
61 _logger.log(Level.WARNING,format(msg,args));
64 public void warn(Throwable thrown)
69 public void warn(String msg, Throwable thrown)
71 _logger.log(Level.WARNING, msg, thrown);
74 public void info(String msg, Object... args)
76 if (_logger.isLoggable(Level.INFO))
77 _logger.log(Level.INFO, format(msg, args));
80 public void info(Throwable thrown)
85 public void info(String msg, Throwable thrown)
87 _logger.log(Level.INFO, msg, thrown);
90 public boolean isDebugEnabled()
92 return _logger.isLoggable(Level.FINE);
95 public void setDebugEnabled(boolean enabled)
99 configuredLevel = _logger.getLevel();
100 _logger.setLevel(Level.FINE);
104 _logger.setLevel(configuredLevel);
108 public void debug(String msg, Object... args)
110 if (_logger.isLoggable(Level.FINE))
111 _logger.log(Level.FINE,format(msg, args));
114 public void debug(String msg, long arg)
116 if (_logger.isLoggable(Level.FINE))
117 _logger.log(Level.FINE,format(msg, arg));
120 public void debug(Throwable thrown)
125 public void debug(String msg, Throwable thrown)
127 _logger.log(Level.FINE, msg, thrown);
131 * Create a Child Logger of this Logger.
133 protected Logger newLogger(String fullname)
135 return new JavaUtilLog(fullname);
138 public void ignore(Throwable ignored)
142 warn(Log.IGNORED, ignored);
146 private String format(String msg, Object... args)
148 msg = String.valueOf(msg); // Avoids NPE
149 String braces = "{}";
150 StringBuilder builder = new StringBuilder();
152 for (Object arg : args)
154 int bracesIndex = msg.indexOf(braces, start);
157 builder.append(msg.substring(start));
160 start = msg.length();
164 builder.append(msg.substring(start, bracesIndex));
165 builder.append(String.valueOf(arg));
166 start = bracesIndex + braces.length();
169 builder.append(msg.substring(start));
170 return builder.toString();