Class PerfLogger


  • public final class PerfLogger
    extends Object
    PerfLogger is a simpler wrapper around a slf4j Logger which comes with the capability to issue log statements containing the measurement between start() and end() methods.

    Usage:

    • final long start = perflogger.start();
    • .. some code ..
    • perflogger.end(start, 1, "myMethodName: param1={}", param1);

    The above will do nothing if the log level for the logger passed to PerfLogger at construction time is not DEBUG or TRACE - otherwise start() will return the current time in milliseconds and end will issue a log statement if the time between start and end was bigger than 1 ms, and it will pass the parameters to the log statement. The idea is to keep up performance at max possible if the log level is INFO or higher - but to allow some meaningful logging if at DEBUG or TRACe. The difference between DEBUG and TRACE is that TRACE will log start too (if a log message is passed to start) and it will always log the end - whereas in case of DEBUG the start will never be logged and the end will only be logged if the time is bigger than what's passed to end.

    • Constructor Detail

      • PerfLogger

        public PerfLogger​(Logger delegate)
        Create a new PerfLogger that shall use the given Logger object for logging
    • Method Detail

      • start

        public final long start()
        Shortcut to #start(null, false)
      • startForInfoLog

        public final long startForInfoLog()
        Shortcut to start(null, true)
      • start

        public final long start​(String traceMsgOrNull)
        Shortcut to start(traceMsgOrNull, false)
      • startForInfoLog

        public final long startForInfoLog​(String traceMsgOrNull)
        Shortcut to start(traceMsgOrNull, true)
      • end

        public final void end​(long start,
                              long logAtDebugIfSlowerThanMs,
                              String logMessagePrefix,
                              Object arg1)
        See end(long, long, long, String, Object...) Note that this method exists for performance optimization only (compared to the other end() method with a vararg.
      • end

        public final void end​(long start,
                              long logAtDebugIfSlowerThanMs,
                              String logMessagePrefix,
                              Object arg1,
                              Object arg2)
        See end(long, long, long, String, Object...) Note that this method exists for performance optimization only (compared to the other end() method with a vararg.
      • end

        public final void end​(long start,
                              long logAtDebugIfSlowerThanMs,
                              long logAtInfoIfSlowerThanMs,
                              String logMessagePrefix,
                              Object arg1)
        See end(long, long, long, String, Object...) Note that this method exists for performance optimization only (compared to the other end() method with a vararg.
      • end

        public final void end​(long start,
                              long logAtDebugIfSlowerThanMs,
                              long logAtInfoIfSlowerThanMs,
                              String logMessagePrefix,
                              Object arg1,
                              Object arg2)
        See end(long, long, long, String, Object...) Note that this method exists for performance optimization only (compared to the other end() method with a vararg.
      • end

        public void end​(long start,
                        long logAtDebugIfSlowerThanMs,
                        long logAtInfoIfSlowerThanMs,
                        String logMessagePrefix,
                        Object... arguments)
        Returns quickly if start is negative (which is the case according to log level at the time of start() or startForInfoLog()). If log level is set to TRACE, log.trace is always emitted. If log level is set to DEBUG, then log.debug is emitted if 'now' is bigger (slower) than start by at least logAtDebugIfSlowerThanMs. If log level is set to INFO, then long.info is emitted if 'now' is bigger (slower) than start by at least logAtInfoIfSlowerThanMs.
        Parameters:
        start - the start time with which 'now' should be compared
        logAtDebugIfSlowerThanMs - the number of milliseconds that must be surpassed to issue a log.debug (if log level is DEBUG)
        logAtInfoIfSlowerThanMs - the number of milliseconds that must be surpassed to issue a log.info (if log level is DEBUG)
        logMessagePrefix - the log message 'prefix' - to which the given arguments will be passed, plus the measured time difference in the format '[took x ms']
        arguments - the arguments which is to be passed to the log statement
      • isInfoEnabled

        public final boolean isInfoEnabled()
        Whether or not the delegate has log level INFO configured
      • isDebugEnabled

        public final boolean isDebugEnabled()
        Whether or not the delegate has log level DEBUG configured
      • isTraceEnabled

        public final boolean isTraceEnabled()
        Whether or not the delegate has log level TRACE configured