LogContext

public abstract class LogContext<LOGGER extends AbstractLogger<API>, API extends FloggerApi<API>> implements FloggerApi<API>, LogData

The base context for a logging statement, which implements the base logging API.

This class is an implementation of the base FloggerApi interface and acts as a holder for any state applied to the log statement during the fluent call sequence. The lifecycle of a logging context is very short; it is created by a logger, usually in response to a call to the at method, and normally lasts only as long as the log statement.

This class should not be visible to normal users of the logging API and it is only needed when extending the API to add more functionality. In order to extend the logging API and add methods to the fluent call chain, the LoggingApi interface should be extended to add any new methods, and this class should be extended to implement them. A new logger class will then be needed to return the extended context.

Logging contexts are not thread safe.

See also

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

Original Java code of Google Flogger

Constructors

Link copied to clipboard
protected void LogContext(Level level, boolean isForced)
Creates a logging context with the specified level, and with a timestamp obtained from the configured logging Platform.
protected void LogContext(Level level, boolean isForced, long timestampNanos)
Creates a logging context with the specified level and timestamp.

Types

Link copied to clipboard
public final class Key
The predefined metadata keys used by the default logging API.

Properties

Link copied to clipboard
public final Level level
The log level of the log statement that this context was created for.
Link copied to clipboard
The log site information for this log statement (set immediately prior to post-processing).
Link copied to clipboard
The template context if formatting is required (set only after post-processing).
Link copied to clipboard
public final long timestampNanos
The timestamp of the log statement that this context is associated with.

Functions

Link copied to clipboard
protected final void addMetadata<T>(FloggerMetadataKey<T> key, T value)
Adds the given key/value pair to this logging context.
Link copied to clipboard
protected abstract API api()
Returns the current API (which is just the concrete sub-type of this instance).
Link copied to clipboard
public final API atMostEvery(int n, TimeUnit unit)
Modifies the current log statement to be emitted at most once per specified time period.
Link copied to clipboard
public final API every(int n)
Modifies the current log statement to be emitted at most one-in-N times.
Link copied to clipboard
public final Array<Object> getArguments()
Returns the arguments to be formatted with the message.
Link copied to clipboard
public final Level getLevel()
Returns the log level for the current log statement.
Link copied to clipboard
public final Object getLiteralArgument()
Returns the single argument to be logged directly when no arguments were provided.
Link copied to clipboard
protected abstract LOGGER getLogger()
Returns the logger which created this context.
Link copied to clipboard
public final String getLoggerName()
Returns the logger name (which is usually a canonicalized class name) or null if not given.
Link copied to clipboard
public final FloggerLogSite getLogSite()
Returns the log site data for the current log statement.
Link copied to clipboard
protected abstract MessageParser getMessageParser()
Returns the message parser used for all log statements made through this logger.
Link copied to clipboard
public final Metadata getMetadata()
Returns any additional metadata for this log statement.
Link copied to clipboard
Returns a template key for this log statement, or null if the statement does not require formatting (in which case the message to be logged can be determined by calling getLiteralArgument).
Link copied to clipboard
public final long getTimestampMicros()
Link copied to clipboard
public final long getTimestampNanos()
Returns a nanosecond timestamp for the current log statement.
Link copied to clipboard
public final boolean isEnabled()
Returns true if logging is enabled at the level implied for this API, according to the current logger backend.
Link copied to clipboard
public final void log()
Terminal log statement when a message is not required.
public final void log(String msg)
Logs the given literal string without interpreting any argument placeholders.
public final void log(String message, byte p1)
public final void log(String message, char p1)
public final void log(String message, int p1)
public final void log(String message, long p1)
public final void log(String message, short p1)
public final void log(String message, boolean p1, boolean p2)
public final void log(String message, boolean p1, byte p2)
public final void log(String message, boolean p1, char p2)
public final void log(String message, boolean p1, double p2)
public final void log(String message, boolean p1, float p2)
public final void log(String message, boolean p1, int p2)
public final void log(String message, boolean p1, @Nullable() Object p2)
public final void log(String message, boolean p1, long p2)
public final void log(String message, boolean p1, short p2)
public final void log(String message, byte p1, boolean p2)
public final void log(String message, byte p1, byte p2)
public final void log(String message, byte p1, char p2)
public final void log(String message, byte p1, double p2)
public final void log(String message, byte p1, float p2)
public final void log(String message, byte p1, int p2)
public final void log(String message, byte p1, @Nullable() Object p2)
public final void log(String message, byte p1, long p2)
public final void log(String message, byte p1, short p2)
public final void log(String message, char p1, boolean p2)
public final void log(String message, char p1, byte p2)
public final void log(String message, char p1, char p2)
public final void log(String message, char p1, double p2)
public final void log(String message, char p1, float p2)
public final void log(String message, char p1, int p2)
public final void log(String message, char p1, @Nullable() Object p2)
public final void log(String message, char p1, long p2)
public final void log(String message, char p1, short p2)
public final void log(String message, double p1, boolean p2)
public final void log(String message, double p1, byte p2)
public final void log(String message, double p1, char p2)
public final void log(String message, double p1, double p2)
public final void log(String message, double p1, float p2)
public final void log(String message, double p1, int p2)
public final void log(String message, double p1, @Nullable() Object p2)
public final void log(String message, double p1, long p2)
public final void log(String message, double p1, short p2)
public final void log(String message, float p1, boolean p2)
public final void log(String message, float p1, byte p2)
public final void log(String message, float p1, char p2)
public final void log(String message, float p1, double p2)
public final void log(String message, float p1, float p2)
public final void log(String message, float p1, int p2)
public final void log(String message, float p1, @Nullable() Object p2)
public final void log(String message, float p1, long p2)
public final void log(String message, float p1, short p2)
public final void log(String message, int p1, boolean p2)
public final void log(String message, int p1, byte p2)
public final void log(String message, int p1, char p2)
public final void log(String message, int p1, double p2)
public final void log(String message, int p1, float p2)
public final void log(String message, int p1, int p2)
public final void log(String message, int p1, @Nullable() Object p2)
public final void log(String message, int p1, long p2)
public final void log(String message, int p1, short p2)
public final void log(String message, @Nullable() Object p1, boolean p2)
public final void log(String message, @Nullable() Object p1, byte p2)
public final void log(String message, @Nullable() Object p1, char p2)
public final void log(String message, @Nullable() Object p1, double p2)
public final void log(String message, @Nullable() Object p1, float p2)
public final void log(String message, @Nullable() Object p1, int p2)
public final void log(String message, @Nullable() Object p1, @Nullable() Object p2)
public final void log(String message, @Nullable() Object p1, long p2)
public final void log(String message, @Nullable() Object p1, short p2)
public final void log(String message, long p1, boolean p2)
public final void log(String message, long p1, byte p2)
public final void log(String message, long p1, char p2)
public final void log(String message, long p1, double p2)
public final void log(String message, long p1, float p2)
public final void log(String message, long p1, int p2)
public final void log(String message, long p1, @Nullable() Object p2)
public final void log(String message, long p1, long p2)
public final void log(String message, long p1, short p2)
public final void log(String message, short p1, boolean p2)
public final void log(String message, short p1, byte p2)
public final void log(String message, short p1, char p2)
public final void log(String message, short p1, double p2)
public final void log(String message, short p1, float p2)
public final void log(String message, short p1, int p2)
public final void log(String message, short p1, @Nullable() Object p2)
public final void log(String message, short p1, long p2)
public final void log(String message, short p1, short p2)
public final void log(String message, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3)
public final void log(String message, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3, @Nullable() Object p4)
public final void log(String msg, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3, @Nullable() Object p4, @Nullable() Object p5)
public final void log(String msg, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3, @Nullable() Object p4, @Nullable() Object p5, @Nullable() Object p6)
public final void log(String msg, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3, @Nullable() Object p4, @Nullable() Object p5, @Nullable() Object p6, @Nullable() Object p7)
public final void log(String msg, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3, @Nullable() Object p4, @Nullable() Object p5, @Nullable() Object p6, @Nullable() Object p7, @Nullable() Object p8)
public final void log(String msg, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3, @Nullable() Object p4, @Nullable() Object p5, @Nullable() Object p6, @Nullable() Object p7, @Nullable() Object p8, @Nullable() Object p9)
public final void log(String msg, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3, @Nullable() Object p4, @Nullable() Object p5, @Nullable() Object p6, @Nullable() Object p7, @Nullable() Object p8, @Nullable() Object p9, @Nullable() Object p10)
public final void log(String msg, @Nullable() Object p1, @Nullable() Object p2, @Nullable() Object p3, @Nullable() Object p4, @Nullable() Object p5, @Nullable() Object p6, @Nullable() Object p7, @Nullable() Object p8, @Nullable() Object p9, @Nullable() Object p10, Array<Object> rest)
Logs a message with formatted arguments (see log for details).
public final void log(String message, @Nullable() Object p1)
Logs a formatted representation of the given parameter, using the specified message template.
Link copied to clipboard
public final void logVarargs(String message, @Nullable() Array<Object> params)
Logs a formatted representation of values in the given array, using the specified message template.
Link copied to clipboard
protected abstract API noOp()
Returns the constant no-op logging API, which can be returned by fluent methods in extended logging contexts to efficiently disable logging.
Link copied to clipboard
public final API onAverageEvery(int n)
Modifies the current log statement to be emitted with likelihood 1 in n.
Link copied to clipboard
public API per(LoggingScopeProvider scopeProvider)
Aggregates stateful logging with respect to a scoped context determined by the given scope provider.
public final API per(Enum<? extends Object> key)
Aggregates stateful logging with respect to the given enum value.
public API per<T>(@Nullable() @Nullable() T key, LogPerBucketingStrategy<? extends Object> strategy)
Aggregates stateful logging with respect to a given key.
Link copied to clipboard
protected boolean postProcess(@Nullable() @Nullable() LogSiteKey logSiteKey)
A callback which can be overridden to implement post processing of logging contexts prior to passing them to the backend.
Link copied to clipboard
protected final void removeMetadata(FloggerMetadataKey<? extends Object> key)
Removes all key/value pairs with the specified key.
Link copied to clipboard
protected final boolean updateRateLimiterStatus(@Nullable() @Nullable() RateLimitStatus status)
Callback to allow custom log contexts to apply additional rate limiting behaviour.
Link copied to clipboard
public final boolean wasForced()
Returns whether this log statement should be emitted regardless of its log level or any other properties.
Link copied to clipboard
public final API with(FloggerMetadataKey<Boolean> key)
Sets a boolean metadata key constant to true for this log statement in a structured way that is accessible to logger backends.
public final API with<T>(FloggerMetadataKey<T> key, @Nullable() @Nullable() T value)
Associates a metadata key constant with a runtime value for this log statement in a structured way that is accessible to logger backends.
Link copied to clipboard
public final API withCause(Throwable cause)
Associates a Throwable instance with the current log statement, to be interpreted as the cause of this statement.
Link copied to clipboard
public final API withInjectedLogSite(FloggerLogSite logSite)
Sets the log site for the current log statement.
public final API withInjectedLogSite(String internalClassName, String methodName, int encodedLineNumber, @Nullable() @Nullable() String sourceFileName)
Internal method not for public use.
Link copied to clipboard
Creates a synthetic exception and attaches it as the "cause" of the log statement as a way to provide additional context for the logging call itself.