Java: Only log first error occurrence (with hourly reset) I have a redis caching server. If that caching server is down, my app will query the database directly.

I want to know from the logs if my caching server is not reachable. However, if I add a log error, it would completely spam my logfiles if a high amount of users would try to access data.

As a result, I would like to only log the very first error occurrence, resetting only if no errors occurred for an hour.

Is this a feasable approach at all? If so, please review my implementation:

public class CustomCacheErrorHandler implements CacheErrorHandler {

   private Logger logger = LoggerFactory.getLogger(this.getClass());
   private long errorOccurred;
   private final long ONEHOUR = 3600000;

   public void handleCacheGetError(RuntimeException exception, Cache cache, Object key) {
      if (errorOccurred &lt; (System.currentTimeMillis() - ONEHOUR))
         logger.error("Error while getting cache " + cache.getName() + " for Key " + key);
      errorOccurred = System.currentTimeMillis()
   }
}


Using currentTimeMillis over System.nanoTime() and a final long ONEHOUR over TimeUnit.HOURS.toMillis(1) for performance.

Thanks in advance!
Your code currently checks when the last error occurred. This might not be what you actually want. Assuming that an error occurs every 30 minutes, I would expect to have a log entry every 60 minutes. Your current code produces one log entry at the beginning, and no more log entries after that.

I once wrote similar code, and I separated the code into two Java classes: the Throttler for the actual algorithm and the ThrottledLogger for using the algorithm to throttle log messages.

I'm still happy with that old code, and it had the following additional features:


It allows several events before throttling kicks in.
When throttling kicks in, that is logged as well ("further log messages will be suppressed").
When throttling has finished, that is logged as well ("suppressed {} messages").


With these changes, the log messages do not hide any important information. The throttled logger I used was created like this:

new ThrottledLogger(
    logger,
    5,  // number of allowed messages
    1, TimeUnit.HOUR  // 1 more message every hour
);


With this information you should be able to write the same code as I did. Be sure to write some unit tests for the throttling algorithm, to demonstrate that it works indeed like you want it to work.

If you don't need this additional complexity, you should at least move the errorOccurred = assignment into the body of the if statement.