Class LogUtil


  • public final class LogUtil
    extends Object
    Utilities for logging.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  LogUtil.MdcField
      Represents an MDC field that can be added to the MDC of a logger.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static String X_CORRELATION_ID
      Name of the Header for the global Ditto correlation ID.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void enhanceLogWithCorrelationId​(akka.event.DiagnosticLoggingAdapter loggingAdapter, CharSequence correlationId, LogUtil.MdcField... additionalMdcFields)
      Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry for the passed correlationId (if present).
      static void enhanceLogWithCorrelationId​(akka.event.DiagnosticLoggingAdapter loggingAdapter, Optional<String> correlationId, LogUtil.MdcField... additionalMdcFields)
      Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry for the passed correlationId (if present).
      static void enhanceLogWithCorrelationId​(akka.event.DiagnosticLoggingAdapter loggingAdapter, org.eclipse.ditto.model.base.headers.DittoHeaders dittoHeaders, LogUtil.MdcField... additionalMdcFields)
      Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry by extracting a correlationId of the passed dittoHeaders (if present).
      static void enhanceLogWithCorrelationId​(akka.event.DiagnosticLoggingAdapter loggingAdapter, org.eclipse.ditto.model.base.headers.WithDittoHeaders<?> withDittoHeaders, LogUtil.MdcField... additionalMdcFields)
      Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry by extracting a correlationId of the passed withDittoHeaders (if present).
      static void enhanceLogWithCorrelationId​(CharSequence correlationId)
      Enhances the default slf4j MDC with a correlationId.
      static void enhanceLogWithCorrelationId​(Optional<String> correlationId)
      Enhances the default slf4j MDC with a correlationId.
      static void enhanceLogWithCorrelationId​(org.eclipse.ditto.model.base.headers.DittoHeaders dittoHeaders)
      Enhances the default slf4j MDC with a correlationId of the passed dittoHeaders (if present).
      static void enhanceLogWithCorrelationId​(org.eclipse.ditto.model.base.headers.WithDittoHeaders<?> withDittoHeaders)
      Enhances the default slf4j MDC with a correlationId of the passed withDittoHeaders (if present).
      static void enhanceLogWithCorrelationIdOrRandom​(org.eclipse.ditto.model.base.headers.DittoHeaders dittoHeaders)
      Enhances the default slf4j MDC with a correlationId of the passed dittoHeaders (if present), else a random correlation ID.
      static void enhanceLogWithCorrelationIdOrRandom​(org.eclipse.ditto.model.base.headers.WithDittoHeaders<?> withDittoHeaders)
      Enhances the default slf4j MDC with a correlationId of the passed withDittoHeaders (if present), else a random correlation ID.
      static void enhanceLogWithCustomField​(akka.event.DiagnosticLoggingAdapter loggingAdapter, String fieldName, CharSequence fieldValue, LogUtil.MdcField... additionalMdcFields)
      Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry for the passed fieldName with the passed fieldValue (if present).
      static void enhanceLogWithCustomField​(LogUtil.MdcField mdcField)
      Enhances the default MDC with a map entry for the passed mdcFields.
      static Optional<String> getCorrelationId()
      Gets the correlationId from the default slf4j MDC.
      static String getCorrelationId​(Supplier<String> defaultCorrelationIdSupplier)
      Gets the correlationId from the default slf4j MDC.
      static LogUtil.MdcField newMdcField​(String name, String value)
      Creates a new MDC field.
      static akka.event.DiagnosticLoggingAdapter obtain​(akka.actor.Actor logSource)
      Obtain LoggingAdapter with MDC support for the given actor.
      static void removeCorrelationId()
      Removes the correlationId from the default slf4j MDC.
      static void removeCustomField​(akka.event.DiagnosticLoggingAdapter loggingAdapter, String fieldName)
      Removes the fieldName from the default slf4j MDC.
    • Field Detail

      • X_CORRELATION_ID

        public static final String X_CORRELATION_ID
        Name of the Header for the global Ditto correlation ID.
        See Also:
        Constant Field Values
    • Method Detail

      • obtain

        public static akka.event.DiagnosticLoggingAdapter obtain​(akka.actor.Actor logSource)
        Obtain LoggingAdapter with MDC support for the given actor.
        Parameters:
        logSource - the Actor used as logSource
        Returns:
        the created DiagnosticLoggingAdapter.
      • enhanceLogWithCorrelationId

        public static void enhanceLogWithCorrelationId​(akka.event.DiagnosticLoggingAdapter loggingAdapter,
                                                       org.eclipse.ditto.model.base.headers.WithDittoHeaders<?> withDittoHeaders,
                                                       LogUtil.MdcField... additionalMdcFields)
        Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry by extracting a correlationId of the passed withDittoHeaders (if present).
        Parameters:
        loggingAdapter - the DiagnosticLoggingAdapter to set the "MDC" on.
        withDittoHeaders - where to extract a possible correlation ID from.
        additionalMdcFields - additional fields to add to the MDC.
      • enhanceLogWithCorrelationId

        public static void enhanceLogWithCorrelationId​(akka.event.DiagnosticLoggingAdapter loggingAdapter,
                                                       org.eclipse.ditto.model.base.headers.DittoHeaders dittoHeaders,
                                                       LogUtil.MdcField... additionalMdcFields)
        Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry by extracting a correlationId of the passed dittoHeaders (if present).
        Parameters:
        loggingAdapter - the DiagnosticLoggingAdapter to set the "MDC" on.
        dittoHeaders - where to extract a possible correlation ID from.
        additionalMdcFields - additional fields to add to the MDC.
      • enhanceLogWithCorrelationId

        public static void enhanceLogWithCorrelationId​(akka.event.DiagnosticLoggingAdapter loggingAdapter,
                                                       Optional<String> correlationId,
                                                       LogUtil.MdcField... additionalMdcFields)
        Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry for the passed correlationId (if present).
        Parameters:
        loggingAdapter - the DiagnosticLoggingAdapter to set the "MDC" on.
        correlationId - the optional correlation ID to set.
        additionalMdcFields - additional fields to add to the MDC.
      • enhanceLogWithCustomField

        public static void enhanceLogWithCustomField​(LogUtil.MdcField mdcField)
        Enhances the default MDC with a map entry for the passed mdcFields.
        Parameters:
        mdcField - the field to add to the MDC.
      • enhanceLogWithCorrelationId

        public static void enhanceLogWithCorrelationId​(akka.event.DiagnosticLoggingAdapter loggingAdapter,
                                                       CharSequence correlationId,
                                                       LogUtil.MdcField... additionalMdcFields)
        Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry for the passed correlationId (if present).
        Parameters:
        loggingAdapter - the DiagnosticLoggingAdapter to set the "MDC" on.
        correlationId - the optional correlation ID to set.
        additionalMdcFields - additional fields to add to the MDC.
      • enhanceLogWithCustomField

        public static void enhanceLogWithCustomField​(akka.event.DiagnosticLoggingAdapter loggingAdapter,
                                                     String fieldName,
                                                     @Nullable
                                                     CharSequence fieldValue,
                                                     LogUtil.MdcField... additionalMdcFields)
        Enhances the passed DiagnosticLoggingAdapter with an "MDC" map entry for the passed fieldName with the passed fieldValue (if present).
        Parameters:
        loggingAdapter - the DiagnosticLoggingAdapter to set the "MDC" on.
        fieldName - the field value to set in MDC.
        fieldValue - the optional value to set.
        additionalMdcFields - additional fields to add to the MDC.
      • enhanceLogWithCorrelationId

        public static void enhanceLogWithCorrelationId​(org.eclipse.ditto.model.base.headers.WithDittoHeaders<?> withDittoHeaders)
        Enhances the default slf4j MDC with a correlationId of the passed withDittoHeaders (if present).
        Parameters:
        withDittoHeaders - where to extract a possible correlation ID from.
      • enhanceLogWithCorrelationId

        public static void enhanceLogWithCorrelationId​(org.eclipse.ditto.model.base.headers.DittoHeaders dittoHeaders)
        Enhances the default slf4j MDC with a correlationId of the passed dittoHeaders (if present).
        Parameters:
        dittoHeaders - where to extract a possible correlation ID from.
      • enhanceLogWithCorrelationIdOrRandom

        public static void enhanceLogWithCorrelationIdOrRandom​(org.eclipse.ditto.model.base.headers.WithDittoHeaders<?> withDittoHeaders)
        Enhances the default slf4j MDC with a correlationId of the passed withDittoHeaders (if present), else a random correlation ID.
        Parameters:
        withDittoHeaders - where to extract a possible correlation ID from.
      • enhanceLogWithCorrelationIdOrRandom

        public static void enhanceLogWithCorrelationIdOrRandom​(org.eclipse.ditto.model.base.headers.DittoHeaders dittoHeaders)
        Enhances the default slf4j MDC with a correlationId of the passed dittoHeaders (if present), else a random correlation ID.
        Parameters:
        dittoHeaders - where to extract a possible correlation ID from.
      • enhanceLogWithCorrelationId

        public static void enhanceLogWithCorrelationId​(Optional<String> correlationId)
        Enhances the default slf4j MDC with a correlationId.
        Parameters:
        correlationId - the optional correlation ID to set.
      • enhanceLogWithCorrelationId

        public static void enhanceLogWithCorrelationId​(@Nullable
                                                       CharSequence correlationId)
        Enhances the default slf4j MDC with a correlationId.
        Parameters:
        correlationId - the optional correlation ID to set.
      • getCorrelationId

        public static Optional<String> getCorrelationId()
        Gets the correlationId from the default slf4j MDC.
        Returns:
        the correlationId from MDC or an empty Optional if it didn't exist.
      • getCorrelationId

        public static String getCorrelationId​(Supplier<String> defaultCorrelationIdSupplier)
        Gets the correlationId from the default slf4j MDC.
        Parameters:
        defaultCorrelationIdSupplier - supplies a default correlation ID if none could be found inside MDC.
        Returns:
        The correlationId from MDC or from defaultCorrelationIdSupplier if it didn't exist.
      • removeCorrelationId

        public static void removeCorrelationId()
        Removes the correlationId from the default slf4j MDC.
      • removeCustomField

        public static void removeCustomField​(akka.event.DiagnosticLoggingAdapter loggingAdapter,
                                             String fieldName)
        Removes the fieldName from the default slf4j MDC.
        Parameters:
        loggingAdapter - the DiagnosticLoggingAdapter to remove the MDC field from.
        fieldName - the field to remove.
      • newMdcField

        public static LogUtil.MdcField newMdcField​(String name,
                                                   @Nullable
                                                   String value)
        Creates a new MDC field.
        Parameters:
        name - the name of the field.
        value - the value of the field.
        Returns:
        the MDC field for name and value.
        Throws:
        NullPointerException - if name is null.