Package com.jcabi.log

Class Logger

java.lang.Object
com.jcabi.log.Logger

public final class Logger extends Object
Universal logger, and adapter between your app and SLF4J API.

Instead of relying on some logging engine you can use this class, which transforms all messages to SLF4J. This approach gives you a perfect decoupling of business logic and logging mechanism. All methods in the class are called statically, without the necessity to instantiate the class.

Use it like this in any class, and in any package:

 package com.example.XXX;
 import com.jcabi.log.Logger;
 public class MyClass {
   public void foo(Integer num) {
     Logger.info(this, "foo(%d) just called", num);
   }
 }

Or statically (pay attention to MyClass.class):

 public class MyClass {
   public static void foo(Integer num) {
     Logger.info(MyClass.class, "foo(%d) just called", num);
   }
 }

Exact binding between SLF4J and logging facility has to be specified in pom.xml of your project (or in classpath directly).

For performance reasons in most cases before sending a TRACE or DEBUG log message you may check whether this logging level is enabled in the project, e.g.:

 //...
 if (Logger.isTraceEnabled(this)) {
   Logger.trace(this, "#foo() called");
 }
 //...

There is only one reason to do so - if you want to save time spent on preparing of the arguments. By default, such a call is made inside every method of Logger class.

Since:
0.1
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    debug(Object source, String msg)
    Protocol one message, with DEBUG priority level.
    static void
    debug(Object source, String msg, Object... args)
    Protocol one message, with DEBUG priority level.
    static void
    debugForced(Object source, String msg, Object... args)
    Protocol one message, with DEBUG priority level without internal checking whether DEBUG level is enabled.
    static void
    error(Object source, String msg)
    Protocol one message, with ERROR priority level.
    static void
    error(Object source, String msg, Object... args)
    Protocol one message, with ERROR priority level.
    static String
    format(String fmt, Object... args)
    Format one string.
    static void
    info(Object source, String msg)
    Protocol one message, with INFO priority level.
    static void
    info(Object source, String msg, Object... args)
    Protocol one message, with INFO priority level.
    static void
    infoForced(Object source, String msg, Object... args)
    Protocol one message, with INFO priority level without internal checking whether INFO level is enabled.
    static boolean
    Validates whether DEBUG priority level is enabled for this particular logger.
    static boolean
    isEnabled(Level level, Object source)
    Is the given logging level enabled?
    static boolean
    Validates whether INFO priority level is enabled for this particular logger.
    static boolean
    Validates whether TRACE priority level is enabled for this particular logger.
    static boolean
    Validates whether INFO priority level is enabled for this particular logger.
    static void
    log(Level level, Object source, String msg, Object... args)
    Log one line using the logging level specified.
    stream(Level level, Object source)
    Returns an OutputStream, which converts all incoming data into logging lines (separated by \x0A in UTF-8).
    static void
    trace(Object source, String msg)
    Protocol one message, with TRACE priority level.
    static void
    trace(Object source, String msg, Object... args)
    Protocol one message, with TRACE priority level.
    static void
    traceForced(Object source, String msg, Object... args)
    Protocol one message, with TRACE priority level without internal checking whether TRACE level is enabled.
    static void
    warn(Object source, String msg)
    Protocol one message, with WARN priority level.
    static void
    warn(Object source, String msg, Object... args)
    Protocol one message, with WARN priority level.
    static void
    warnForced(Object source, String msg, Object... args)
    Protocol one message, with WARN priority level without internal checking whether WARN level is enabled.
    static com.jcabi.log.SupplierLogger
    Log messages constructed from Suppliers.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • format

      public static String format(String fmt, Object... args)
      Format one string.
      Parameters:
      fmt - The format
      args - List of arbitrary arguments
      Returns:
      Formatted string
    • trace

      public static void trace(Object source, String msg)
      Protocol one message, with TRACE priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged
      Since:
      0.7.11
    • trace

      public static void trace(Object source, String msg, Object... args)
      Protocol one message, with TRACE priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • traceForced

      public static void traceForced(Object source, String msg, Object... args)
      Protocol one message, with TRACE priority level without internal checking whether TRACE level is enabled.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • debug

      public static void debug(Object source, String msg)
      Protocol one message, with DEBUG priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      Since:
      0.7.11
    • debug

      public static void debug(Object source, String msg, Object... args)
      Protocol one message, with DEBUG priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • debugForced

      public static void debugForced(Object source, String msg, Object... args)
      Protocol one message, with DEBUG priority level without internal checking whether DEBUG level is enabled.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • info

      public static void info(Object source, String msg)
      Protocol one message, with INFO priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged
      Since:
      0.7.11
    • info

      public static void info(Object source, String msg, Object... args)
      Protocol one message, with INFO priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • infoForced

      public static void infoForced(Object source, String msg, Object... args)
      Protocol one message, with INFO priority level without internal checking whether INFO level is enabled.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • warn

      public static void warn(Object source, String msg)
      Protocol one message, with WARN priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged
      Since:
      0.7.11
    • warn

      public static void warn(Object source, String msg, Object... args)
      Protocol one message, with WARN priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • warnForced

      public static void warnForced(Object source, String msg, Object... args)
      Protocol one message, with WARN priority level without internal checking whether WARN level is enabled.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • error

      public static void error(Object source, String msg)
      Protocol one message, with ERROR priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged
      Since:
      0.7.11
    • error

      public static void error(Object source, String msg, Object... args)
      Protocol one message, with ERROR priority level.
      Parameters:
      source - The source of the logging operation
      msg - The text message to be logged, with meta-tags
      args - List of arguments
    • isTraceEnabled

      public static boolean isTraceEnabled(Object source)
      Validates whether TRACE priority level is enabled for this particular logger.
      Parameters:
      source - The source of the logging operation
      Returns:
      Is it enabled?
    • isDebugEnabled

      public static boolean isDebugEnabled(Object source)
      Validates whether DEBUG priority level is enabled for this particular logger.
      Parameters:
      source - The source of the logging operation
      Returns:
      Is it enabled?
    • isInfoEnabled

      public static boolean isInfoEnabled(Object source)
      Validates whether INFO priority level is enabled for this particular logger.
      Parameters:
      source - The source of the logging operation
      Returns:
      Is it enabled?
      Since:
      0.5
    • isWarnEnabled

      public static boolean isWarnEnabled(Object source)
      Validates whether INFO priority level is enabled for this particular logger.
      Parameters:
      source - The source of the logging operation
      Returns:
      Is it enabled?
      Since:
      0.5
    • isEnabled

      public static boolean isEnabled(Level level, Object source)
      Is the given logging level enabled?
      Parameters:
      level - The level of logging
      source - The source of the logging operation
      Returns:
      Is it enabled?
      Since:
      0.13
    • log

      public static void log(Level level, Object source, String msg, Object... args)
      Log one line using the logging level specified.
      Parameters:
      level - The level of logging
      source - The source of the logging operation
      msg - The text message to be logged
      args - Optional arguments for string formatting
      Since:
      0.8
      Suppressed Checkstyle violations:
      ParameterNumber (4 lines)
    • stream

      public static OutputStream stream(Level level, Object source)
      Returns an OutputStream, which converts all incoming data into logging lines (separated by \x0A in UTF-8).
      Parameters:
      level - The level of logging
      source - The source of the logging operation
      Returns:
      Output stream directly pointed to the logging facility
      Since:
      0.8
      See Also:
    • withSupplier

      public static com.jcabi.log.SupplierLogger withSupplier()
      Log messages constructed from Suppliers. It is more efficient to use method referencing because the method won't be called unless the specified logging level is enabled. This saves you the effort of having to check if the level is enabled before calling the logging method. E.g.
           if (Logger.isDebugEnabled(this)) {
               Logger.debug(this, "Some %s", calculate());
           }
       

      turns into
           Logger.withSupplier().debug(this, "Some %s", this::calculate());
       

      and the calculate() method won't be called unless the debug level is active.
      Returns:
      Object containing methods for logging with Supplier-constructed messages