Class ThrottleLogger


  • public class ThrottleLogger
    extends Object
    Logger that will repeat the same message not more than once per defined interval. Messages are stored in WeakHashMap as keys. This means that throttling might not actually work if full GC is run. This is a trade-off from storing hard references to messages and always remembering them for the full hour so that memory can be freed if java needs it.

    ThrottleLogger use logging template to compare message identities. Messages identical if templates are the same object.
    The easiest way to achieve it is to use java string pool/literals instead of objects. As uninterned objects would not work.

    For example:

    
       log.debug("Hello world!")
       lod.debug("Hello " + "world!")
     

    will be treated as two different messages. But

    
       log.debug("Hello world!")
       lod.debug(("Hello " + "world!").intern())
     

    be the same.

    Usage of arguments are optional. If last parameter instanceof Throwable it considered as Exception and will be treated as one. Template syntax use "{}" as placeholders to format message.
    Formats messages according to Slf4j MessageFormatter syntax.
    It is recommended to use parametrized templates if not especially required otherwise.

    
      log.debug("Hello {}!", "World")
     

    Be careful: throttling would not be supported if template != template. It is considered a code smell to use string concatenation/jdk message format/string builder/other approaches. Using that approached not only consumes considerably more CPU/Memory resources but also prevents effectively use ThrottleLogger.

    
      log.debug(String.format("Hello %s!","World")) <- bad
      log.debug("Hello " + "World!")) <- bad
      log.debug(new StringBuilder().append("Hello ").append("World!").toString()) <- bad
     
    
      int a = 0;
      log.debug("Hello {} a={}!", "World", a)
      try {
        ...
      } catch (Exception e) {
        log.error("Hello {} a={}!", "World", a, e);
      }
     
    • Field Detail

      • TIME_1_HOUR

        public static final long TIME_1_HOUR
      • TIME_5_MINS

        public static final long TIME_5_MINS
      • TIME_1_MIN

        public static final long TIME_1_MIN
      • TIME_30_SEC

        public static final long TIME_30_SEC
    • Method Detail

      • getLogger

        public static ThrottleLogger getLogger​(Class<?> aClass,
                                               long timeLimitNanos,
                                               long messageLimit,
                                               boolean lowerLevel)
      • getLogger

        public static ThrottleLogger getLogger​(String category,
                                               long timeLimitNanos,
                                               long messageLimit,
                                               boolean lowerLevel)
      • getLogger

        public static ThrottleLogger getLogger​(Class<?> aClass,
                                               long timeLimitNanos)
      • getLogger

        public static ThrottleLogger getLogger​(Class<?> aClass,
                                               long timeLimitNanos,
                                               boolean lowerLevel)
      • getLLogger

        public static ThrottleLogger getLLogger​(Class<?> aClass,
                                                long timeLimitNanos)
        Logs duplicate messages with lower priority instead of just ignoring them.
        Parameters:
        aClass - class used to create the name of the logger.
        timeLimitNanos - time period during which same messages will be treated as duplicates.
        Returns:
        created logger
      • getLLogger

        public static ThrottleLogger getLLogger​(Class<?> aClass)
        Logs duplicate messages with lower priority instead of just ignoring, with 5 minutes throttling.
        Parameters:
        aClass - class used to create the name of the logger.
        Returns:
        created logger
      • warn

        public void warn​(String template,
                         Object... args)
        Parameters:
        template -
        args -
      • fatal

        public void fatal​(String template,
                          Object... args)
      • error

        public void error​(String template,
                          Object... args)
      • info

        public void info​(String template,
                         Object... args)
      • debug

        public void debug​(String template,
                          Object... args)
      • warnAndDebugDetails

        public void warnAndDebugDetails​(String template,
                                        Object... args)
      • trace

        public void trace​(String template,
                          Object... args)
      • all

        public void all​(String template,
                        Object... args)
      • isDebugEnabled

        public boolean isDebugEnabled()
      • isWarnEnabled

        public boolean isWarnEnabled()
      • isInfoEnabled

        public boolean isInfoEnabled()
      • isTraceEnabled

        public boolean isTraceEnabled()