Class LoggerAtLevel

  • Direct Known Subclasses:
    NOOPLogger

    public class LoggerAtLevel
    extends Object
    This is where the fluent API of slf4j-fluent is implemented. With its methods we can build the log message, assign an exception to it, and specify logging limits such as quantity ("don't log this more than once every 5 times") or time ("don't log this more than once every 2 seconds").

    In order to avoid implicit array creation when using varargs, this class provides a number overloaded version of the log method with increasing number of arguments.

    log methods arguments can be of any type. If they are Suppliers, they will be called only when actually logging, thus providing lazy argument evaluation. Also see Util.lazy(Supplier).

    When filtering log entries based on quantity or time, we need to generate the "call site" which is the fully qualified name of the class, plus the name of the method calling the log method, plus the line number.

    See Also:
    Util.lazy(Supplier)
    • Constructor Detail

      • LoggerAtLevel

        public LoggerAtLevel​(BiConsumer<String,​Object[]> loggerMethod,
                             org.fissore.slf4j.TriConsumer<org.slf4j.Marker,​String,​Object[]> loggerMethodWithMarker,
                             boolean isLocationAwareLogger,
                             org.slf4j.Logger logger,
                             int level)
    • Method Detail

      • withCause

        public LoggerAtLevel withCause​(Throwable cause)
        Associates an exception to this log entry.
        Parameters:
        cause - the exception we want to associate this log entry to
        Returns:
        this instance of LoggerAtLevel
      • withMarker

        public LoggerAtLevel withMarker​(org.slf4j.Marker marker)
      • every

        public LoggerAtLevel every​(long amountOfTime,
                                   ChronoUnit unit)
        Configures this LoggerAtLevel to log at most every amount of time.

        For example, say we want to log at most every 2 seconds. If our code attempts to log multiple times for 5 seconds, we'll see only 3 log entries in our log files: the first one, the first one after 2 seconds, and the first one after 4 seconds.

        Parameters:
        amountOfTime - the amount of time to wait between log entries
        unit - the unit of time expressed by amountOfTime
        Returns:
        this instance of LoggerAtLevel
        Throws:
        IllegalStateException - if this LoggerAtLevel was already configured to limit logging by quantity
      • every

        public LoggerAtLevel every​(int amountOfCalls)
        Configures this LoggerAtLevel to log at most every number of times

        For example, say we want to log at most every 5 times. If our code attempts to log 8 times, we'll see 2 log entries in our log files: the first one, and the fifth one.

        Parameters:
        amountOfCalls - the amount of calls that must be made before logging
        Returns:
        this instance of LoggerAtLevel
        Throws:
        IllegalStateException - if this LoggerAtLevel was already configured to limit logging by time
      • log

        public void log​(String message)
        Logs a message with no params
        Parameters:
        message - the log message
      • log

        public void log​(Supplier<String> messageSupplier)
        Logs a message with no params
        Parameters:
        messageSupplier - the log message supplier
      • log

        public void log​(String format,
                        Object arg)
        Logs a message with one param
        Parameters:
        format - the log message
        arg - a log message param
      • log

        public void log​(Supplier<String> formatSupplier,
                        Object arg)
        Logs a message with one param
        Parameters:
        formatSupplier - the log message supplier
        arg - a log message param
      • log

        public void log​(String format,
                        Object arg1,
                        Object arg2)
        Logs a message with two params
        Parameters:
        format - the log message
        arg1 - a log message param
        arg2 - a log message param
      • log

        public void log​(Supplier<String> formatSupplier,
                        Object arg1,
                        Object arg2)
        Logs a message with two params
        Parameters:
        formatSupplier - the log message supplier
        arg1 - a log message param
        arg2 - a log message param
      • log

        public void log​(String format,
                        Object arg1,
                        Object arg2,
                        Object arg3)
        Logs a message with three params
        Parameters:
        format - the log message
        arg1 - a log message param
        arg2 - a log message param
        arg3 - a log message param
      • log

        public void log​(Supplier<String> formatSupplier,
                        Object arg1,
                        Object arg2,
                        Object arg3)
        Logs a message with three params
        Parameters:
        formatSupplier - the log message supplier
        arg1 - a log message param
        arg2 - a log message param
        arg3 - a log message param
      • log

        public void log​(String format,
                        Object arg1,
                        Object arg2,
                        Object arg3,
                        Object arg4)
        Logs a message with four params
        Parameters:
        format - the log message
        arg1 - a log message param
        arg2 - a log message param
        arg3 - a log message param
        arg4 - a log message param
      • log

        public void log​(Supplier<String> formatSupplier,
                        Object arg1,
                        Object arg2,
                        Object arg3,
                        Object arg4)
        Logs a message with four params
        Parameters:
        formatSupplier - the log message supplier
        arg1 - a log message param
        arg2 - a log message param
        arg3 - a log message param
        arg4 - a log message param
      • log

        public void log​(String format,
                        Object arg1,
                        Object arg2,
                        Object arg3,
                        Object arg4,
                        Object arg5)
        Logs a message with five params
        Parameters:
        format - the log message
        arg1 - a log message param
        arg2 - a log message param
        arg3 - a log message param
        arg4 - a log message param
        arg5 - a log message param
      • log

        public void log​(Supplier<String> formatSupplier,
                        Object arg1,
                        Object arg2,
                        Object arg3,
                        Object arg4,
                        Object arg5)
        Logs a message with five params
        Parameters:
        formatSupplier - the log message supplier
        arg1 - a log message param
        arg2 - a log message param
        arg3 - a log message param
        arg4 - a log message param
        arg5 - a log message param
      • log

        public void log​(String format,
                        Object... args)
        Logs a message with varying number of params
        Parameters:
        format - the log message
        args - log message params
      • log

        public void log​(Supplier<String> formatSupplier,
                        Object... args)
        Logs a message with varying number of params
        Parameters:
        formatSupplier - the log message supplier
        args - log message params