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

com.wavefront.common.logger.SharedRateLimitingLogger Maven / Gradle / Ivy

There is a newer version: 2023-22.3
Show newest version
package com.wavefront.common.logger;

import com.google.common.util.concurrent.RateLimiter;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

/**
 * A rate-limiting logger that can be shared between multiple threads
 * that use the same context key.
 */
@SuppressWarnings("UnstableApiUsage")
public class SharedRateLimitingLogger extends DelegatingLogger {
  private static final Map SHARED_CACHE = new ConcurrentHashMap<>();

  private final RateLimiter rateLimiter;

  /**
   * @param delegate     Delegate logger.
   * @param context      Shared context key.
   * @param rateLimit    Rate limit (messages per second)
   */
  public SharedRateLimitingLogger(Logger delegate, String context, double rateLimit) {
    super(delegate);
    this.rateLimiter = SHARED_CACHE.computeIfAbsent(context, x -> RateLimiter.create(rateLimit));
  }

  /**
   * @param level   log level.
   * @param message string to write to log.
   */
  @Override
  public void log(Level level, String message) {
    if (!delegate.isLoggable(level)) {
      return;
    }
    if (rateLimiter.tryAcquire()) {
      log(new LogRecord(level, message));
    }
  }

  /**
   * @param level           Log level.
   * @param messageSupplier A function, which when called, produces the desired log message.
   */
  @Override
  public void log(Level level, Supplier messageSupplier) {
    if (!delegate.isLoggable(level)) {
      return;
    }
    if (rateLimiter.tryAcquire()) {
      log(new LogRecord(level, messageSupplier.get()));
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy