How to log repeated warnings only once

后端 未结 4 2147
执念已碎
执念已碎 2021-02-12 23:36

There is a pattern that happens every now and then. I have a method called many times, and it contains this snippet:

Foo foo = getConfiguredFoo();
if (foo == nu         


        
4条回答
  •  我寻月下人不归
    2021-02-13 00:17

    If this is the only thing you want to print one time, then using a saved boolean would be your best bet. If you wanted something you could use throughout your project, I have created something that may be useful. I just created a Java class that uses a log4j logger instance. When I want to log a message, I just do something like this:

    LogConsolidated.log(logger, Level.WARN, 5000, "File: " + f + " not found.", e);
    

    Instead of:

    logger.warn("File: " + f + " not found.", e);
    

    Which makes it log a maximum of 1 time every 5 seconds, and prints how many times it should have logged (e.g. |x53|). Obviously, you can make it so you don't have as many parameters, or pull the level out by doing log.warn or something, but this works for my use case.

    For you (if you only want to print one time, every time) this is overkill, but you can still do it by passing in something like: Long.MAX_LONG in as the 3rd parameter. I like the flexibility to be able to determine frequency for each specific log message (hence the parameter). For example, this would accomplish what you want:

    LogConsolidated.log(logger, Level.WARN, Long.MAX_LONG, "File: " + f + " not found.", e);
    

    Here is the LogConsolidated class:

    import java.util.HashMap;
    
    import org.apache.log4j.Level;
    import org.apache.log4j.Logger;
    
    public class LogConsolidated {
    
        private static HashMap lastLoggedTime = new HashMap<>();
    
        /**
         * Logs given message to given logger as long as:
         * 
      *
    • A message (from same class and line number) has not already been logged within the past timeBetweenLogs.
    • *
    • The given level is active for given logger.
    • *
    * Note: If messages are skipped, they are counted. When timeBetweenLogs has passed, and a repeat message is logged, * the count will be displayed. * @param logger Where to log. * @param level Level to log. * @param timeBetweenLogs Milliseconds to wait between similar log messages. * @param message The actual message to log. * @param t Can be null. Will log stack trace if not null. */ public static void log(Logger logger, Level level, long timeBetweenLogs, String message, Throwable t) { if (logger.isEnabledFor(level)) { String uniqueIdentifier = getFileAndLine(); TimeAndCount lastTimeAndCount = lastLoggedTime.get(uniqueIdentifier); if (lastTimeAndCount != null) { synchronized (lastTimeAndCount) { long now = System.currentTimeMillis(); if (now - lastTimeAndCount.time < timeBetweenLogs) { lastTimeAndCount.count++; return; } else { log(logger, level, "|x" + lastTimeAndCount.count + "| " + message, t); } } } else { log(logger, level, message, t); } lastLoggedTime.put(uniqueIdentifier, new TimeAndCount()); } } private static String getFileAndLine() { StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); boolean enteredLogConsolidated = false; for (StackTraceElement ste : stackTrace) { if (ste.getClassName().equals(LogConsolidated.class.getName())) { enteredLogConsolidated = true; } else if (enteredLogConsolidated) { // We have now file/line before entering LogConsolidated. return ste.getFileName() + ":" + ste.getLineNumber(); } } return "?"; } private static void log(Logger logger, Level level, String message, Throwable t) { if (t == null) { logger.log(level, message); } else { logger.log(level, message, t); } } private static class TimeAndCount { long time; int count; TimeAndCount() { this.time = System.currentTimeMillis(); this.count = 0; } } }

提交回复
热议问题