Class LoggingUtils


  • public final class LoggingUtils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private LoggingUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> java.util.function.Consumer<T> debugClosure​(org.slf4j.Logger logger)  
      static <T> java.util.function.Consumer<T> debugClosure​(org.slf4j.Logger logger, java.lang.Throwable thrown)  
      static <T> java.util.function.Consumer<T> errorClosure​(org.slf4j.Logger logger)  
      static <T> java.util.function.Consumer<T> errorClosure​(org.slf4j.Logger logger, java.lang.Throwable thrown)  
      static java.util.NavigableMap<java.lang.Integer,​java.lang.String> generateMnemonicMap​(java.lang.Class<?> clazz, java.lang.String commonPrefix)
      Scans using reflection API for all fields that are public static final that start with the given common prefix (case sensitive) and are of type Number.
      static java.util.NavigableMap<java.lang.Integer,​java.lang.String> generateMnemonicMap​(java.lang.Class<?> clazz, java.util.function.Predicate<? super java.lang.reflect.Field> acceptor)
      Scans using reflection API for all numeric public static final fields that are also accepted by the predicate.
      static java.util.Map<java.lang.String,​java.lang.Integer> getAmbiguousMenmonics​(java.lang.Class<?> clazz, java.lang.String commonPrefix)
      Scans using reflection API for all numeric public static final fields that have a common prefix and whose value is used by several of the other matching fields
      static java.util.Map<java.lang.String,​java.lang.Integer> getAmbiguousMenmonics​(java.lang.Class<?> clazz, java.util.function.Predicate<? super java.lang.reflect.Field> acceptor)
      Scans using reflection API for all numeric public static final fields that are also accepted by the predicate and whose value is used by several of the other matching fields
      static java.util.Collection<java.lang.reflect.Field> getMnemonicFields​(java.lang.Class<?> clazz, java.util.function.Predicate<? super java.lang.reflect.Field> acceptor)
      Scans using reflection API for all numeric public static final fields that are also accepted by the predicate.
      static <T> java.util.function.Consumer<T> infoClosure​(org.slf4j.Logger logger)  
      static <T> java.util.function.Consumer<T> infoClosure​(org.slf4j.Logger logger, java.lang.Throwable thrown)  
      static boolean isLoggable​(java.util.logging.Level level, java.util.logging.Level threshold)
      Verifies if the given level is above the required threshold for logging.
      static boolean isLoggable​(org.slf4j.Logger logger, java.util.logging.Level level)  
      static <T> java.util.function.Consumer<T> loggingClosure​(org.slf4j.Logger logger, java.util.logging.Level level)  
      static <T> java.util.function.Consumer<T> loggingClosure​(org.slf4j.Logger logger, java.util.logging.Level level, java.lang.Throwable t)  
      static void logMessage​(org.slf4j.Logger logger, java.util.logging.Level level, java.lang.Object message, java.lang.Throwable t)  
      static <T> java.util.function.Consumer<T> nologClosure​(org.slf4j.Logger logger)  
      static <T> java.util.function.Consumer<T> traceClosure​(org.slf4j.Logger logger)  
      static <T> java.util.function.Consumer<T> traceClosure​(org.slf4j.Logger logger, java.lang.Throwable thrown)  
      static <T> java.util.function.Consumer<T> warnClosure​(org.slf4j.Logger logger)  
      static <T> java.util.function.Consumer<T> warnClosure​(org.slf4j.Logger logger, java.lang.Throwable thrown)  
      static SimplifiedLog wrap​(org.slf4j.Logger logger)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LoggingUtils

        private LoggingUtils()
    • Method Detail

      • generateMnemonicMap

        public static java.util.NavigableMap<java.lang.Integer,​java.lang.String> generateMnemonicMap​(java.lang.Class<?> clazz,
                                                                                                           java.lang.String commonPrefix)
        Scans using reflection API for all fields that are public static final that start with the given common prefix (case sensitive) and are of type Number.
        Parameters:
        clazz - The Class to query
        commonPrefix - The expected common prefix
        Returns:
        A NavigableMap of all the matching fields, where key=the field's Integer value and mapping=the field's name
        See Also:
        generateMnemonicMap(Class, Predicate)
      • generateMnemonicMap

        public static java.util.NavigableMap<java.lang.Integer,​java.lang.String> generateMnemonicMap​(java.lang.Class<?> clazz,
                                                                                                           java.util.function.Predicate<? super java.lang.reflect.Field> acceptor)
        Scans using reflection API for all numeric public static final fields that are also accepted by the predicate. Any field that is not such or fail to retrieve its value, or has a duplicate value is silently skipped.
        Parameters:
        clazz - The Class to query
        acceptor - The Predicate used to decide whether to process the Field (besides being a Number and public static final).
        Returns:
        A NavigableMap of all the matching fields, where key=the field's Integer value and mapping=the field's name
        See Also:
        getMnemonicFields(Class, Predicate)
      • getAmbiguousMenmonics

        public static java.util.Map<java.lang.String,​java.lang.Integer> getAmbiguousMenmonics​(java.lang.Class<?> clazz,
                                                                                                    java.lang.String commonPrefix)
        Scans using reflection API for all numeric public static final fields that have a common prefix and whose value is used by several of the other matching fields
        Parameters:
        clazz - The Class to query
        commonPrefix - The expected common prefix
        Returns:
        A Map of all the mnemonic fields names whose value is the same as other fields in this map. The key is the field's name and value is its associated opcode.
        See Also:
        getAmbiguousMenmonics(Class, Predicate)
      • getAmbiguousMenmonics

        public static java.util.Map<java.lang.String,​java.lang.Integer> getAmbiguousMenmonics​(java.lang.Class<?> clazz,
                                                                                                    java.util.function.Predicate<? super java.lang.reflect.Field> acceptor)
        Scans using reflection API for all numeric public static final fields that are also accepted by the predicate and whose value is used by several of the other matching fields
        Parameters:
        clazz - The Class to query
        acceptor - The Predicate used to decide whether to process the Field (besides being a Number and public static final).
        Returns:
        A Map of all the mnemonic fields names whose value is the same as other fields in this map. The key is the field's name and value is its associated opcode.
        See Also:
        getMnemonicFields(Class, Predicate)
      • getMnemonicFields

        public static java.util.Collection<java.lang.reflect.Field> getMnemonicFields​(java.lang.Class<?> clazz,
                                                                                      java.util.function.Predicate<? super java.lang.reflect.Field> acceptor)
        Scans using reflection API for all numeric public static final fields that are also accepted by the predicate.
        Parameters:
        clazz - The Class to query
        acceptor - The Predicate used to decide whether to process the Field (besides being a Number and public static final).
        Returns:
        A Collection of all the fields that have satisfied all conditions
      • isLoggable

        public static boolean isLoggable​(java.util.logging.Level level,
                                         java.util.logging.Level threshold)
        Verifies if the given level is above the required threshold for logging.
        Parameters:
        level - The Level to evaluate
        threshold - The threshold Level
        Returns:
        true if the evaluated level is above the required threshold.

        Note(s):

        • If either argument is null then result is false.

        • If the evaluated level is Level.OFF then result is false regardless of the threshold.

        • If the threshold is Level.ALL and the evaluated level is not Level.OFF the result is true.

        • Otherwise, the evaluated level Level.intValue() must be greater or equal to the threshold.

      • wrap

        public static SimplifiedLog wrap​(org.slf4j.Logger logger)
      • logMessage

        public static void logMessage​(org.slf4j.Logger logger,
                                      java.util.logging.Level level,
                                      java.lang.Object message,
                                      java.lang.Throwable t)
      • isLoggable

        public static boolean isLoggable​(org.slf4j.Logger logger,
                                         java.util.logging.Level level)
        Parameters:
        logger - The Logger instance - ignored if null
        level - The validate log Level - ignored if null
        Returns:

        true if the level is enabled for the logger. The mapping of the level to the logger is as follows:

        • Level.OFF always returns false
        • Level.SEVERE returns Logger.isErrorEnabled()
        • Level.WARNING returns Logger.isWarnEnabled()
        • Level.INFO and Level.ALL returns Logger.isInfoEnabled()
        • Level.CONFIG and Level.FINE returns Logger.isDebugEnabled()
        • All other levels return Logger.isTraceEnabled()
      • loggingClosure

        public static <T> java.util.function.Consumer<T> loggingClosure​(org.slf4j.Logger logger,
                                                                        java.util.logging.Level level)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        level - The log Level mapped as follows:
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if the specific level is enabled
      • loggingClosure

        public static <T> java.util.function.Consumer<T> loggingClosure​(org.slf4j.Logger logger,
                                                                        java.util.logging.Level level,
                                                                        java.lang.Throwable t)
      • nologClosure

        public static <T> java.util.function.Consumer<T> nologClosure​(org.slf4j.Logger logger)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        Returns:
        A consumer whose Consumer.accept(Object) method logs nothing when invoked
      • errorClosure

        public static <T> java.util.function.Consumer<T> errorClosure​(org.slf4j.Logger logger)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isErrorEnabled()
      • errorClosure

        public static <T> java.util.function.Consumer<T> errorClosure​(org.slf4j.Logger logger,
                                                                      java.lang.Throwable thrown)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        thrown - A Throwable to attach to the message - ignored if null
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isErrorEnabled()
      • warnClosure

        public static <T> java.util.function.Consumer<T> warnClosure​(org.slf4j.Logger logger)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isWarnEnabled()
      • warnClosure

        public static <T> java.util.function.Consumer<T> warnClosure​(org.slf4j.Logger logger,
                                                                     java.lang.Throwable thrown)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        thrown - A Throwable to attach to the message - ignored if null
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isWarnEnabled()
      • infoClosure

        public static <T> java.util.function.Consumer<T> infoClosure​(org.slf4j.Logger logger)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isInfoEnabled()
      • infoClosure

        public static <T> java.util.function.Consumer<T> infoClosure​(org.slf4j.Logger logger,
                                                                     java.lang.Throwable thrown)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        thrown - A Throwable to attach to the message - ignored if null
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isInfoEnabled()
      • debugClosure

        public static <T> java.util.function.Consumer<T> debugClosure​(org.slf4j.Logger logger)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isDebugEnabled()
      • debugClosure

        public static <T> java.util.function.Consumer<T> debugClosure​(org.slf4j.Logger logger,
                                                                      java.lang.Throwable thrown)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        thrown - A Throwable to attach to the message - ignored if null
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isDebugEnabled()
      • traceClosure

        public static <T> java.util.function.Consumer<T> traceClosure​(org.slf4j.Logger logger)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isTraceEnabled()
      • traceClosure

        public static <T> java.util.function.Consumer<T> traceClosure​(org.slf4j.Logger logger,
                                                                      java.lang.Throwable thrown)
        Type Parameters:
        T - Generic message type consumer
        Parameters:
        logger - The Logger instance to use
        thrown - A Throwable to attach to the message - ignored if null
        Returns:
        A consumer whose Consumer.accept(Object) method logs the String.valueOf(Object) value of its argument if Logger.isTraceEnabled()