Package org.slf4j

Interface Logger

All Known Subinterfaces:
LocationAwareLogger
All Known Implementing Classes:
AbstractLogger, EventRecordingLogger, JDK14LoggerAdapter, LegacyAbstractLogger, LocLogger, LoggerWrapper, MarkerIgnoringBase, NOPLogger, Reload4jLoggerAdapter, SimpleLogger, SubstituteLogger, XLogger

public interface Logger
The org.slf4j.Logger interface is the main user entry point of SLF4J API. It is expected that logging takes place through concrete implementations of this interface.

Typical usage pattern:

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;

 public class Wombat {

   final static Logger logger = LoggerFactory.getLogger(Wombat.class);
   Integer t;
   Integer oldT;

   public void setTemperature(Integer temperature) {
     oldT = t;
     t = temperature;
     logger.debug("Temperature set to {}. Old temperature was {}.", t, oldT);
     if (temperature.intValue() > 50) {
       logger.info("Temperature has risen above 50 degrees.");
     }
   }
 }
 

Note that version 2.0 of the SLF4J API introduces a fluent api, the most significant API change to occur in the last 20 years.

Be sure to read the FAQ entry relating to parameterized logging. Note that logging statements can be parameterized in presence of an exception/throwable.

Once you are comfortable using loggers, i.e. instances of this interface, consider using MDC as well as Markers.

Author:
Ceki Gülcü
  • Field Details

  • Method Details

    • getName

      Return the name of this Logger instance.
      Returns:
      name of this logger instance
    • makeLoggingEventBuilder

      Make a new LoggingEventBuilder instance as appropriate for this logger implementation. This default implementation always returns a new instance of DefaultLoggingEventBuilder.

      This method is intended to be used by logging systems implementing the SLF4J API and not by end users.

      Also note that a LoggingEventBuilder instance should be built for all levels, independently of the level argument. In other words, this method is an unconditional constructor for the LoggingEventBuilder appropriate for this logger implementation.

      Parameters:
      level - desired level for the event builder
      Returns:
      a new LoggingEventBuilder instance as appropriate for this logger
      Since:
      2.0
    • atLevel

      Make a new LoggingEventBuilder instance as appropriate for this logger and the desired Level passed as parameter. If this Logger is disabled for the given Level, then a NOPLoggingEventBuilder is returned. This is the main optimization in the fluent API.

      Parameters:
      level - desired level for the event builder
      Returns:
      a new LoggingEventBuilder instance as appropriate for this logger
      Since:
      2.0
    • isEnabledForLevel

      default boolean isEnabledForLevel(Level level)
      Returns whether this Logger is enabled for a given Level.
      Parameters:
      level -
      Returns:
      true if enabled, false otherwise.
    • isTraceEnabled

      boolean isTraceEnabled()
      Is the logger instance enabled for the TRACE level?
      Returns:
      True if this Logger is enabled for the TRACE level, false otherwise.
      Since:
      1.4
    • trace

      void trace(String msg)
      Log a message at the TRACE level.
      Parameters:
      msg - the message string to be logged
      Since:
      1.4
    • trace

      void trace(String format, Object arg)
      Log a message at the TRACE level according to the specified format and argument.

      This form avoids superfluous object creation when the logger is disabled for the TRACE level.

      Parameters:
      format - the format string
      arg - the argument
      Since:
      1.4
    • trace

      void trace(String format, Object arg1, Object arg2)
      Log a message at the TRACE level according to the specified format and arguments.

      This form avoids superfluous object creation when the logger is disabled for the TRACE level.

      Parameters:
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
      Since:
      1.4
    • trace

      void trace(String format, Object... arguments)
      Log a message at the TRACE level according to the specified format and arguments.

      This form avoids superfluous string concatenation when the logger is disabled for the TRACE level. However, this variant incurs the hidden (and relatively small) cost of creating an Object[] before invoking the method, even if this logger is disabled for TRACE. The variants taking one and two arguments exist solely in order to avoid this hidden cost.

      Parameters:
      format - the format string
      arguments - a list of 3 or more arguments
      Since:
      1.4
    • trace

      void trace(String msg, Throwable t)
      Log an exception (throwable) at the TRACE level with an accompanying message.
      Parameters:
      msg - the message accompanying the exception
      t - the exception (throwable) to log
      Since:
      1.4
    • isTraceEnabled

      boolean isTraceEnabled(Marker marker)
      Similar to isTraceEnabled() method except that the marker data is also taken into account.
      Parameters:
      marker - The marker data to take into consideration
      Returns:
      True if this Logger is enabled for the TRACE level, false otherwise.
      Since:
      1.4
    • atTrace

      Entry point for fluent-logging for Level.TRACE level.

      If this logger is disabled for the TRACE level, then a NOPLoggingEventBuilder instance is returned. As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.

      Returns:
      LoggingEventBuilder instance as appropriate for level TRACE
      Since:
      2.0
    • trace

      void trace(Marker marker, String msg)
      Log a message with the specific Marker at the TRACE level.
      Parameters:
      marker - the marker data specific to this log statement
      msg - the message string to be logged
      Since:
      1.4
    • trace

      void trace(Marker marker, String format, Object arg)
      This method is similar to trace(String, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg - the argument
      Since:
      1.4
    • trace

      void trace(Marker marker, String format, Object arg1, Object arg2)
      This method is similar to trace(String, Object, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
      Since:
      1.4
    • trace

      void trace(Marker marker, String format, Object... argArray)
      This method is similar to trace(String, Object...) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      argArray - an array of arguments
      Since:
      1.4
    • trace

      void trace(Marker marker, String msg, Throwable t)
      This method is similar to trace(String, Throwable) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      msg - the message accompanying the exception
      t - the exception (throwable) to log
      Since:
      1.4
    • isDebugEnabled

      boolean isDebugEnabled()
      Is the logger instance enabled for the DEBUG level?
      Returns:
      True if this Logger is enabled for the DEBUG level, false otherwise.
    • debug

      void debug(String msg)
      Log a message at the DEBUG level.
      Parameters:
      msg - the message string to be logged
    • debug

      void debug(String format, Object arg)
      Log a message at the DEBUG level according to the specified format and argument.

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

      Parameters:
      format - the format string
      arg - the argument
    • debug

      void debug(String format, Object arg1, Object arg2)
      Log a message at the DEBUG level according to the specified format and arguments.

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

      Parameters:
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
    • debug

      void debug(String format, Object... arguments)
      Log a message at the DEBUG level according to the specified format and arguments.

      This form avoids superfluous string concatenation when the logger is disabled for the DEBUG level. However, this variant incurs the hidden (and relatively small) cost of creating an Object[] before invoking the method, even if this logger is disabled for DEBUG. The variants taking one and two arguments exist solely in order to avoid this hidden cost.

      Parameters:
      format - the format string
      arguments - a list of 3 or more arguments
    • debug

      void debug(String msg, Throwable t)
      Log an exception (throwable) at the DEBUG level with an accompanying message.
      Parameters:
      msg - the message accompanying the exception
      t - the exception (throwable) to log
    • isDebugEnabled

      boolean isDebugEnabled(Marker marker)
      Similar to isDebugEnabled() method except that the marker data is also taken into account.
      Parameters:
      marker - The marker data to take into consideration
      Returns:
      True if this Logger is enabled for the DEBUG level, false otherwise.
    • debug

      void debug(Marker marker, String msg)
      Log a message with the specific Marker at the DEBUG level.
      Parameters:
      marker - the marker data specific to this log statement
      msg - the message string to be logged
    • debug

      void debug(Marker marker, String format, Object arg)
      This method is similar to debug(String, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg - the argument
    • debug

      void debug(Marker marker, String format, Object arg1, Object arg2)
      This method is similar to debug(String, Object, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
    • debug

      void debug(Marker marker, String format, Object... arguments)
      This method is similar to debug(String, Object...) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arguments - a list of 3 or more arguments
    • debug

      void debug(Marker marker, String msg, Throwable t)
      This method is similar to debug(String, Throwable) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      msg - the message accompanying the exception
      t - the exception (throwable) to log
    • atDebug

      Entry point for fluent-logging for Level.DEBUG level.

      If this logger is disabled for the DEBUG level, then a NOPLoggingEventBuilder instance is returned. As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.

      Returns:
      LoggingEventBuilder instance as appropriate for level DEBUG
      Since:
      2.0
    • isInfoEnabled

      boolean isInfoEnabled()
      Is the logger instance enabled for the INFO level?
      Returns:
      True if this Logger is enabled for the INFO level, false otherwise.
    • info

      void info(String msg)
      Log a message at the INFO level.
      Parameters:
      msg - the message string to be logged
    • info

      void info(String format, Object arg)
      Log a message at the INFO level according to the specified format and argument.

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

      Parameters:
      format - the format string
      arg - the argument
    • info

      void info(String format, Object arg1, Object arg2)
      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.

      Parameters:
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
    • info

      void info(String format, Object... arguments)
      Log a message at the INFO level according to the specified format and arguments.

      This form avoids superfluous string concatenation when the logger is disabled for the INFO level. However, this variant incurs the hidden (and relatively small) cost of creating an Object[] before invoking the method, even if this logger is disabled for INFO. The variants taking one and two arguments exist solely in order to avoid this hidden cost.

      Parameters:
      format - the format string
      arguments - a list of 3 or more arguments
    • info

      void info(String msg, Throwable t)
      Log an exception (throwable) at the INFO level with an accompanying message.
      Parameters:
      msg - the message accompanying the exception
      t - the exception (throwable) to log
    • isInfoEnabled

      boolean isInfoEnabled(Marker marker)
      Similar to isInfoEnabled() method except that the marker data is also taken into consideration.
      Parameters:
      marker - The marker data to take into consideration
      Returns:
      true if this Logger is enabled for the INFO level, false otherwise.
    • info

      void info(Marker marker, String msg)
      Log a message with the specific Marker at the INFO level.
      Parameters:
      marker - The marker specific to this log statement
      msg - the message string to be logged
    • info

      void info(Marker marker, String format, Object arg)
      This method is similar to info(String, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg - the argument
    • info

      void info(Marker marker, String format, Object arg1, Object arg2)
      This method is similar to info(String, Object, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
    • info

      void info(Marker marker, String format, Object... arguments)
      This method is similar to info(String, Object...) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arguments - a list of 3 or more arguments
    • info

      void info(Marker marker, String msg, Throwable t)
      This method is similar to info(String, Throwable) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data for this log statement
      msg - the message accompanying the exception
      t - the exception (throwable) to log
    • atInfo

      Entry point for fluent-logging for Level.INFO level.

      If this logger is disabled for the INFO level, then a NOPLoggingEventBuilder instance is returned. As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.

      Returns:
      LoggingEventBuilder instance as appropriate for level INFO
      Since:
      2.0
    • isWarnEnabled

      boolean isWarnEnabled()
      Is the logger instance enabled for the WARN level?
      Returns:
      True if this Logger is enabled for the WARN level, false otherwise.
    • warn

      void warn(String msg)
      Log a message at the WARN level.
      Parameters:
      msg - the message string to be logged
    • warn

      void warn(String format, Object arg)
      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.

      Parameters:
      format - the format string
      arg - the argument
    • warn

      void warn(String format, Object... arguments)
      Log a message at the WARN level according to the specified format and arguments.

      This form avoids superfluous string concatenation when the logger is disabled for the WARN level. However, this variant incurs the hidden (and relatively small) cost of creating an Object[] before invoking the method, even if this logger is disabled for WARN. The variants taking one and two arguments exist solely in order to avoid this hidden cost.

      Parameters:
      format - the format string
      arguments - a list of 3 or more arguments
    • warn

      void warn(String format, Object arg1, Object arg2)
      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.

      Parameters:
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
    • warn

      void warn(String msg, Throwable t)
      Log an exception (throwable) at the WARN level with an accompanying message.
      Parameters:
      msg - the message accompanying the exception
      t - the exception (throwable) to log
    • isWarnEnabled

      boolean isWarnEnabled(Marker marker)
      Similar to isWarnEnabled() method except that the marker data is also taken into consideration.
      Parameters:
      marker - The marker data to take into consideration
      Returns:
      True if this Logger is enabled for the WARN level, false otherwise.
    • warn

      void warn(Marker marker, String msg)
      Log a message with the specific Marker at the WARN level.
      Parameters:
      marker - The marker specific to this log statement
      msg - the message string to be logged
    • warn

      void warn(Marker marker, String format, Object arg)
      This method is similar to warn(String, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg - the argument
    • warn

      void warn(Marker marker, String format, Object arg1, Object arg2)
      This method is similar to warn(String, Object, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
    • warn

      void warn(Marker marker, String format, Object... arguments)
      This method is similar to warn(String, Object...) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arguments - a list of 3 or more arguments
    • warn

      void warn(Marker marker, String msg, Throwable t)
      This method is similar to warn(String, Throwable) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data for this log statement
      msg - the message accompanying the exception
      t - the exception (throwable) to log
    • atWarn

      Entry point for fluent-logging for Level.WARN level.

      If this logger is disabled for the WARN level, then a NOPLoggingEventBuilder instance is returned. As the name indicates, this builder does not perform any operations. This is the main optimization in the fluent API.

      Returns:
      LoggingEventBuilder instance as appropriate for level WARN
      Since:
      2.0
    • isErrorEnabled

      boolean isErrorEnabled()
      Is the logger instance enabled for the ERROR level?
      Returns:
      True if this Logger is enabled for the ERROR level, false otherwise.
    • error

      void error(String msg)
      Log a message at the ERROR level.
      Parameters:
      msg - the message string to be logged
    • error

      void error(String format, Object arg)
      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.

      Parameters:
      format - the format string
      arg - the argument
    • error

      void error(String format, Object arg1, Object arg2)
      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.

      Parameters:
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
    • error

      void error(String format, Object... arguments)
      Log a message at the ERROR level according to the specified format and arguments.

      This form avoids superfluous string concatenation when the logger is disabled for the ERROR level. However, this variant incurs the hidden (and relatively small) cost of creating an Object[] before invoking the method, even if this logger is disabled for ERROR. The variants taking one and two arguments exist solely in order to avoid this hidden cost.

      Parameters:
      format - the format string
      arguments - a list of 3 or more arguments
    • error

      void error(String msg, Throwable t)
      Log an exception (throwable) at the ERROR level with an accompanying message.
      Parameters:
      msg - the message accompanying the exception
      t - the exception (throwable) to log
    • isErrorEnabled

      boolean isErrorEnabled(Marker marker)
      Similar to isErrorEnabled() method except that the marker data is also taken into consideration.
      Parameters:
      marker - The marker data to take into consideration
      Returns:
      True if this Logger is enabled for the ERROR level, false otherwise.
    • error

      void error(Marker marker, String msg)
      Log a message with the specific Marker at the ERROR level.
      Parameters:
      marker - The marker specific to this log statement
      msg - the message string to be logged
    • error

      void error(Marker marker, String format, Object arg)
      This method is similar to error(String, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg - the argument
    • error

      void error(Marker marker, String format, Object arg1, Object arg2)
      This method is similar to error(String, Object, Object) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arg1 - the first argument
      arg2 - the second argument
    • error

      void error(Marker marker, String format, Object... arguments)
      This method is similar to error(String, Object...) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      format - the format string
      arguments - a list of 3 or more arguments
    • error

      void error(Marker marker, String msg, Throwable t)
      This method is similar to error(String, Throwable) method except that the marker data is also taken into consideration.
      Parameters:
      marker - the marker data specific to this log statement
      msg - the message accompanying the exception
      t - the exception (throwable) to log
    • atError

      Entry point for fluent-logging for Level.ERROR level.

      If this logger is disabled for the ERROR level, then a NOPLoggingEventBuilder instance is returned. As the name indicates, this builder does not perform any operations.

      Returns:
      LoggingEventBuilder instance as appropriate for level ERROR
      Since:
      2.0