/* * 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); } }