Class CuiLogger


  • public class CuiLogger
    extends Object

    Wrapper around java-util Logger that simplifies its usage. In addition it provides a similar api like slf4j. It is not meant to act as logging-facade like slf4j or jakarta-commons-logging. It only provides a little syntactic sugar for the built-in logger.

    Obtaining a logger

    private static final CuiLogger log = new CuiLogger(SomeClass.class);

    private static final CuiLogger log = new CuiLogger("SomeLoggerName");

    private static final CuiLogger log = CuiLoggerFactory.getLogger();

    Logging

    CuiLogger provides an implicit code guard, if used correctly. Used correctly hereby means to either use formatting with parameter or incorporating Supplier for generating the actual log-message. For other means of creating a message you still can use code guards.

    log.trace("Parameter-type matches exactly '%s'", assignableSource);

    log.debug("Adding found method '%s' on class '%s'", name, clazz);

    log.info("Starting up application");

    // In order not to mess up with the ellipsis parameter

    // exceptions must be the first parameter

    log.warn(e, "Exception during lenientFormat for '%s'", objectToString);

    log.error(e, "Caught an exception");

    log.info(() -> "Supplier can be used as well");

    log.error(e, () -> "Even with exceptions");

    log.trace(() -> "I will only be evaluated if the trace-level for is enabled");

    Formatting

    Like slf4j there is a simple way of formatting log-messages. In addition to '{}' the formatting supports '%s' as well. At runtime it replaces the '{}' tokens with '%s' and passes the data to MoreStrings.lenientFormat(String, Object...) for creating the actual log-message. As a variant providing a Supplier works as well.

    Author:
    Oliver Wolff
    • Constructor Detail

      • CuiLogger

        public CuiLogger​(Class<?> clazz)
        Parameters:
        clazz - to be used for acquiring a concrete Logger instance. Must not be null
      • CuiLogger

        public CuiLogger​(String name)
        Parameters:
        name - to be used for acquiring a concrete Logger instance. Must not be null
    • Method Detail

      • isTraceEnabled

        public boolean isTraceEnabled()
        Is the logger instance enabled for the trace level?
        Returns:
        true if this CuiLogger is enabled for the trace level, false otherwise.
      • trace

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

        public void trace​(Supplier<String> msg)
        Log a message at the trace level.
        Parameters:
        msg - the message string to be logged
      • trace

        public void trace​(Throwable throwable,
                          Supplier<String> msg)
        Log a message at the trace level.
        Parameters:
        throwable - to be logged
        msg - the message string to be logged
      • trace

        public void trace​(String msg,
                          Throwable throwable)
        Log a message at the trace level.
        Parameters:
        msg - the message string to be logged
        throwable - to be logged
      • trace

        public void trace​(Throwable throwable,
                          String template,
                          Object... parameter)
        Log a message at the trace level.
        Parameters:
        throwable - to be logged
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • trace

        public void trace​(String template,
                          Object... parameter)
        Log a message at the trace level.
        Parameters:
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • isDebugEnabled

        public boolean isDebugEnabled()
        Is the logger instance enabled for the debug level?
        Returns:
        true if this CuiLogger is enabled for the debug level, false otherwise.
      • debug

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

        public void debug​(String msg,
                          Throwable throwable)
        Log a message at the debug level.
        Parameters:
        msg - the message string to be logged
        throwable - to be logged
      • debug

        public void debug​(Supplier<String> msg)
        Log a message at the debug level.
        Parameters:
        msg - the message string to be logged
      • debug

        public void debug​(Throwable throwable,
                          Supplier<String> msg)
        Log a message at the debug level.
        Parameters:
        throwable - to be logged
        msg - the message string to be logged
      • debug

        public void debug​(Throwable throwable,
                          String template,
                          Object... parameter)
        Log a message at the debug level.
        Parameters:
        throwable - to be logged
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • debug

        public void debug​(String template,
                          Object... parameter)
        Log a message at the debug level.
        Parameters:
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • isInfoEnabled

        public boolean isInfoEnabled()
        Is the logger instance enabled for the info level?
        Returns:
        true if this CuiLogger is enabled for the info level, false otherwise.
      • info

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

        public void info​(String msg,
                         Throwable throwable)
        Log a message at the info level.
        Parameters:
        msg - the message string to be logged
        throwable - to be logged
      • info

        public void info​(Supplier<String> msg)
        Log a message at the info level.
        Parameters:
        msg - the message string to be logged
      • info

        public void info​(Throwable throwable,
                         Supplier<String> msg)
        Log a message at the info level.
        Parameters:
        throwable - to be logged
        msg - the message string to be logged
      • info

        public void info​(Throwable throwable,
                         String template,
                         Object... parameter)
        Log a message at the info level.
        Parameters:
        throwable - to be logged
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • info

        public void info​(String template,
                         Object... parameter)
        Log a message at the info level.
        Parameters:
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • isWarnEnabled

        public boolean isWarnEnabled()
        Is the logger instance enabled for the warn level?
        Returns:
        true if this CuiLogger is enabled for the warn level, false otherwise.
      • warn

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

        public void warn​(String msg,
                         Throwable throwable)
        Log a message at the warn level.
        Parameters:
        msg - the message string to be logged
        throwable - to be logged
      • warn

        public void warn​(Supplier<String> msg)
        Log a message at the warn level.
        Parameters:
        msg - the message string to be logged
      • warn

        public void warn​(Throwable throwable,
                         Supplier<String> msg)
        Log a message at the warn level.
        Parameters:
        throwable - to be logged
        msg - the message string to be logged
      • warn

        public void warn​(Throwable throwable,
                         String template,
                         Object... parameter)
        Log a message at the warn level.
        Parameters:
        throwable - to be logged
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • warn

        public void warn​(String template,
                         Object... parameter)
        Log a message at the warn level.
        Parameters:
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • isErrorEnabled

        public boolean isErrorEnabled()
        Is the logger instance enabled for the error level?
        Returns:
        true if this CuiLogger is enabled for the error level, false otherwise.
      • error

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

        public void error​(String msg,
                          Throwable throwable)
        Log a message at the error level.
        Parameters:
        msg - the message string to be logged
        throwable - to be logged
      • error

        public void error​(Supplier<String> msg)
        Log a message at the error level.
        Parameters:
        msg - the message string to be logged
      • error

        public void error​(Throwable throwable,
                          Supplier<String> msg)
        Log a message at the error level.
        Parameters:
        throwable - to be logged
        msg - the message string to be logged
      • error

        public void error​(Throwable throwable,
                          String template,
                          Object... parameter)
        Log a message at the error level.
        Parameters:
        throwable - to be logged
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • error

        public void error​(String template,
                          Object... parameter)
        Log a message at the error level.
        Parameters:
        template - to be used for formatting, see class-documentation for details on formatting
        parameter - to be used for replacing the placeholder
      • getName

        public String getName()
        Returns:
        the name / class of the underlying logger
      • getLogLevel

        public LogLevel getLogLevel()
        Returns:
        CUI log level derived from JUL log level. E.g. FINEST(300) matches TRACE(400), CONFIG(700) matches DEBUG(500), ALL matches TRACE.