From dd7ec6b31e6235b73a80a231bf4a3236da4f866c Mon Sep 17 00:00:00 2001 From: Jeremy Landis Date: Tue, 6 May 2014 20:28:13 -0400 Subject: [PATCH] Add git attributes Added suggested git attributes & the few files that needed to change to comply as a result. --- .gitattributes | 22 + .../org/slf4j/impl/AndroidLoggerAdapter.java | 1104 +++++++------- .../org/slf4j/impl/AndroidLoggerFactory.java | 246 ++-- .../java/org/slf4j/impl/SimpleLogger.java | 1306 ++++++++--------- .../test/resources/simplelogger.properties | 68 +- 5 files changed, 1384 insertions(+), 1362 deletions(-) create mode 100644 .gitattributes diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..412eeda7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,22 @@ +# Auto detect text files and perform LF normalization +* text=auto + +# Custom for Visual Studio +*.cs diff=csharp +*.sln merge=union +*.csproj merge=union +*.vbproj merge=union +*.fsproj merge=union +*.dbproj merge=union + +# Standard to msysgit +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain diff --git a/slf4j-android/src/main/java/org/slf4j/impl/AndroidLoggerAdapter.java b/slf4j-android/src/main/java/org/slf4j/impl/AndroidLoggerAdapter.java index 06d1daa7..c13a7ce8 100755 --- a/slf4j-android/src/main/java/org/slf4j/impl/AndroidLoggerAdapter.java +++ b/slf4j-android/src/main/java/org/slf4j/impl/AndroidLoggerAdapter.java @@ -1,553 +1,553 @@ -/* - * Copyright (c) 2004-2013 QOS.ch - * All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ -package org.slf4j.impl; - -import android.util.Log; -import org.slf4j.helpers.FormattingTuple; -import org.slf4j.helpers.MarkerIgnoringBase; -import org.slf4j.helpers.MessageFormatter; - -/** - *

A simple implementation that delegates all log requests to the Google Android - * logging facilities. Note that this logger does not support {@link org.slf4j.Marker}. - * Methods taking marker data as parameter simply invoke the eponymous method - * without the Marker argument, discarding any marker data in the process.

- * - *

The logging levels specified for SLF4J can be almost directly mapped to - * the levels that exist in the Google Android platform. The following table - * shows the mapping implemented by this logger.

- * - * - * - * - * - * - * - * - *
SLF4JAndroid
TRACE{@link android.util.Log#VERBOSE}
DEBUG{@link android.util.Log#DEBUG}
INFO{@link android.util.Log#INFO}
WARN{@link android.util.Log#WARN}
ERROR{@link android.util.Log#ERROR}
- * - *

Use loggers as usual: - *

- *

- * - *

Logger instances created using the LoggerFactory are named either according to the name - * or the fully qualified class name of the class given as a parameter. - * Each logger name will be used as the log message tag on the Android platform. - * However, tag names cannot be longer than 23 characters so if logger name exceeds this limit then - * it will be truncated by the LoggerFactory. The following examples illustrate this. - * - * - * - * - * - * - *
Original NameTruncated Name
org.example.myproject.mypackage.MyClasso*.e*.m*.m*.MyClass
o.e.myproject.mypackage.MyClasso.e.m*.m*.MyClass
org.example.ThisNameIsWayTooLongAndWillBeTruncated*LongAndWillBeTruncated
ThisNameIsWayTooLongAndWillBeTruncated*LongAndWillBeTruncated
- *

- * - * @author Andrey Korzhevskiy - */ -class AndroidLoggerAdapter extends MarkerIgnoringBase { - private static final long serialVersionUID = -1227274521521287937L; - - - /** - * Package access allows only {@link AndroidLoggerFactory} to instantiate - * SimpleLogger instances. - */ - AndroidLoggerAdapter(String tag) { - this.name = tag; - } - - /** - * Is this logger instance enabled for the VERBOSE level? - * - * @return True if this Logger is enabled for level VERBOSE, false otherwise. - */ - public boolean isTraceEnabled() { - return isLoggable(Log.VERBOSE); - } - - /** - * Log a message object at level VERBOSE. - * - * @param msg - * - the message object to be logged - */ - public void trace(String msg) { - log(Log.VERBOSE, msg, null); - } - - /** - * Log a message at level VERBOSE according to the specified format and - * argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for level VERBOSE. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - public void trace(String format, Object arg) { - formatAndLog(Log.VERBOSE, format, arg); - } - - /** - * Log a message at level VERBOSE according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the VERBOSE level. - *

- * - * @param format - * the format string - * @param arg1 - * the first argument - * @param arg2 - * the second argument - */ - public void trace(String format, Object arg1, Object arg2) { - formatAndLog(Log.VERBOSE, format, arg1, arg2); - } - - /** - * Log a message at level VERBOSE according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the VERBOSE level. - *

- * - * @param format - * the format string - * @param argArray - * an array of arguments - */ - public void trace(String format, Object... argArray) { - formatAndLog(Log.VERBOSE, format, argArray); - } - - /** - * Log an exception (throwable) at level VERBOSE with an accompanying message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - public void trace(String msg, Throwable t) { - log(Log.VERBOSE, msg, t); - } - - /** - * Is this logger instance enabled for the DEBUG level? - * - * @return True if this Logger is enabled for level DEBUG, false otherwise. - */ - public boolean isDebugEnabled() { - return isLoggable(Log.DEBUG); - } - - /** - * Log a message object at level DEBUG. - * - * @param msg - * - the message object to be logged - */ - public void debug(String msg) { - log(Log.DEBUG, msg, null); - } - - /** - * Log a message at level DEBUG according to the specified format and argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for level DEBUG. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - public void debug(String format, Object arg) { - formatAndLog(Log.DEBUG, format, arg); - } - - /** - * Log a message at level DEBUG according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the DEBUG level. - *

- * - * @param format - * the format string - * @param arg1 - * the first argument - * @param arg2 - * the second argument - */ - public void debug(String format, Object arg1, Object arg2) { - formatAndLog(Log.DEBUG, format, arg1, arg2); - } - - /** - * Log a message at level DEBUG according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the DEBUG level. - *

- * - * @param format - * the format string - * @param argArray - * an array of arguments - */ - public void debug(String format, Object... argArray) { - formatAndLog(Log.DEBUG, format, argArray); - } - - /** - * Log an exception (throwable) at level DEBUG with an accompanying message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - public void debug(String msg, Throwable t) { - log(Log.VERBOSE, msg, t); - } - - /** - * Is this logger instance enabled for the INFO level? - * - * @return True if this Logger is enabled for the INFO level, false otherwise. - */ - public boolean isInfoEnabled() { - return isLoggable(Log.INFO); - } - - /** - * Log a message object at the INFO level. - * - * @param msg - * - the message object to be logged - */ - public void info(String msg) { - log(Log.INFO, msg, null); - } - - /** - * Log a message at level INFO according to the specified format and argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the INFO level. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - public void info(String format, Object arg) { - formatAndLog(Log.INFO, format, arg); - } - - /** - * Log a message at the INFO level according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the INFO level. - *

- * - * @param format - * the format string - * @param arg1 - * the first argument - * @param arg2 - * the second argument - */ - public void info(String format, Object arg1, Object arg2) { - formatAndLog(Log.INFO, format, arg1, arg2); - } - - /** - * Log a message at level INFO according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the INFO level. - *

- * - * @param format - * the format string - * @param argArray - * an array of arguments - */ - public void info(String format, Object... argArray) { - formatAndLog(Log.INFO, format, argArray); - } - - /** - * Log an exception (throwable) at the INFO level with an accompanying - * message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - public void info(String msg, Throwable t) { - log(Log.INFO, msg, t); - } - - /** - * Is this logger instance enabled for the WARN level? - * - * @return True if this Logger is enabled for the WARN level, false - * otherwise. - */ - public boolean isWarnEnabled() { - return isLoggable(Log.WARN); - } - - /** - * Log a message object at the WARN level. - * - * @param msg - * - the message object to be logged - */ - public void warn(String msg) { - log(Log.WARN, msg, null); - } - - /** - * Log a message at the WARN level according to the specified format and - * argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the WARN level. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - public void warn(String format, Object arg) { - formatAndLog(Log.WARN, format, arg); - } - - /** - * Log a message at the WARN level according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the WARN level. - *

- * - * @param format - * the format string - * @param arg1 - * the first argument - * @param arg2 - * the second argument - */ - public void warn(String format, Object arg1, Object arg2) { - formatAndLog(Log.WARN, format, arg1, arg2); - } - - /** - * Log a message at level WARN according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the WARN level. - *

- * - * @param format - * the format string - * @param argArray - * an array of arguments - */ - public void warn(String format, Object... argArray) { - formatAndLog(Log.WARN, format, argArray); - } - - /** - * Log an exception (throwable) at the WARN level with an accompanying - * message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - public void warn(String msg, Throwable t) { - log(Log.WARN, msg, t); - } - - /** - * Is this logger instance enabled for level ERROR? - * - * @return True if this Logger is enabled for level ERROR, false otherwise. - */ - public boolean isErrorEnabled() { - return isLoggable(Log.ERROR); - } - - /** - * Log a message object at the ERROR level. - * - * @param msg - * - the message object to be logged - */ - public void error(String msg) { - log(Log.ERROR, msg, null); - } - - /** - * Log a message at the ERROR level according to the specified format and - * argument. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the ERROR level. - *

- * - * @param format - * the format string - * @param arg - * the argument - */ - public void error(String format, Object arg) { - formatAndLog(Log.ERROR, format, arg); - } - - /** - * Log a message at the ERROR level according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the ERROR level. - *

- * - * @param format - * the format string - * @param arg1 - * the first argument - * @param arg2 - * the second argument - */ - public void error(String format, Object arg1, Object arg2) { - formatAndLog(Log.ERROR, format, arg1, arg2); - } - - /** - * Log a message at level ERROR according to the specified format and - * arguments. - * - *

- * This form avoids superfluous object creation when the logger is disabled - * for the ERROR level. - *

- * - * @param format - * the format string - * @param argArray - * an array of arguments - */ - public void error(String format, Object... argArray) { - formatAndLog(Log.ERROR, format, argArray); - } - - /** - * Log an exception (throwable) at the ERROR level with an accompanying - * message. - * - * @param msg - * the message accompanying the exception - * @param t - * the exception (throwable) to log - */ - public void error(String msg, Throwable t) { - log(Log.ERROR, msg, t); - } - - private void formatAndLog(int priority, String format, Object... argArray) { - if (isLoggable(priority)) { - FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); - _log(priority, ft.getMessage(), ft.getThrowable()); - } - } - - private void log(int priority, String message, Throwable throwable) { - if (isLoggable(priority)) { - _log(priority, message, throwable); - } - } - - private boolean isLoggable(int priority) { - return Log.isLoggable(name, priority); - } - - private void _log(int priority, String message, Throwable throwable) { - if (throwable != null) { - message += '\n' + Log.getStackTraceString(throwable); - } - Log.println(priority, name, message); - } +/* + * Copyright (c) 2004-2013 QOS.ch + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.slf4j.impl; + +import android.util.Log; +import org.slf4j.helpers.FormattingTuple; +import org.slf4j.helpers.MarkerIgnoringBase; +import org.slf4j.helpers.MessageFormatter; + +/** + *

A simple implementation that delegates all log requests to the Google Android + * logging facilities. Note that this logger does not support {@link org.slf4j.Marker}. + * Methods taking marker data as parameter simply invoke the eponymous method + * without the Marker argument, discarding any marker data in the process.

+ * + *

The logging levels specified for SLF4J can be almost directly mapped to + * the levels that exist in the Google Android platform. The following table + * shows the mapping implemented by this logger.

+ * + * + * + * + * + * + * + * + *
SLF4JAndroid
TRACE{@link android.util.Log#VERBOSE}
DEBUG{@link android.util.Log#DEBUG}
INFO{@link android.util.Log#INFO}
WARN{@link android.util.Log#WARN}
ERROR{@link android.util.Log#ERROR}
+ * + *

Use loggers as usual: + *

+ *

+ * + *

Logger instances created using the LoggerFactory are named either according to the name + * or the fully qualified class name of the class given as a parameter. + * Each logger name will be used as the log message tag on the Android platform. + * However, tag names cannot be longer than 23 characters so if logger name exceeds this limit then + * it will be truncated by the LoggerFactory. The following examples illustrate this. + * + * + * + * + * + * + *
Original NameTruncated Name
org.example.myproject.mypackage.MyClasso*.e*.m*.m*.MyClass
o.e.myproject.mypackage.MyClasso.e.m*.m*.MyClass
org.example.ThisNameIsWayTooLongAndWillBeTruncated*LongAndWillBeTruncated
ThisNameIsWayTooLongAndWillBeTruncated*LongAndWillBeTruncated
+ *

+ * + * @author Andrey Korzhevskiy + */ +class AndroidLoggerAdapter extends MarkerIgnoringBase { + private static final long serialVersionUID = -1227274521521287937L; + + + /** + * Package access allows only {@link AndroidLoggerFactory} to instantiate + * SimpleLogger instances. + */ + AndroidLoggerAdapter(String tag) { + this.name = tag; + } + + /** + * Is this logger instance enabled for the VERBOSE level? + * + * @return True if this Logger is enabled for level VERBOSE, false otherwise. + */ + public boolean isTraceEnabled() { + return isLoggable(Log.VERBOSE); + } + + /** + * Log a message object at level VERBOSE. + * + * @param msg + * - the message object to be logged + */ + public void trace(String msg) { + log(Log.VERBOSE, msg, null); + } + + /** + * Log a message at level VERBOSE according to the specified format and + * argument. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for level VERBOSE. + *

+ * + * @param format + * the format string + * @param arg + * the argument + */ + public void trace(String format, Object arg) { + formatAndLog(Log.VERBOSE, format, arg); + } + + /** + * Log a message at level VERBOSE according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the VERBOSE level. + *

+ * + * @param format + * the format string + * @param arg1 + * the first argument + * @param arg2 + * the second argument + */ + public void trace(String format, Object arg1, Object arg2) { + formatAndLog(Log.VERBOSE, format, arg1, arg2); + } + + /** + * Log a message at level VERBOSE according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the VERBOSE level. + *

+ * + * @param format + * the format string + * @param argArray + * an array of arguments + */ + public void trace(String format, Object... argArray) { + formatAndLog(Log.VERBOSE, format, argArray); + } + + /** + * Log an exception (throwable) at level VERBOSE with an accompanying message. + * + * @param msg + * the message accompanying the exception + * @param t + * the exception (throwable) to log + */ + public void trace(String msg, Throwable t) { + log(Log.VERBOSE, msg, t); + } + + /** + * Is this logger instance enabled for the DEBUG level? + * + * @return True if this Logger is enabled for level DEBUG, false otherwise. + */ + public boolean isDebugEnabled() { + return isLoggable(Log.DEBUG); + } + + /** + * Log a message object at level DEBUG. + * + * @param msg + * - the message object to be logged + */ + public void debug(String msg) { + log(Log.DEBUG, msg, null); + } + + /** + * Log a message at level DEBUG according to the specified format and argument. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for level DEBUG. + *

+ * + * @param format + * the format string + * @param arg + * the argument + */ + public void debug(String format, Object arg) { + formatAndLog(Log.DEBUG, format, arg); + } + + /** + * Log a message at level DEBUG according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the DEBUG level. + *

+ * + * @param format + * the format string + * @param arg1 + * the first argument + * @param arg2 + * the second argument + */ + public void debug(String format, Object arg1, Object arg2) { + formatAndLog(Log.DEBUG, format, arg1, arg2); + } + + /** + * Log a message at level DEBUG according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the DEBUG level. + *

+ * + * @param format + * the format string + * @param argArray + * an array of arguments + */ + public void debug(String format, Object... argArray) { + formatAndLog(Log.DEBUG, format, argArray); + } + + /** + * Log an exception (throwable) at level DEBUG with an accompanying message. + * + * @param msg + * the message accompanying the exception + * @param t + * the exception (throwable) to log + */ + public void debug(String msg, Throwable t) { + log(Log.VERBOSE, msg, t); + } + + /** + * Is this logger instance enabled for the INFO level? + * + * @return True if this Logger is enabled for the INFO level, false otherwise. + */ + public boolean isInfoEnabled() { + return isLoggable(Log.INFO); + } + + /** + * Log a message object at the INFO level. + * + * @param msg + * - the message object to be logged + */ + public void info(String msg) { + log(Log.INFO, msg, null); + } + + /** + * Log a message at level INFO according to the specified format and argument. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the INFO level. + *

+ * + * @param format + * the format string + * @param arg + * the argument + */ + public void info(String format, Object arg) { + formatAndLog(Log.INFO, format, arg); + } + + /** + * Log a message at the INFO level according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the INFO level. + *

+ * + * @param format + * the format string + * @param arg1 + * the first argument + * @param arg2 + * the second argument + */ + public void info(String format, Object arg1, Object arg2) { + formatAndLog(Log.INFO, format, arg1, arg2); + } + + /** + * Log a message at level INFO according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the INFO level. + *

+ * + * @param format + * the format string + * @param argArray + * an array of arguments + */ + public void info(String format, Object... argArray) { + formatAndLog(Log.INFO, format, argArray); + } + + /** + * Log an exception (throwable) at the INFO level with an accompanying + * message. + * + * @param msg + * the message accompanying the exception + * @param t + * the exception (throwable) to log + */ + public void info(String msg, Throwable t) { + log(Log.INFO, msg, t); + } + + /** + * Is this logger instance enabled for the WARN level? + * + * @return True if this Logger is enabled for the WARN level, false + * otherwise. + */ + public boolean isWarnEnabled() { + return isLoggable(Log.WARN); + } + + /** + * Log a message object at the WARN level. + * + * @param msg + * - the message object to be logged + */ + public void warn(String msg) { + log(Log.WARN, msg, null); + } + + /** + * Log a message at the WARN level according to the specified format and + * argument. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the WARN level. + *

+ * + * @param format + * the format string + * @param arg + * the argument + */ + public void warn(String format, Object arg) { + formatAndLog(Log.WARN, format, arg); + } + + /** + * Log a message at the WARN level according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the WARN level. + *

+ * + * @param format + * the format string + * @param arg1 + * the first argument + * @param arg2 + * the second argument + */ + public void warn(String format, Object arg1, Object arg2) { + formatAndLog(Log.WARN, format, arg1, arg2); + } + + /** + * Log a message at level WARN according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the WARN level. + *

+ * + * @param format + * the format string + * @param argArray + * an array of arguments + */ + public void warn(String format, Object... argArray) { + formatAndLog(Log.WARN, format, argArray); + } + + /** + * Log an exception (throwable) at the WARN level with an accompanying + * message. + * + * @param msg + * the message accompanying the exception + * @param t + * the exception (throwable) to log + */ + public void warn(String msg, Throwable t) { + log(Log.WARN, msg, t); + } + + /** + * Is this logger instance enabled for level ERROR? + * + * @return True if this Logger is enabled for level ERROR, false otherwise. + */ + public boolean isErrorEnabled() { + return isLoggable(Log.ERROR); + } + + /** + * Log a message object at the ERROR level. + * + * @param msg + * - the message object to be logged + */ + public void error(String msg) { + log(Log.ERROR, msg, null); + } + + /** + * Log a message at the ERROR level according to the specified format and + * argument. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the ERROR level. + *

+ * + * @param format + * the format string + * @param arg + * the argument + */ + public void error(String format, Object arg) { + formatAndLog(Log.ERROR, format, arg); + } + + /** + * Log a message at the ERROR level according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the ERROR level. + *

+ * + * @param format + * the format string + * @param arg1 + * the first argument + * @param arg2 + * the second argument + */ + public void error(String format, Object arg1, Object arg2) { + formatAndLog(Log.ERROR, format, arg1, arg2); + } + + /** + * Log a message at level ERROR according to the specified format and + * arguments. + * + *

+ * This form avoids superfluous object creation when the logger is disabled + * for the ERROR level. + *

+ * + * @param format + * the format string + * @param argArray + * an array of arguments + */ + public void error(String format, Object... argArray) { + formatAndLog(Log.ERROR, format, argArray); + } + + /** + * Log an exception (throwable) at the ERROR level with an accompanying + * message. + * + * @param msg + * the message accompanying the exception + * @param t + * the exception (throwable) to log + */ + public void error(String msg, Throwable t) { + log(Log.ERROR, msg, t); + } + + private void formatAndLog(int priority, String format, Object... argArray) { + if (isLoggable(priority)) { + FormattingTuple ft = MessageFormatter.arrayFormat(format, argArray); + _log(priority, ft.getMessage(), ft.getThrowable()); + } + } + + private void log(int priority, String message, Throwable throwable) { + if (isLoggable(priority)) { + _log(priority, message, throwable); + } + } + + private boolean isLoggable(int priority) { + return Log.isLoggable(name, priority); + } + + private void _log(int priority, String message, Throwable throwable) { + if (throwable != null) { + message += '\n' + Log.getStackTraceString(throwable); + } + Log.println(priority, name, message); + } } \ No newline at end of file diff --git a/slf4j-android/src/main/java/org/slf4j/impl/AndroidLoggerFactory.java b/slf4j-android/src/main/java/org/slf4j/impl/AndroidLoggerFactory.java index 88ae5250..f27ff551 100755 --- a/slf4j-android/src/main/java/org/slf4j/impl/AndroidLoggerFactory.java +++ b/slf4j-android/src/main/java/org/slf4j/impl/AndroidLoggerFactory.java @@ -1,124 +1,124 @@ -/* - * Copyright (c) 2004-2013 QOS.ch - * All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ -package org.slf4j.impl; - -import org.slf4j.ILoggerFactory; -import org.slf4j.Logger; - -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -/** - * AndroidLoggerFactory is an implementation of {@link ILoggerFactory} returning - * the appropriately named {@link AndroidLoggerFactory} instance. - * - * @author Andrey Korzhevskiy - */ -class AndroidLoggerFactory implements ILoggerFactory { - static final String ANONYMOUS_TAG = "null"; - static final int TAG_MAX_LENGTH = 23; - - private final ConcurrentMap loggerMap = new ConcurrentHashMap(); - - /** - * Return an appropriate {@link AndroidLoggerAdapter} instance by name. - */ - public Logger getLogger(String name) { - String tag = loggerNameToTag(name); - Logger logger = loggerMap.get(tag); - if (logger == null) { - Logger newInstance = new AndroidLoggerAdapter(tag); - Logger oldInstance = loggerMap.putIfAbsent(tag, newInstance); - logger = oldInstance == null ? newInstance : oldInstance; - } - return logger; - } - - /** - * Tag names cannot be longer than {@value #TAG_MAX_LENGTH} characters on Android platform. - * - * Returns the short logger tag (up to {@value #TAG_MAX_LENGTH} characters) for the given logger name. - * Traditionally loggers are named by fully-qualified Java classes; this - * method attempts to return a concise identifying part of such names. - * - * See also: - * android/system/core/include/cutils/property.h - * android/frameworks/base/core/jni/android_util_Log.cpp - * dalvik.system.DalvikLogging - * - */ - static String loggerNameToTag(String loggerName) { - // Anonymous logger - if (loggerName == null) { - return ANONYMOUS_TAG; - } - - int length = loggerName.length(); - if (length <= TAG_MAX_LENGTH) { - return loggerName; - } - - int tagLength = 0; - int lastTokenIndex = 0; - int lastPeriodIndex; - StringBuilder tagName = new StringBuilder(TAG_MAX_LENGTH + 3); - while ((lastPeriodIndex = loggerName.indexOf('.', lastTokenIndex)) != -1) { - tagName.append(loggerName.charAt(lastTokenIndex)); - // token of one character appended as is otherwise truncate it to one character - int tokenLength = lastPeriodIndex - lastTokenIndex; - if (tokenLength > 1) { - tagName.append('*'); - } - tagName.append('.'); - lastTokenIndex = lastPeriodIndex + 1; - - // check if name is already too long - tagLength = tagName.length(); - if (tagLength > TAG_MAX_LENGTH) { - return getSimpleName(loggerName); - } - } - - // Either we had no useful dot location at all - // or last token would exceed TAG_MAX_LENGTH - int tokenLength = length - lastTokenIndex; - if (tagLength == 0 || (tagLength + tokenLength) > TAG_MAX_LENGTH) { - return getSimpleName(loggerName); - } - - // last token (usually class name) appended as is - tagName.append(loggerName, lastTokenIndex, length); - return tagName.toString(); - } - - private static String getSimpleName(String loggerName) { - // Take leading part and append '*' to indicate that it was truncated - int length = loggerName.length(); - int lastPeriodIndex = loggerName.lastIndexOf('.'); - return lastPeriodIndex != -1 && length - (lastPeriodIndex + 1) <= TAG_MAX_LENGTH - ? loggerName.substring(lastPeriodIndex + 1) - : '*' + loggerName.substring(length - TAG_MAX_LENGTH + 1); - } +/* + * Copyright (c) 2004-2013 QOS.ch + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.slf4j.impl; + +import org.slf4j.ILoggerFactory; +import org.slf4j.Logger; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * AndroidLoggerFactory is an implementation of {@link ILoggerFactory} returning + * the appropriately named {@link AndroidLoggerFactory} instance. + * + * @author Andrey Korzhevskiy + */ +class AndroidLoggerFactory implements ILoggerFactory { + static final String ANONYMOUS_TAG = "null"; + static final int TAG_MAX_LENGTH = 23; + + private final ConcurrentMap loggerMap = new ConcurrentHashMap(); + + /** + * Return an appropriate {@link AndroidLoggerAdapter} instance by name. + */ + public Logger getLogger(String name) { + String tag = loggerNameToTag(name); + Logger logger = loggerMap.get(tag); + if (logger == null) { + Logger newInstance = new AndroidLoggerAdapter(tag); + Logger oldInstance = loggerMap.putIfAbsent(tag, newInstance); + logger = oldInstance == null ? newInstance : oldInstance; + } + return logger; + } + + /** + * Tag names cannot be longer than {@value #TAG_MAX_LENGTH} characters on Android platform. + * + * Returns the short logger tag (up to {@value #TAG_MAX_LENGTH} characters) for the given logger name. + * Traditionally loggers are named by fully-qualified Java classes; this + * method attempts to return a concise identifying part of such names. + * + * See also: + * android/system/core/include/cutils/property.h + * android/frameworks/base/core/jni/android_util_Log.cpp + * dalvik.system.DalvikLogging + * + */ + static String loggerNameToTag(String loggerName) { + // Anonymous logger + if (loggerName == null) { + return ANONYMOUS_TAG; + } + + int length = loggerName.length(); + if (length <= TAG_MAX_LENGTH) { + return loggerName; + } + + int tagLength = 0; + int lastTokenIndex = 0; + int lastPeriodIndex; + StringBuilder tagName = new StringBuilder(TAG_MAX_LENGTH + 3); + while ((lastPeriodIndex = loggerName.indexOf('.', lastTokenIndex)) != -1) { + tagName.append(loggerName.charAt(lastTokenIndex)); + // token of one character appended as is otherwise truncate it to one character + int tokenLength = lastPeriodIndex - lastTokenIndex; + if (tokenLength > 1) { + tagName.append('*'); + } + tagName.append('.'); + lastTokenIndex = lastPeriodIndex + 1; + + // check if name is already too long + tagLength = tagName.length(); + if (tagLength > TAG_MAX_LENGTH) { + return getSimpleName(loggerName); + } + } + + // Either we had no useful dot location at all + // or last token would exceed TAG_MAX_LENGTH + int tokenLength = length - lastTokenIndex; + if (tagLength == 0 || (tagLength + tokenLength) > TAG_MAX_LENGTH) { + return getSimpleName(loggerName); + } + + // last token (usually class name) appended as is + tagName.append(loggerName, lastTokenIndex, length); + return tagName.toString(); + } + + private static String getSimpleName(String loggerName) { + // Take leading part and append '*' to indicate that it was truncated + int length = loggerName.length(); + int lastPeriodIndex = loggerName.lastIndexOf('.'); + return lastPeriodIndex != -1 && length - (lastPeriodIndex + 1) <= TAG_MAX_LENGTH + ? loggerName.substring(lastPeriodIndex + 1) + : '*' + loggerName.substring(length - TAG_MAX_LENGTH + 1); + } } \ No newline at end of file diff --git a/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java b/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java index 18b395ff..d31d44af 100644 --- a/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java +++ b/slf4j-simple/src/main/java/org/slf4j/impl/SimpleLogger.java @@ -1,653 +1,653 @@ -/** - * Copyright (c) 2004-2012 QOS.ch - * All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining - * a copy of this software and associated documentation files (the - * "Software"), to deal in the Software without restriction, including - * without limitation the rights to use, copy, modify, merge, publish, - * distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to - * the following conditions: - * - * The above copyright notice and this permission notice shall be - * included in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE - * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION - * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION - * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - * - */ -package org.slf4j.impl; - -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.InputStream; -import java.io.PrintStream; -import java.security.AccessController; -import java.security.PrivilegedAction; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Properties; - -import org.slf4j.Logger; -import org.slf4j.helpers.FormattingTuple; -import org.slf4j.helpers.MarkerIgnoringBase; -import org.slf4j.helpers.MessageFormatter; -import org.slf4j.helpers.Util; -import org.slf4j.spi.LocationAwareLogger; - -/** - *

Simple implementation of {@link Logger} that sends all enabled log messages, - * for all defined loggers, to the console ({@code System.err}). - * The following system properties are supported to configure the behavior of this logger:

- * - *
    - *
  • org.slf4j.simpleLogger.logFile - The output target which can be the path to a file, or - * the special values "System.out" and "System.err". Default is "System.err". - * - *
  • org.slf4j.simpleLogger.defaultLogLevel - Default log level for all instances of SimpleLogger. - * Must be one of ("trace", "debug", "info", "warn", or "error"). If not specified, defaults to "info".
  • - * - *
  • org.slf4j.simpleLogger.log.a.b.c - Logging detail level for a SimpleLogger instance - * named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", or "error". When a SimpleLogger - * named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent - * logger will be used, and if none is set, then the value specified by - * org.slf4j.simpleLogger.defaultLogLevel will be used.
  • - * - *
  • org.slf4j.simpleLogger.showDateTime - Set to true if you want the current date and - * time to be included in output messages. Default is false
  • - * - *
  • org.slf4j.simpleLogger.dateTimeFormat - The date and time format to be used in the output messages. - * The pattern describing the date and time format is defined by - * SimpleDateFormat. - * If the format is not specified or is invalid, the number of milliseconds since start up will be output.
  • - * - *
  • org.slf4j.simpleLogger.showThreadName -Set to true if you want to output the current - * thread name. Defaults to true.
  • - * - *
  • org.slf4j.simpleLogger.showLogName - Set to true if you want the Logger instance name - * to be included in output messages. Defaults to true.
  • - * - *
  • org.slf4j.simpleLogger.showShortLogName - Set to true if you want the last component - * of the name to be included in output messages. Defaults to false.
  • - * - *
  • org.slf4j.simpleLogger.levelInBrackets - Should the level string be output in brackets? Defaults - * to false.
  • - * - *
  • org.slf4j.simpleLogger.warnLevelString - The string value output for the warn level. Defaults - * to WARN.
  • - - *
- * - *

In addition to looking for system properties with the names specified above, this implementation also checks for - * a class loader resource named "simplelogger.properties", and includes any matching definitions - * from this resource (if it exists).

- * - *

With no configuration, the default output includes the relative time in milliseconds, thread name, the level, - * logger name, and the message followed by the line separator for the host. In log4j terms it amounts to the "%r [%t] - * %level %logger - %m%n" pattern.

- *

Sample output follows.

- *
- * 176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order.
- * 225 [main] INFO examples.SortAlgo - Entered the sort method.
- * 304 [main] INFO examples.SortAlgo - Dump of integer array:
- * 317 [main] INFO examples.SortAlgo - Element [0] = 0
- * 331 [main] INFO examples.SortAlgo - Element [1] = 1
- * 343 [main] INFO examples.Sort - The next log statement should be an error message.
- * 346 [main] ERROR examples.SortAlgo - Tried to dump an uninitialized array.
- *   at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
- *   at org.log4j.examples.Sort.main(Sort.java:64)
- * 467 [main] INFO  examples.Sort - Exiting main method.
- * 
- * - *

This implementation is heavily inspired by - * Apache Commons Logging's SimpleLog.

- * - * @author Ceki Gülcü - * @author Scott Sanders - * @author Rod Waldhoff - * @author Robert Burrell Donkin - * @author Cédrik LIME - */ -public class SimpleLogger extends MarkerIgnoringBase { - - private static final long serialVersionUID = -632788891211436180L; - private static final String CONFIGURATION_FILE = "simplelogger.properties"; - - private static long START_TIME = System.currentTimeMillis(); - private static final Properties SIMPLE_LOGGER_PROPS = new Properties(); - - private static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT; - private static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT; - private static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT; - private static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT; - private static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT; - - private static boolean INITIALIZED = false; - - private static int DEFAULT_LOG_LEVEL = LOG_LEVEL_INFO; - private static boolean SHOW_DATE_TIME = false; - private static String DATE_TIME_FORMAT_STR = null; - private static DateFormat DATE_FORMATTER = null; - private static boolean SHOW_THREAD_NAME = true; - private static boolean SHOW_LOG_NAME = true; - private static boolean SHOW_SHORT_LOG_NAME = false; - private static String LOG_FILE = "System.err"; - private static PrintStream TARGET_STREAM = null; - private static boolean LEVEL_IN_BRACKETS = false; - private static String WARN_LEVEL_STRING = "WARN"; - - - /** All system properties used by SimpleLogger start with this prefix */ - public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger."; - - public static final String DEFAULT_LOG_LEVEL_KEY = SYSTEM_PREFIX + "defaultLogLevel"; - public static final String SHOW_DATE_TIME_KEY = SYSTEM_PREFIX + "showDateTime"; - public static final String DATE_TIME_FORMAT_KEY = SYSTEM_PREFIX + "dateTimeFormat"; - public static final String SHOW_THREAD_NAME_KEY = SYSTEM_PREFIX + "showThreadName"; - public static final String SHOW_LOG_NAME_KEY = SYSTEM_PREFIX + "showLogName"; - public static final String SHOW_SHORT_LOG_NAME_KEY = SYSTEM_PREFIX + "showShortLogName"; - public static final String LOG_FILE_KEY = SYSTEM_PREFIX + "logFile"; - public static final String LEVEL_IN_BRACKETS_KEY = SYSTEM_PREFIX + "levelInBrackets"; - public static final String WARN_LEVEL_STRING_KEY = SYSTEM_PREFIX + "warnLevelString"; - - - public static final String LOG_KEY_PREFIX = SYSTEM_PREFIX + "log."; - - - private static String getStringProperty(String name) { - String prop = null; - try { - prop = System.getProperty(name); - } catch (SecurityException e) { - ; // Ignore - } - return (prop == null) ? SIMPLE_LOGGER_PROPS.getProperty(name) : prop; - } - - private static String getStringProperty(String name, String defaultValue) { - String prop = getStringProperty(name); - return (prop == null) ? defaultValue : prop; - } - - private static boolean getBooleanProperty(String name, boolean defaultValue) { - String prop = getStringProperty(name); - return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop); - } - - - // Initialize class attributes. - // Load properties file, if found. - // Override with system properties. - static void init() { - INITIALIZED = true; - loadProperties(); - - String defaultLogLevelString = getStringProperty(DEFAULT_LOG_LEVEL_KEY, null); - if (defaultLogLevelString != null) - DEFAULT_LOG_LEVEL = stringToLevel(defaultLogLevelString); - - SHOW_LOG_NAME = getBooleanProperty(SHOW_LOG_NAME_KEY, SHOW_LOG_NAME); - SHOW_SHORT_LOG_NAME = getBooleanProperty(SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME); - SHOW_DATE_TIME = getBooleanProperty(SHOW_DATE_TIME_KEY, SHOW_DATE_TIME); - SHOW_THREAD_NAME = getBooleanProperty(SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME); - DATE_TIME_FORMAT_STR = getStringProperty(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR); - LEVEL_IN_BRACKETS = getBooleanProperty(LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS); - WARN_LEVEL_STRING = getStringProperty(WARN_LEVEL_STRING_KEY, WARN_LEVEL_STRING); - - LOG_FILE = getStringProperty(LOG_FILE_KEY, LOG_FILE); - TARGET_STREAM = computeTargetStream(LOG_FILE); - - if (DATE_TIME_FORMAT_STR != null) { - try { - DATE_FORMATTER = new SimpleDateFormat(DATE_TIME_FORMAT_STR); - } catch (IllegalArgumentException e) { - Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e); - } - } - } - - - private static PrintStream computeTargetStream(String logFile) { - if ("System.err".equalsIgnoreCase(logFile)) - return System.err; - else if ("System.out".equalsIgnoreCase(logFile)) { - return System.out; - } else { - try { - FileOutputStream fos = new FileOutputStream(logFile); - PrintStream printStream = new PrintStream(fos); - return printStream; - } catch (FileNotFoundException e) { - Util.report("Could not open [" + logFile + "]. Defaulting to System.err", e); - return System.err; - } - } - } - - private static void loadProperties() { - // Add props from the resource simplelogger.properties - InputStream in = AccessController.doPrivileged( - new PrivilegedAction() { - public InputStream run() { - ClassLoader threadCL = Thread.currentThread().getContextClassLoader(); - if (threadCL != null) { - return threadCL.getResourceAsStream(CONFIGURATION_FILE); - } else { - return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE); - } - } - }); - if (null != in) { - try { - SIMPLE_LOGGER_PROPS.load(in); - in.close(); - } catch (java.io.IOException e) { - // ignored - } - } - } - - /** The current log level */ - protected int currentLogLevel = LOG_LEVEL_INFO; - /** The short name of this simple log instance */ - private transient String shortLogName = null; - - /** - * Package access allows only {@link SimpleLoggerFactory} to instantiate - * SimpleLogger instances. - */ - SimpleLogger(String name) { - if (!INITIALIZED) { - init(); - } - this.name = name; - - String levelString = recursivelyComputeLevelString(); - if (levelString != null) { - this.currentLogLevel = stringToLevel(levelString); - } else { - this.currentLogLevel = DEFAULT_LOG_LEVEL; - } - } - - String recursivelyComputeLevelString() { - String tempName = name; - String levelString = null; - int indexOfLastDot = tempName.length(); - while ((levelString == null) && (indexOfLastDot > -1)) { - tempName = tempName.substring(0, indexOfLastDot); - levelString = getStringProperty(LOG_KEY_PREFIX + tempName, null); - indexOfLastDot = String.valueOf(tempName).lastIndexOf("."); - } - return levelString; - } - - private static int stringToLevel(String levelStr) { - if ("trace".equalsIgnoreCase(levelStr)) { - return LOG_LEVEL_TRACE; - } else if ("debug".equalsIgnoreCase(levelStr)) { - return LOG_LEVEL_DEBUG; - } else if ("info".equalsIgnoreCase(levelStr)) { - return LOG_LEVEL_INFO; - } else if ("warn".equalsIgnoreCase(levelStr)) { - return LOG_LEVEL_WARN; - } else if ("error".equalsIgnoreCase(levelStr)) { - return LOG_LEVEL_ERROR; - } - // assume INFO by default - return LOG_LEVEL_INFO; - } - - - /** - * This is our internal implementation for logging regular (non-parameterized) - * log messages. - * - * @param level One of the LOG_LEVEL_XXX constants defining the log level - * @param message The message itself - * @param t The exception whose stack trace should be logged - */ - private void log(int level, String message, Throwable t) { - if (!isLevelEnabled(level)) { - return; - } - - StringBuffer buf = new StringBuffer(32); - - // Append date-time if so configured - if (SHOW_DATE_TIME) { - if (DATE_FORMATTER != null) { - buf.append(getFormattedDate()); - buf.append(' '); - } else { - buf.append(System.currentTimeMillis() - START_TIME); - buf.append(' '); - } - } - - // Append current thread name if so configured - if (SHOW_THREAD_NAME) { - buf.append('['); - buf.append(Thread.currentThread().getName()); - buf.append("] "); - } - - if (LEVEL_IN_BRACKETS) buf.append('['); - - // Append a readable representation of the log level - switch (level) { - case LOG_LEVEL_TRACE: - buf.append("TRACE"); - break; - case LOG_LEVEL_DEBUG: - buf.append("DEBUG"); - break; - case LOG_LEVEL_INFO: - buf.append("INFO"); - break; - case LOG_LEVEL_WARN: - buf.append(WARN_LEVEL_STRING); - break; - case LOG_LEVEL_ERROR: - buf.append("ERROR"); - break; - } - if (LEVEL_IN_BRACKETS) buf.append(']'); - buf.append(' '); - - // Append the name of the log instance if so configured - if (SHOW_SHORT_LOG_NAME) { - if (shortLogName == null) shortLogName = computeShortName(); - buf.append(String.valueOf(shortLogName)).append(" - "); - } else if (SHOW_LOG_NAME) { - buf.append(String.valueOf(name)).append(" - "); - } - - // Append the message - buf.append(message); - - write(buf, t); - - } - - void write(StringBuffer buf, Throwable t) { - TARGET_STREAM.println(buf.toString()); - if (t != null) { - t.printStackTrace(TARGET_STREAM); - } - TARGET_STREAM.flush(); - } - - private String getFormattedDate() { - Date now = new Date(); - String dateText; - synchronized (DATE_FORMATTER) { - dateText = DATE_FORMATTER.format(now); - } - return dateText; - } - - private String computeShortName() { - return name.substring(name.lastIndexOf(".") + 1); - } - - /** - * For formatted messages, first substitute arguments and then log. - * - * @param level - * @param format - * @param arg1 - * @param arg2 - */ - private void formatAndLog(int level, String format, Object arg1, - Object arg2) { - if (!isLevelEnabled(level)) { - return; - } - FormattingTuple tp = MessageFormatter.format(format, arg1, arg2); - log(level, tp.getMessage(), tp.getThrowable()); - } - - /** - * For formatted messages, first substitute arguments and then log. - * - * @param level - * @param format - * @param arguments a list of 3 ore more arguments - */ - private void formatAndLog(int level, String format, Object... arguments) { - if (!isLevelEnabled(level)) { - return; - } - FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments); - log(level, tp.getMessage(), tp.getThrowable()); - } - - /** - * Is the given log level currently enabled? - * - * @param logLevel is this level enabled? - */ - protected boolean isLevelEnabled(int logLevel) { - // log level are numerically ordered so can use simple numeric - // comparison - return (logLevel >= currentLogLevel); - } - - /** Are {@code trace} messages currently enabled? */ - public boolean isTraceEnabled() { - return isLevelEnabled(LOG_LEVEL_TRACE); - } - - /** - * A simple implementation which logs messages of level TRACE according - * to the format outlined above. - */ - public void trace(String msg) { - log(LOG_LEVEL_TRACE, msg, null); - } - - /** - * Perform single parameter substitution before logging the message of level - * TRACE according to the format outlined above. - */ - public void trace(String format, Object param1) { - formatAndLog(LOG_LEVEL_TRACE, format, param1, null); - } - - /** - * Perform double parameter substitution before logging the message of level - * TRACE according to the format outlined above. - */ - public void trace(String format, Object param1, Object param2) { - formatAndLog(LOG_LEVEL_TRACE, format, param1, param2); - } - - /** - * Perform double parameter substitution before logging the message of level - * TRACE according to the format outlined above. - */ - public void trace(String format, Object... argArray) { - formatAndLog(LOG_LEVEL_TRACE, format, argArray); - } - - /** Log a message of level TRACE, including an exception. */ - public void trace(String msg, Throwable t) { - log(LOG_LEVEL_TRACE, msg, t); - } - - /** Are {@code debug} messages currently enabled? */ - public boolean isDebugEnabled() { - return isLevelEnabled(LOG_LEVEL_DEBUG); - } - - /** - * A simple implementation which logs messages of level DEBUG according - * to the format outlined above. - */ - public void debug(String msg) { - log(LOG_LEVEL_DEBUG, msg, null); - } - - /** - * Perform single parameter substitution before logging the message of level - * DEBUG according to the format outlined above. - */ - public void debug(String format, Object param1) { - formatAndLog(LOG_LEVEL_DEBUG, format, param1, null); - } - - /** - * Perform double parameter substitution before logging the message of level - * DEBUG according to the format outlined above. - */ - public void debug(String format, Object param1, Object param2) { - formatAndLog(LOG_LEVEL_DEBUG, format, param1, param2); - } - - /** - * Perform double parameter substitution before logging the message of level - * DEBUG according to the format outlined above. - */ - public void debug(String format, Object... argArray) { - formatAndLog(LOG_LEVEL_DEBUG, format, argArray); - } - - /** Log a message of level DEBUG, including an exception. */ - public void debug(String msg, Throwable t) { - log(LOG_LEVEL_DEBUG, msg, t); - } - - /** Are {@code info} messages currently enabled? */ - public boolean isInfoEnabled() { - return isLevelEnabled(LOG_LEVEL_INFO); - } - - /** - * A simple implementation which logs messages of level INFO according - * to the format outlined above. - */ - public void info(String msg) { - log(LOG_LEVEL_INFO, msg, null); - } - - /** - * Perform single parameter substitution before logging the message of level - * INFO according to the format outlined above. - */ - public void info(String format, Object arg) { - formatAndLog(LOG_LEVEL_INFO, format, arg, null); - } - - /** - * Perform double parameter substitution before logging the message of level - * INFO according to the format outlined above. - */ - public void info(String format, Object arg1, Object arg2) { - formatAndLog(LOG_LEVEL_INFO, format, arg1, arg2); - } - - /** - * Perform double parameter substitution before logging the message of level - * INFO according to the format outlined above. - */ - public void info(String format, Object... argArray) { - formatAndLog(LOG_LEVEL_INFO, format, argArray); - } - - /** Log a message of level INFO, including an exception. */ - public void info(String msg, Throwable t) { - log(LOG_LEVEL_INFO, msg, t); - } - - /** Are {@code warn} messages currently enabled? */ - public boolean isWarnEnabled() { - return isLevelEnabled(LOG_LEVEL_WARN); - } - - /** - * A simple implementation which always logs messages of level WARN according - * to the format outlined above. - */ - public void warn(String msg) { - log(LOG_LEVEL_WARN, msg, null); - } - - /** - * Perform single parameter substitution before logging the message of level - * WARN according to the format outlined above. - */ - public void warn(String format, Object arg) { - formatAndLog(LOG_LEVEL_WARN, format, arg, null); - } - - /** - * Perform double parameter substitution before logging the message of level - * WARN according to the format outlined above. - */ - public void warn(String format, Object arg1, Object arg2) { - formatAndLog(LOG_LEVEL_WARN, format, arg1, arg2); - } - - /** - * Perform double parameter substitution before logging the message of level - * WARN according to the format outlined above. - */ - public void warn(String format, Object... argArray) { - formatAndLog(LOG_LEVEL_WARN, format, argArray); - } - - /** Log a message of level WARN, including an exception. */ - public void warn(String msg, Throwable t) { - log(LOG_LEVEL_WARN, msg, t); - } - - /** Are {@code error} messages currently enabled? */ - public boolean isErrorEnabled() { - return isLevelEnabled(LOG_LEVEL_ERROR); - } - - /** - * A simple implementation which always logs messages of level ERROR according - * to the format outlined above. - */ - public void error(String msg) { - log(LOG_LEVEL_ERROR, msg, null); - } - - /** - * Perform single parameter substitution before logging the message of level - * ERROR according to the format outlined above. - */ - public void error(String format, Object arg) { - formatAndLog(LOG_LEVEL_ERROR, format, arg, null); - } - - /** - * Perform double parameter substitution before logging the message of level - * ERROR according to the format outlined above. - */ - public void error(String format, Object arg1, Object arg2) { - formatAndLog(LOG_LEVEL_ERROR, format, arg1, arg2); - } - - /** - * Perform double parameter substitution before logging the message of level - * ERROR according to the format outlined above. - */ - public void error(String format, Object... argArray) { - formatAndLog(LOG_LEVEL_ERROR, format, argArray); - } - - /** Log a message of level ERROR, including an exception. */ - public void error(String msg, Throwable t) { - log(LOG_LEVEL_ERROR, msg, t); - } -} +/** + * Copyright (c) 2004-2012 QOS.ch + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + */ +package org.slf4j.impl; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.helpers.FormattingTuple; +import org.slf4j.helpers.MarkerIgnoringBase; +import org.slf4j.helpers.MessageFormatter; +import org.slf4j.helpers.Util; +import org.slf4j.spi.LocationAwareLogger; + +/** + *

Simple implementation of {@link Logger} that sends all enabled log messages, + * for all defined loggers, to the console ({@code System.err}). + * The following system properties are supported to configure the behavior of this logger:

+ * + *
    + *
  • org.slf4j.simpleLogger.logFile - The output target which can be the path to a file, or + * the special values "System.out" and "System.err". Default is "System.err". + * + *
  • org.slf4j.simpleLogger.defaultLogLevel - Default log level for all instances of SimpleLogger. + * Must be one of ("trace", "debug", "info", "warn", or "error"). If not specified, defaults to "info".
  • + * + *
  • org.slf4j.simpleLogger.log.a.b.c - Logging detail level for a SimpleLogger instance + * named "a.b.c". Right-side value must be one of "trace", "debug", "info", "warn", or "error". When a SimpleLogger + * named "a.b.c" is initialized, its level is assigned from this property. If unspecified, the level of nearest parent + * logger will be used, and if none is set, then the value specified by + * org.slf4j.simpleLogger.defaultLogLevel will be used.
  • + * + *
  • org.slf4j.simpleLogger.showDateTime - Set to true if you want the current date and + * time to be included in output messages. Default is false
  • + * + *
  • org.slf4j.simpleLogger.dateTimeFormat - The date and time format to be used in the output messages. + * The pattern describing the date and time format is defined by + * SimpleDateFormat. + * If the format is not specified or is invalid, the number of milliseconds since start up will be output.
  • + * + *
  • org.slf4j.simpleLogger.showThreadName -Set to true if you want to output the current + * thread name. Defaults to true.
  • + * + *
  • org.slf4j.simpleLogger.showLogName - Set to true if you want the Logger instance name + * to be included in output messages. Defaults to true.
  • + * + *
  • org.slf4j.simpleLogger.showShortLogName - Set to true if you want the last component + * of the name to be included in output messages. Defaults to false.
  • + * + *
  • org.slf4j.simpleLogger.levelInBrackets - Should the level string be output in brackets? Defaults + * to false.
  • + * + *
  • org.slf4j.simpleLogger.warnLevelString - The string value output for the warn level. Defaults + * to WARN.
  • + + *
+ * + *

In addition to looking for system properties with the names specified above, this implementation also checks for + * a class loader resource named "simplelogger.properties", and includes any matching definitions + * from this resource (if it exists).

+ * + *

With no configuration, the default output includes the relative time in milliseconds, thread name, the level, + * logger name, and the message followed by the line separator for the host. In log4j terms it amounts to the "%r [%t] + * %level %logger - %m%n" pattern.

+ *

Sample output follows.

+ *
+ * 176 [main] INFO examples.Sort - Populating an array of 2 elements in reverse order.
+ * 225 [main] INFO examples.SortAlgo - Entered the sort method.
+ * 304 [main] INFO examples.SortAlgo - Dump of integer array:
+ * 317 [main] INFO examples.SortAlgo - Element [0] = 0
+ * 331 [main] INFO examples.SortAlgo - Element [1] = 1
+ * 343 [main] INFO examples.Sort - The next log statement should be an error message.
+ * 346 [main] ERROR examples.SortAlgo - Tried to dump an uninitialized array.
+ *   at org.log4j.examples.SortAlgo.dump(SortAlgo.java:58)
+ *   at org.log4j.examples.Sort.main(Sort.java:64)
+ * 467 [main] INFO  examples.Sort - Exiting main method.
+ * 
+ * + *

This implementation is heavily inspired by + * Apache Commons Logging's SimpleLog.

+ * + * @author Ceki Gülcü + * @author Scott Sanders + * @author Rod Waldhoff + * @author Robert Burrell Donkin + * @author Cédrik LIME + */ +public class SimpleLogger extends MarkerIgnoringBase { + + private static final long serialVersionUID = -632788891211436180L; + private static final String CONFIGURATION_FILE = "simplelogger.properties"; + + private static long START_TIME = System.currentTimeMillis(); + private static final Properties SIMPLE_LOGGER_PROPS = new Properties(); + + private static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT; + private static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT; + private static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT; + private static final int LOG_LEVEL_WARN = LocationAwareLogger.WARN_INT; + private static final int LOG_LEVEL_ERROR = LocationAwareLogger.ERROR_INT; + + private static boolean INITIALIZED = false; + + private static int DEFAULT_LOG_LEVEL = LOG_LEVEL_INFO; + private static boolean SHOW_DATE_TIME = false; + private static String DATE_TIME_FORMAT_STR = null; + private static DateFormat DATE_FORMATTER = null; + private static boolean SHOW_THREAD_NAME = true; + private static boolean SHOW_LOG_NAME = true; + private static boolean SHOW_SHORT_LOG_NAME = false; + private static String LOG_FILE = "System.err"; + private static PrintStream TARGET_STREAM = null; + private static boolean LEVEL_IN_BRACKETS = false; + private static String WARN_LEVEL_STRING = "WARN"; + + + /** All system properties used by SimpleLogger start with this prefix */ + public static final String SYSTEM_PREFIX = "org.slf4j.simpleLogger."; + + public static final String DEFAULT_LOG_LEVEL_KEY = SYSTEM_PREFIX + "defaultLogLevel"; + public static final String SHOW_DATE_TIME_KEY = SYSTEM_PREFIX + "showDateTime"; + public static final String DATE_TIME_FORMAT_KEY = SYSTEM_PREFIX + "dateTimeFormat"; + public static final String SHOW_THREAD_NAME_KEY = SYSTEM_PREFIX + "showThreadName"; + public static final String SHOW_LOG_NAME_KEY = SYSTEM_PREFIX + "showLogName"; + public static final String SHOW_SHORT_LOG_NAME_KEY = SYSTEM_PREFIX + "showShortLogName"; + public static final String LOG_FILE_KEY = SYSTEM_PREFIX + "logFile"; + public static final String LEVEL_IN_BRACKETS_KEY = SYSTEM_PREFIX + "levelInBrackets"; + public static final String WARN_LEVEL_STRING_KEY = SYSTEM_PREFIX + "warnLevelString"; + + + public static final String LOG_KEY_PREFIX = SYSTEM_PREFIX + "log."; + + + private static String getStringProperty(String name) { + String prop = null; + try { + prop = System.getProperty(name); + } catch (SecurityException e) { + ; // Ignore + } + return (prop == null) ? SIMPLE_LOGGER_PROPS.getProperty(name) : prop; + } + + private static String getStringProperty(String name, String defaultValue) { + String prop = getStringProperty(name); + return (prop == null) ? defaultValue : prop; + } + + private static boolean getBooleanProperty(String name, boolean defaultValue) { + String prop = getStringProperty(name); + return (prop == null) ? defaultValue : "true".equalsIgnoreCase(prop); + } + + + // Initialize class attributes. + // Load properties file, if found. + // Override with system properties. + static void init() { + INITIALIZED = true; + loadProperties(); + + String defaultLogLevelString = getStringProperty(DEFAULT_LOG_LEVEL_KEY, null); + if (defaultLogLevelString != null) + DEFAULT_LOG_LEVEL = stringToLevel(defaultLogLevelString); + + SHOW_LOG_NAME = getBooleanProperty(SHOW_LOG_NAME_KEY, SHOW_LOG_NAME); + SHOW_SHORT_LOG_NAME = getBooleanProperty(SHOW_SHORT_LOG_NAME_KEY, SHOW_SHORT_LOG_NAME); + SHOW_DATE_TIME = getBooleanProperty(SHOW_DATE_TIME_KEY, SHOW_DATE_TIME); + SHOW_THREAD_NAME = getBooleanProperty(SHOW_THREAD_NAME_KEY, SHOW_THREAD_NAME); + DATE_TIME_FORMAT_STR = getStringProperty(DATE_TIME_FORMAT_KEY, DATE_TIME_FORMAT_STR); + LEVEL_IN_BRACKETS = getBooleanProperty(LEVEL_IN_BRACKETS_KEY, LEVEL_IN_BRACKETS); + WARN_LEVEL_STRING = getStringProperty(WARN_LEVEL_STRING_KEY, WARN_LEVEL_STRING); + + LOG_FILE = getStringProperty(LOG_FILE_KEY, LOG_FILE); + TARGET_STREAM = computeTargetStream(LOG_FILE); + + if (DATE_TIME_FORMAT_STR != null) { + try { + DATE_FORMATTER = new SimpleDateFormat(DATE_TIME_FORMAT_STR); + } catch (IllegalArgumentException e) { + Util.report("Bad date format in " + CONFIGURATION_FILE + "; will output relative time", e); + } + } + } + + + private static PrintStream computeTargetStream(String logFile) { + if ("System.err".equalsIgnoreCase(logFile)) + return System.err; + else if ("System.out".equalsIgnoreCase(logFile)) { + return System.out; + } else { + try { + FileOutputStream fos = new FileOutputStream(logFile); + PrintStream printStream = new PrintStream(fos); + return printStream; + } catch (FileNotFoundException e) { + Util.report("Could not open [" + logFile + "]. Defaulting to System.err", e); + return System.err; + } + } + } + + private static void loadProperties() { + // Add props from the resource simplelogger.properties + InputStream in = AccessController.doPrivileged( + new PrivilegedAction() { + public InputStream run() { + ClassLoader threadCL = Thread.currentThread().getContextClassLoader(); + if (threadCL != null) { + return threadCL.getResourceAsStream(CONFIGURATION_FILE); + } else { + return ClassLoader.getSystemResourceAsStream(CONFIGURATION_FILE); + } + } + }); + if (null != in) { + try { + SIMPLE_LOGGER_PROPS.load(in); + in.close(); + } catch (java.io.IOException e) { + // ignored + } + } + } + + /** The current log level */ + protected int currentLogLevel = LOG_LEVEL_INFO; + /** The short name of this simple log instance */ + private transient String shortLogName = null; + + /** + * Package access allows only {@link SimpleLoggerFactory} to instantiate + * SimpleLogger instances. + */ + SimpleLogger(String name) { + if (!INITIALIZED) { + init(); + } + this.name = name; + + String levelString = recursivelyComputeLevelString(); + if (levelString != null) { + this.currentLogLevel = stringToLevel(levelString); + } else { + this.currentLogLevel = DEFAULT_LOG_LEVEL; + } + } + + String recursivelyComputeLevelString() { + String tempName = name; + String levelString = null; + int indexOfLastDot = tempName.length(); + while ((levelString == null) && (indexOfLastDot > -1)) { + tempName = tempName.substring(0, indexOfLastDot); + levelString = getStringProperty(LOG_KEY_PREFIX + tempName, null); + indexOfLastDot = String.valueOf(tempName).lastIndexOf("."); + } + return levelString; + } + + private static int stringToLevel(String levelStr) { + if ("trace".equalsIgnoreCase(levelStr)) { + return LOG_LEVEL_TRACE; + } else if ("debug".equalsIgnoreCase(levelStr)) { + return LOG_LEVEL_DEBUG; + } else if ("info".equalsIgnoreCase(levelStr)) { + return LOG_LEVEL_INFO; + } else if ("warn".equalsIgnoreCase(levelStr)) { + return LOG_LEVEL_WARN; + } else if ("error".equalsIgnoreCase(levelStr)) { + return LOG_LEVEL_ERROR; + } + // assume INFO by default + return LOG_LEVEL_INFO; + } + + + /** + * This is our internal implementation for logging regular (non-parameterized) + * log messages. + * + * @param level One of the LOG_LEVEL_XXX constants defining the log level + * @param message The message itself + * @param t The exception whose stack trace should be logged + */ + private void log(int level, String message, Throwable t) { + if (!isLevelEnabled(level)) { + return; + } + + StringBuffer buf = new StringBuffer(32); + + // Append date-time if so configured + if (SHOW_DATE_TIME) { + if (DATE_FORMATTER != null) { + buf.append(getFormattedDate()); + buf.append(' '); + } else { + buf.append(System.currentTimeMillis() - START_TIME); + buf.append(' '); + } + } + + // Append current thread name if so configured + if (SHOW_THREAD_NAME) { + buf.append('['); + buf.append(Thread.currentThread().getName()); + buf.append("] "); + } + + if (LEVEL_IN_BRACKETS) buf.append('['); + + // Append a readable representation of the log level + switch (level) { + case LOG_LEVEL_TRACE: + buf.append("TRACE"); + break; + case LOG_LEVEL_DEBUG: + buf.append("DEBUG"); + break; + case LOG_LEVEL_INFO: + buf.append("INFO"); + break; + case LOG_LEVEL_WARN: + buf.append(WARN_LEVEL_STRING); + break; + case LOG_LEVEL_ERROR: + buf.append("ERROR"); + break; + } + if (LEVEL_IN_BRACKETS) buf.append(']'); + buf.append(' '); + + // Append the name of the log instance if so configured + if (SHOW_SHORT_LOG_NAME) { + if (shortLogName == null) shortLogName = computeShortName(); + buf.append(String.valueOf(shortLogName)).append(" - "); + } else if (SHOW_LOG_NAME) { + buf.append(String.valueOf(name)).append(" - "); + } + + // Append the message + buf.append(message); + + write(buf, t); + + } + + void write(StringBuffer buf, Throwable t) { + TARGET_STREAM.println(buf.toString()); + if (t != null) { + t.printStackTrace(TARGET_STREAM); + } + TARGET_STREAM.flush(); + } + + private String getFormattedDate() { + Date now = new Date(); + String dateText; + synchronized (DATE_FORMATTER) { + dateText = DATE_FORMATTER.format(now); + } + return dateText; + } + + private String computeShortName() { + return name.substring(name.lastIndexOf(".") + 1); + } + + /** + * For formatted messages, first substitute arguments and then log. + * + * @param level + * @param format + * @param arg1 + * @param arg2 + */ + private void formatAndLog(int level, String format, Object arg1, + Object arg2) { + if (!isLevelEnabled(level)) { + return; + } + FormattingTuple tp = MessageFormatter.format(format, arg1, arg2); + log(level, tp.getMessage(), tp.getThrowable()); + } + + /** + * For formatted messages, first substitute arguments and then log. + * + * @param level + * @param format + * @param arguments a list of 3 ore more arguments + */ + private void formatAndLog(int level, String format, Object... arguments) { + if (!isLevelEnabled(level)) { + return; + } + FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments); + log(level, tp.getMessage(), tp.getThrowable()); + } + + /** + * Is the given log level currently enabled? + * + * @param logLevel is this level enabled? + */ + protected boolean isLevelEnabled(int logLevel) { + // log level are numerically ordered so can use simple numeric + // comparison + return (logLevel >= currentLogLevel); + } + + /** Are {@code trace} messages currently enabled? */ + public boolean isTraceEnabled() { + return isLevelEnabled(LOG_LEVEL_TRACE); + } + + /** + * A simple implementation which logs messages of level TRACE according + * to the format outlined above. + */ + public void trace(String msg) { + log(LOG_LEVEL_TRACE, msg, null); + } + + /** + * Perform single parameter substitution before logging the message of level + * TRACE according to the format outlined above. + */ + public void trace(String format, Object param1) { + formatAndLog(LOG_LEVEL_TRACE, format, param1, null); + } + + /** + * Perform double parameter substitution before logging the message of level + * TRACE according to the format outlined above. + */ + public void trace(String format, Object param1, Object param2) { + formatAndLog(LOG_LEVEL_TRACE, format, param1, param2); + } + + /** + * Perform double parameter substitution before logging the message of level + * TRACE according to the format outlined above. + */ + public void trace(String format, Object... argArray) { + formatAndLog(LOG_LEVEL_TRACE, format, argArray); + } + + /** Log a message of level TRACE, including an exception. */ + public void trace(String msg, Throwable t) { + log(LOG_LEVEL_TRACE, msg, t); + } + + /** Are {@code debug} messages currently enabled? */ + public boolean isDebugEnabled() { + return isLevelEnabled(LOG_LEVEL_DEBUG); + } + + /** + * A simple implementation which logs messages of level DEBUG according + * to the format outlined above. + */ + public void debug(String msg) { + log(LOG_LEVEL_DEBUG, msg, null); + } + + /** + * Perform single parameter substitution before logging the message of level + * DEBUG according to the format outlined above. + */ + public void debug(String format, Object param1) { + formatAndLog(LOG_LEVEL_DEBUG, format, param1, null); + } + + /** + * Perform double parameter substitution before logging the message of level + * DEBUG according to the format outlined above. + */ + public void debug(String format, Object param1, Object param2) { + formatAndLog(LOG_LEVEL_DEBUG, format, param1, param2); + } + + /** + * Perform double parameter substitution before logging the message of level + * DEBUG according to the format outlined above. + */ + public void debug(String format, Object... argArray) { + formatAndLog(LOG_LEVEL_DEBUG, format, argArray); + } + + /** Log a message of level DEBUG, including an exception. */ + public void debug(String msg, Throwable t) { + log(LOG_LEVEL_DEBUG, msg, t); + } + + /** Are {@code info} messages currently enabled? */ + public boolean isInfoEnabled() { + return isLevelEnabled(LOG_LEVEL_INFO); + } + + /** + * A simple implementation which logs messages of level INFO according + * to the format outlined above. + */ + public void info(String msg) { + log(LOG_LEVEL_INFO, msg, null); + } + + /** + * Perform single parameter substitution before logging the message of level + * INFO according to the format outlined above. + */ + public void info(String format, Object arg) { + formatAndLog(LOG_LEVEL_INFO, format, arg, null); + } + + /** + * Perform double parameter substitution before logging the message of level + * INFO according to the format outlined above. + */ + public void info(String format, Object arg1, Object arg2) { + formatAndLog(LOG_LEVEL_INFO, format, arg1, arg2); + } + + /** + * Perform double parameter substitution before logging the message of level + * INFO according to the format outlined above. + */ + public void info(String format, Object... argArray) { + formatAndLog(LOG_LEVEL_INFO, format, argArray); + } + + /** Log a message of level INFO, including an exception. */ + public void info(String msg, Throwable t) { + log(LOG_LEVEL_INFO, msg, t); + } + + /** Are {@code warn} messages currently enabled? */ + public boolean isWarnEnabled() { + return isLevelEnabled(LOG_LEVEL_WARN); + } + + /** + * A simple implementation which always logs messages of level WARN according + * to the format outlined above. + */ + public void warn(String msg) { + log(LOG_LEVEL_WARN, msg, null); + } + + /** + * Perform single parameter substitution before logging the message of level + * WARN according to the format outlined above. + */ + public void warn(String format, Object arg) { + formatAndLog(LOG_LEVEL_WARN, format, arg, null); + } + + /** + * Perform double parameter substitution before logging the message of level + * WARN according to the format outlined above. + */ + public void warn(String format, Object arg1, Object arg2) { + formatAndLog(LOG_LEVEL_WARN, format, arg1, arg2); + } + + /** + * Perform double parameter substitution before logging the message of level + * WARN according to the format outlined above. + */ + public void warn(String format, Object... argArray) { + formatAndLog(LOG_LEVEL_WARN, format, argArray); + } + + /** Log a message of level WARN, including an exception. */ + public void warn(String msg, Throwable t) { + log(LOG_LEVEL_WARN, msg, t); + } + + /** Are {@code error} messages currently enabled? */ + public boolean isErrorEnabled() { + return isLevelEnabled(LOG_LEVEL_ERROR); + } + + /** + * A simple implementation which always logs messages of level ERROR according + * to the format outlined above. + */ + public void error(String msg) { + log(LOG_LEVEL_ERROR, msg, null); + } + + /** + * Perform single parameter substitution before logging the message of level + * ERROR according to the format outlined above. + */ + public void error(String format, Object arg) { + formatAndLog(LOG_LEVEL_ERROR, format, arg, null); + } + + /** + * Perform double parameter substitution before logging the message of level + * ERROR according to the format outlined above. + */ + public void error(String format, Object arg1, Object arg2) { + formatAndLog(LOG_LEVEL_ERROR, format, arg1, arg2); + } + + /** + * Perform double parameter substitution before logging the message of level + * ERROR according to the format outlined above. + */ + public void error(String format, Object... argArray) { + formatAndLog(LOG_LEVEL_ERROR, format, argArray); + } + + /** Log a message of level ERROR, including an exception. */ + public void error(String msg, Throwable t) { + log(LOG_LEVEL_ERROR, msg, t); + } +} diff --git a/slf4j-simple/src/test/resources/simplelogger.properties b/slf4j-simple/src/test/resources/simplelogger.properties index 208edac6..5fdec061 100644 --- a/slf4j-simple/src/test/resources/simplelogger.properties +++ b/slf4j-simple/src/test/resources/simplelogger.properties @@ -1,34 +1,34 @@ -# SLF4J's SimpleLogger configuration file -# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err. - -# Default logging detail level for all instances of SimpleLogger. -# Must be one of ("trace", "debug", "info", "warn", or "error"). -# If not specified, defaults to "info". -#org.slf4j.simpleLogger.defaultLogLevel=info - -# Logging detail level for a SimpleLogger instance named "xxxxx". -# Must be one of ("trace", "debug", "info", "warn", or "error"). -# If not specified, the default logging detail level is used. -#org.slf4j.simpleLogger.log.xxxxx= - -# Set to true if you want the current date and time to be included in output messages. -# Default is false, and will output the number of milliseconds elapsed since startup. -#org.slf4j.simpleLogger.showDateTime=false - -# The date and time format to be used in the output messages. -# The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat. -# If the format is not specified or is invalid, the default format is used. -# The default format is yyyy-MM-dd HH:mm:ss:SSS Z. -#org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z - -# Set to true if you want to output the current thread name. -# Defaults to true. -#org.slf4j.simpleLogger.showThreadName=true - -# Set to true if you want the Logger instance name to be included in output messages. -# Defaults to true. -#org.slf4j.simpleLogger.showLogName=true - -# Set to true if you want the last component of the name to be included in output messages. -# Defaults to false. -#org.slf4j.simpleLogger.showShortLogName=false +# SLF4J's SimpleLogger configuration file +# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err. + +# Default logging detail level for all instances of SimpleLogger. +# Must be one of ("trace", "debug", "info", "warn", or "error"). +# If not specified, defaults to "info". +#org.slf4j.simpleLogger.defaultLogLevel=info + +# Logging detail level for a SimpleLogger instance named "xxxxx". +# Must be one of ("trace", "debug", "info", "warn", or "error"). +# If not specified, the default logging detail level is used. +#org.slf4j.simpleLogger.log.xxxxx= + +# Set to true if you want the current date and time to be included in output messages. +# Default is false, and will output the number of milliseconds elapsed since startup. +#org.slf4j.simpleLogger.showDateTime=false + +# The date and time format to be used in the output messages. +# The pattern describing the date and time format is the same that is used in java.text.SimpleDateFormat. +# If the format is not specified or is invalid, the default format is used. +# The default format is yyyy-MM-dd HH:mm:ss:SSS Z. +#org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z + +# Set to true if you want to output the current thread name. +# Defaults to true. +#org.slf4j.simpleLogger.showThreadName=true + +# Set to true if you want the Logger instance name to be included in output messages. +# Defaults to true. +#org.slf4j.simpleLogger.showLogName=true + +# Set to true if you want the last component of the name to be included in output messages. +# Defaults to false. +#org.slf4j.simpleLogger.showShortLogName=false