Class MdcLogger

java.lang.Object
io.bdeploy.common.util.MdcLogger

public class MdcLogger extends Object
Utility class that simplifies writing log statements with MDC data.

The logger is typically initialized with a fixed MDC value that is attached to all log calls. If required additional data can be passed to each log call.

Typical usage pattern:
  public class MyClass {
      private final MdcLogger logger = new MdcLogger(MyClass.class);

      public MyClass() {
          logger.setMdc("green");
      }

      public void foo() {
          logger.log((l) -> l.info("Doing some work."));

          logger.log((l) -> l.info("Doing more work."),"red");
      }
  }

  The following output will be printed when a pattern layout is used:
       %-12.12MDC{BDEPLOY} | %-5level | %-20msg - (%F)%n

       green       | INFO | Doing some work.  - (MyClass.java)
       green / red | INFO | Doing more work.  - (MyClass.java)
 

The actual log call is delegated to a functional interface so that the line numbers and the class is preserved in the log statements. As consequence it is also up the the caller to check if the desired log level is enabled.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The key to which all MDC data is associated with.
  • Constructor Summary

    Constructors
    Constructor
    Description
    MdcLogger(Class<?> clazz)
    Creates a new instance using the given logger.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    log(Consumer<org.slf4j.Logger> writer)
    Writes the desired log statement.
    void
    log(Consumer<org.slf4j.Logger> writer, Object... mdcData)
    Writes the desired log statement.
    void
    setMdcValue(Object... value)
    Sets the MDC value that will be associated with all subsequent log calls.

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • MdcLogger

      public MdcLogger(Class<?> clazz)
      Creates a new instance using the given logger.
      Parameters:
      clazz - the class used by the logger
  • Method Details

    • setMdcValue

      public void setMdcValue(Object... value)
      Sets the MDC value that will be associated with all subsequent log calls.
      Parameters:
      value - the value to associate with the MDC key.
    • log

      public void log(Consumer<org.slf4j.Logger> writer)
      Writes the desired log statement. The previously configured MDC value will be set during the logger is called.
      Parameters:
      writer - the logger to be used to write the log statement
    • log

      public void log(Consumer<org.slf4j.Logger> writer, Object... mdcData)
      Writes the desired log statement. The given MDC data is added to the already defined MDC data. Typically used when there is a base MDC value that should be present in all statements but in some additional values are required.
      Parameters:
      writer - logger to be used to write the log statement
      mdcData - data to associate with the MDC key