LogPerBucketingStrategy

public abstract class LogPerBucketingStrategy<T>

Provides a strategy for "bucketing" a potentially unbounded set of log aggregation keys used by the LoggingApi.per(T, LogPerBucketingStrategy<T>) method.

When implementing new strategies not provided by this class, it is important to ensure that the apply() method returns values from a bounded set of instances wherever possible.

This is important because the returned values are held persistently for potentially many different log sites. If a different instance is returned each time apply() is called, a different instance will be held in each log site. This multiplies the amount of memory that is retained indefinitely by any use of LoggingApi.per(T, LogPerBucketingStrategy<T>).

One way to handle arbitrary key types would be to create a strategy which "interns" instances in some way, to produce singleton identifiers. Unfortunately interning can itself be a cause of unbounded memory leaks, so a bucketing strategy wishing to perform interning should probably support a user defined maximum capacity to limit the overall risk. If too many instances are seen, the strategy should begin to return null (and log an appropriate warning).

The additional complexity created by this approach really tells us that types which require interning in order to be used as aggregation keys should be considered unsuitable, and callers should seek alternatives.

See also

<a href="https://github.com/google/flogger/blob/cb9e836a897d36a78309ee8badf5cad4e6a2d3d8/api/src/main/java/com/google/common/flogger/LogPerBucketingStrategy.java">

Original Java code of Google Flogger

Constructors

Link copied to clipboard
protected void LogPerBucketingStrategy(String name)
Instantiates a strategy with the specified name (used for debugging).

Functions

Link copied to clipboard
@Nullable()
protected abstract Object apply(T key)
Maps a log aggregation key from a potentially unbounded set of key values to a bounded set of instances.
Link copied to clipboard
public final static LogPerBucketingStrategy<Object> byClass()
A strategy which uses the Class of the given key for log aggregation.
Link copied to clipboard
A strategy which uses the Class name of the given key for log aggregation.
Link copied to clipboard
public final static LogPerBucketingStrategy<Object> byHashCode(int maxBuckets)
A strategy which uses the hashCode() of a given key, modulo maxBuckets, for log aggregation.
Link copied to clipboard
public final static LogPerBucketingStrategy<Object> forKnownKeys(Iterable<? extends Object> knownKeys)
A strategy defined for some given set of known keys.
Link copied to clipboard
A strategy to use only if the set of log aggregation keys is known to be a strictly bounded set of instances with singleton semantics.
Link copied to clipboard
public final String toString()