public class RepeatGuardLogger
extends java.lang.Object
RepeatGuard.
Typically you want to create one RepeatGuardLogger for each guarded kind of log message (= logging line in your code).
Only if you want to control the repetition of different kinds of log messages (at different places in your code) using the same counter and timer
(which could mean that one kind of log may always be suppressed while another type may always be logged) then you should reuse one instance
of this class for different lines of logging in your code.
There seems to be no real use case for passing different loggers to one repeat guard, even though this was described in the original design.
Therefore we plan to remove #log(Logger, Level, String) and #logAndIncrement(Logger, Level, String) in the future.
If you feel they are useful (because you have a case where the logger cannot or should not be passed in the constructor) then
we can keep these methods as alternatives to the recommended passing of the logger in the constructor. Please report this to ACS.
| Modifier and Type | Field and Description |
|---|---|
protected RepeatGuard |
guard |
| Constructor and Description |
|---|
RepeatGuardLogger(AcsLogger logger,
long interval,
java.util.concurrent.TimeUnit timeUnit,
int maxRepetitions)
Constructor for a time and counter based repeat guard logger.
|
| Modifier and Type | Method and Description |
|---|---|
static RepeatGuardLogger |
createCounterBasedRepeatGuardLogger(AcsLogger logger,
int maxRepetitions)
Factory method for a repeat guard logger which only uses a counter of skipped logs, regardless of the time passed since the last log.
|
static RepeatGuardLogger |
createTimeBasedRepeatGuardLogger(AcsLogger logger,
long interval,
java.util.concurrent.TimeUnit timeUnit)
Factory method for a repeat guard logger which only uses a timer, regardless of the number of skipped logs.
|
boolean |
isLoggingEnabled()
Checks if the internal repeat guard allows a log.
|
void |
log(java.util.logging.Level level,
java.lang.String message)
Logs the message at the given level, unless the internal
RepeatGuard prevents this based on the timer and/or log record counter. |
void |
log(java.util.logging.Level level,
java.lang.String message,
java.lang.Throwable thr)
Same as
log(Level, String) but with additional Throwable to be logged. |
protected final RepeatGuard guard
public RepeatGuardLogger(AcsLogger logger, long interval, java.util.concurrent.TimeUnit timeUnit, int maxRepetitions)
If only time or counter should apply, but not both together, it is better to use the factory methods
createTimeBasedRepeatGuardLogger(AcsLogger, long, TimeUnit)
or createCounterBasedRepeatGuardLogger(AcsLogger, int).
However it is also possible to use negative values for the quantities that should not be considered.
logger - The logger to be used ininterval - Time interval (in timeUnit units).timeUnit - Time unit of interval parameter.maxRepetitions - Maximum number of skipped repetitions.java.lang.IllegalArgumentException - if interval <= 0 && maxRepetitions <= 0public static RepeatGuardLogger createTimeBasedRepeatGuardLogger(AcsLogger logger, long interval, java.util.concurrent.TimeUnit timeUnit)
java.lang.IllegalArgumentException - if interval <= 0RepeatGuardLogger(AcsLogger, long, TimeUnit, int)public static RepeatGuardLogger createCounterBasedRepeatGuardLogger(AcsLogger logger, int maxRepetitions)
java.lang.IllegalArgumentException - if maxRepetitions <= 0RepeatGuardLogger(AcsLogger, long, TimeUnit, int)public void log(java.util.logging.Level level,
java.lang.String message)
RepeatGuard prevents this based on the timer and/or log record counter.
If a log record counter is active, it will be advanced, which corresponds to RepeatGuard.checkAndIncrement().
(Note that following the same terminology as RepeatGuard, this method would have to be called logAndIncrement;
it is simply called log though because here we don't support the variant of having a counter enabled without using it.)Logger.log(Level, String)public void log(java.util.logging.Level level,
java.lang.String message,
java.lang.Throwable thr)
log(Level, String) but with additional Throwable to be logged.Logger.log(Level, String, Throwable)public boolean isLoggingEnabled()
log methods to be used if
Logger.log(java.util.logging.LogRecord),
which are not exposed by this RepeatGuardLogger
if (isLoggingEnabled()) {myLogger.blabla}.
The internal log counter (if present) will be incremented the same way as for one call to, say, log(Level, String).
Note that you might be better off using RepeatGuard directly if this method is the only one you need from this RepeatGuardLogger.
Using this method makes sense though for a mix of logs, some to be made via the RepeatGuardLogger.log methods,
and others directly via the logger surrounded by a call to this isLoggingEnabled