From d6633e01e6a888461a6f3a4cc2bac1d4c11a7f13 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 27 Jul 2009 23:39:16 +0000 Subject: [PATCH] Adding implementation of a Jetty Logger that routes to a java.util.logging.Logger git-svn-id: svn+ssh://dev.eclipse.org/svnroot/rt/org.eclipse.jetty/jetty/trunk@594 7e9141cc-0065-0410-87d8-b60c137991c4 --- .../eclipse/jetty/util/log/JavaUtilLog.java | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 jetty-util/src/main/java/org/eclipse/jetty/util/log/JavaUtilLog.java diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/log/JavaUtilLog.java b/jetty-util/src/main/java/org/eclipse/jetty/util/log/JavaUtilLog.java new file mode 100644 index 00000000000..6a24e945dd0 --- /dev/null +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/log/JavaUtilLog.java @@ -0,0 +1,104 @@ +package org.eclipse.jetty.util.log; + +import java.util.logging.Level; + +/** + *

+ * Implementation of Jetty {@link Logger} based on {@link java.util.logging.Logger}. + *

+ * + *

+ * Honors the standard jetty system property "org.eclipse.jetty.util.log.DEBUG" to set logger into debug + * mode (defaults to false, set to "true" to enable) + *

+ * + *

+ * You can also set the logger level using + * standard java.util.logging configuration against the name "org.eclipse.jetty.util.log". + *

+ */ +public class JavaUtilLog implements Logger +{ + private java.util.logging.Logger logger; + + public JavaUtilLog() + { + this("org.eclipse.jetty.util.log"); + } + + public JavaUtilLog(String name) + { + logger = java.util.logging.Logger.getLogger(name); + if (Boolean.getBoolean("org.eclipse.jetty.util.log.DEBUG")) + { + logger.setLevel(Level.FINE); + } + } + + public void debug(String msg) + { + logger.log(Level.FINE,msg); + } + + public void debug(String msg, Throwable th) + { + logger.log(Level.FINE,msg,th); + } + + public void debug(String msg, Object arg0, Object arg1) + { + logger.log(Level.FINE,format(msg,arg0,arg1)); + } + + public Logger getLogger(String name) + { + return new JavaUtilLog(name); + } + + public void info(String msg) + { + logger.log(Level.INFO,msg); + } + + public void info(String msg, Object arg0, Object arg1) + { + logger.log(Level.INFO,format(msg,arg0,arg1)); + } + + public boolean isDebugEnabled() + { + return logger.isLoggable(Level.FINE); + } + + public void setDebugEnabled(boolean enabled) + { + logger.setLevel(Level.FINE); + } + + public void warn(String msg) + { + logger.log(Level.WARNING,msg); + } + + public void warn(String msg, Object arg0, Object arg1) + { + logger.log(Level.WARNING,format(msg,arg0,arg1)); + } + + public void warn(String msg, Throwable th) + { + logger.log(Level.WARNING,msg,th); + } + + private String format(String msg, Object arg0, Object arg1) + { + int i0 = msg.indexOf("{}"); + int i1 = i0 < 0?-1:msg.indexOf("{}",i0 + 2); + + if (arg1 != null && i1 >= 0) + msg = msg.substring(0,i1) + arg1 + msg.substring(i1 + 2); + if (arg0 != null && i0 >= 0) + msg = msg.substring(0,i0) + arg0 + msg.substring(i0 + 2); + return msg; + } +}