All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.clickzetta.platform.util.LogThrottler Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package com.clickzetta.platform.util;

import org.slf4j.Logger;

import java.time.Instant;

public class LogThrottler {

  private final Logger log;
  private long lastLoggedTimeSecs = -1;

  public LogThrottler(Logger log) {
    this.log = log;
  }


  public void trace(long seconds, String msg) {
    if (shouldLog(seconds)) {
      log.trace(msg);
    }
  }


  public void trace(long seconds, String format, Object arg) {
    if (shouldLog(seconds)) {
      log.trace(format, arg);
    }
  }


  public void trace(long seconds, String format, Object arg1, Object arg2) {
    if (shouldLog(seconds)) {
      log.trace(format, arg1, arg2);
    }
  }


  public void trace(long seconds, String format, Object... arguments) {
    if (shouldLog(seconds)) {
      log.trace(format, arguments);
    }
  }


  public void warn(long seconds, String msg) {
    if (shouldLog(seconds)) {
      log.warn(msg);
    }
  }


  public void warn(long seconds, String format, Object arg) {
    if (shouldLog(seconds)) {
      log.warn(format, arg);
    }
  }


  public void warn(long seconds, String format, Object arg1, Object arg2) {
    if (shouldLog(seconds)) {
      log.warn(format, arg1, arg2);
    }
  }


  public void warn(long seconds, String format, Object... arguments) {
    if (shouldLog(seconds)) {
      log.warn(format, arguments);
    }
  }


  public void error(long seconds, String msg) {
    if (shouldLog(seconds)) {
      log.error(msg);
    }
  }


  public void error(long seconds, String format, Object arg) {
    if (shouldLog(seconds)) {
      log.error(format, arg);
    }
  }


  public void error(long seconds, String format, Object arg1, Object arg2) {
    if (shouldLog(seconds)) {
      log.error(format, arg1, arg2);
    }
  }

  public void error(long seconds, String format, Object... arguments) {
    if (shouldLog(seconds)) {
      log.error(format, arguments);
    }
  }


  public void info(long seconds, String msg) {
    if (shouldLog(seconds)) {
      log.info(msg);
    }
  }


  public void info(long seconds, String format, Object arg) {
    if (shouldLog(seconds)) {
      log.info(format, arg);
    }
  }


  public void info(long seconds, String format, Object arg1, Object arg2) {
    if (shouldLog(seconds)) {
      log.info(format, arg1, arg2);
    }
  }


  public void info(long seconds, String format, Object... arguments) {
    if (shouldLog(seconds)) {
      log.info(format, arguments);
    }
  }


  public void debug(long seconds, String msg) {
    if (shouldLog(seconds)) {
      log.debug(msg);
    }
  }


  public void debug(long seconds, String format, Object arg) {
    if (shouldLog(seconds)) {
      log.debug(format, arg);
    }
  }


  public void debug(long seconds, String format, Object arg1, Object arg2) {
    if (shouldLog(seconds)) {
      log.debug(format, arg1, arg2);
    }
  }


  public void debug(long seconds, String format, Object... arguments) {
    if (shouldLog(seconds)) {
      log.debug(format, arguments);
    }
  }


  private synchronized boolean shouldLog(long throttlingIntervalSecs) {
    long nowSecs = Instant.now().getEpochSecond();
    if (lastLoggedTimeSecs == -1 || lastLoggedTimeSecs + throttlingIntervalSecs < nowSecs) {
      lastLoggedTimeSecs = nowSecs;
      return true;
    }
    return false;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy