diff --git a/src/main/java/com/samskivert/util/JDK14Logger.java b/src/main/java/com/samskivert/util/JDK14Logger.java index cbeccb86..b4dc2fbd 100644 --- a/src/main/java/com/samskivert/util/JDK14Logger.java +++ b/src/main/java/com/samskivert/util/JDK14Logger.java @@ -21,6 +21,9 @@ package com.samskivert.util; import java.io.UnsupportedEncodingException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import java.util.logging.Handler; import java.util.logging.Level; @@ -102,7 +105,7 @@ public class JDK14Logger implements Logger.Factory return new String[] { null, null }; } - protected static class Impl extends Logger + protected static class Impl extends Logger { public Impl (java.util.logging.Logger impl) { @@ -110,37 +113,25 @@ public class JDK14Logger implements Logger.Factory } @Override // from Logger - public void debug (Object message, Object... args) + protected List getLevels () { - if (_impl.isLoggable(Level.FINE)) { - _impl.log(Level.FINE, format(message, args), getException(message, args)); - } + return LEVELS; } @Override // from Logger - public void info (Object message, Object... args) + protected boolean shouldLog (Level level) { - if (_impl.isLoggable(Level.INFO)) { - _impl.log(Level.INFO, format(message, args), getException(message, args)); - } + return _impl.isLoggable(level); } @Override // from Logger - public void warning (Object message, Object... args) + protected void doLog (Level level, String formatted, Throwable throwable) { - if (_impl.isLoggable(Level.WARNING)) { - _impl.log(Level.WARNING, format(message, args), getException(message, args)); - } - } - - @Override // from Logger - public void error (Object message, Object... args) - { - if (_impl.isLoggable(Level.SEVERE)) { - _impl.log(Level.SEVERE, format(message, args), getException(message, args)); - } + _impl.log(level, formatted, throwable); } protected java.util.logging.Logger _impl; + protected static final List LEVELS = Collections.unmodifiableList(Arrays.asList( + Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE)); } } diff --git a/src/main/java/com/samskivert/util/Log4JLogger.java b/src/main/java/com/samskivert/util/Log4JLogger.java index 3d7946b4..c21c4d8a 100644 --- a/src/main/java/com/samskivert/util/Log4JLogger.java +++ b/src/main/java/com/samskivert/util/Log4JLogger.java @@ -20,6 +20,10 @@ package com.samskivert.util; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + import org.apache.log4j.Level; /** @@ -44,7 +48,7 @@ public class Log4JLogger implements Logger.Factory return getLogger(clazz.getName()); } - protected static class Impl extends Logger + protected static class Impl extends Logger { public Impl (org.apache.log4j.Logger impl) { @@ -52,38 +56,26 @@ public class Log4JLogger implements Logger.Factory } @Override // from Logger - public void debug (Object message, Object... args) + protected List getLevels () { - if (_impl.isEnabledFor(Level.DEBUG)) { - _impl.log(_self, Level.DEBUG, format(message, args), getException(message, args)); - } + return LEVELS; } @Override // from Logger - public void info (Object message, Object... args) + protected boolean shouldLog (Level level) { - if (_impl.isEnabledFor(Level.INFO)) { - _impl.log(_self, Level.INFO, format(message, args), getException(message, args)); - } + return _impl.isEnabledFor(level); } @Override // from Logger - public void warning (Object message, Object... args) + protected void doLog (Level level, String formatted, Throwable throwable) { - if (_impl.isEnabledFor(Level.WARN)) { - _impl.log(_self, Level.WARN, format(message, args), getException(message, args)); - } - } - - @Override // from Logger - public void error (Object message, Object... args) - { - if (_impl.isEnabledFor(Level.ERROR)) { - _impl.log(_self, Level.ERROR, format(message, args), getException(message, args)); - } + _impl.log(_self, level, formatted, throwable); } protected final org.apache.log4j.Logger _impl; protected final String _self = getClass().getName(); + protected static final List LEVELS = Collections.unmodifiableList(Arrays.asList( + Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR)); } } diff --git a/src/main/java/com/samskivert/util/Logger.java b/src/main/java/com/samskivert/util/Logger.java index 7bbd1388..6f69f7fc 100644 --- a/src/main/java/com/samskivert/util/Logger.java +++ b/src/main/java/com/samskivert/util/Logger.java @@ -20,6 +20,8 @@ package com.samskivert.util; +import java.util.List; + /** * Provides logging services to this library and others which depend on this library in such a way * that they can be used in a larger project and easily be made to log to the logging framework in @@ -37,7 +39,7 @@ package com.samskivert.util; *

The final parameter can optionally be a Throwable, which will be supplied to the underlying * log system as an exception to accompany the log message. */ -public abstract class Logger +public abstract class Logger { /** * Used to create logger instances. This is only public so that the log factory can be @@ -94,7 +96,10 @@ public abstract class Logger * @param message the message to be logged. * @param args a list of key/value pairs and an optional final Throwable. */ - public abstract void debug (Object message, Object... args); + public void debug (Object message, Object... args) + { + doLog(0, message, args); + } /** * Logs an info message. @@ -102,7 +107,10 @@ public abstract class Logger * @param message the message to be logged. * @param args a list of key/value pairs and an optional final Throwable. */ - public abstract void info (Object message, Object... args); + public void info (Object message, Object... args) + { + doLog(1, message, args); + } /** * Logs a warning message. @@ -110,7 +118,10 @@ public abstract class Logger * @param message the message to be logged. * @param args a list of key/value pairs and an optional final Throwable. */ - public abstract void warning (Object message, Object... args); + public void warning (Object message, Object... args) + { + doLog(2, message, args); + } /** * Logs an error message. @@ -118,23 +129,61 @@ public abstract class Logger * @param message the message to be logged. * @param args a list of key/value pairs and an optional final Throwable. */ - public abstract void error (Object message, Object... args); - - /** - * Extracts the exception from the message and arguments (if there is one). For use by logger - * implementations. - */ - protected Throwable getException (Object message, Object[] args) + public void error (Object message, Object... args) { - if (message instanceof Throwable) { - return (Throwable)message; - } else if (args.length % 2 == 1 && args[args.length-1] instanceof Throwable) { - return (Throwable)args[args.length-1]; - } else { - return null; - } + doLog(3, message, args); } + protected void doLog (int levIdx, Object message, Object[] args) + { + L level = getLevels().get(levIdx); + if (!shouldLog(level)) { + return; + } + Throwable err = null; + int nn = args.length; + if (message instanceof Throwable) { + err = (Throwable)message; + } else if (nn % 2 == 1 && (args[nn - 1] instanceof Throwable)) { + err = (Throwable)args[--nn]; + } + String msg = String.valueOf(message); + if (nn > 0) { + StringBuilder buf = new StringBuilder(msg); + if (msg.length() > 0) { + buf.append(' '); + } + buf.append('['); + for (int ii = 0; ii < nn; ii += 2) { + if (ii > 0) { + buf.append(',').append(' '); + } + buf.append(args[ii]).append('='); + try { + Object arg = args[ii + 1]; + buf.append(((arg == null) || !arg.getClass().isArray()) + ? arg + : LogBuilder.arrayStr(arg)); + } catch (Throwable t) { + buf.append(""); + } + } + msg = buf.append(']').toString(); + } + doLog(level, msg, err); + } + + /** Returns the logger implementation specific level constants in a specific order: debug, + * info, warn, error. */ + protected abstract List getLevels (); + + /** Returns true if a log message at the specified level should be logged. */ + protected abstract boolean shouldLog (L level); + + /** Performs the actual logging of a message at the specified level. + * @param throwable an exception that accompanies this message or null. */ + protected abstract void doLog (L level, String formatted, Throwable throwable); + /** * Called at static initialization time. Selects and initializes our logging backend. */