Class ScopedLoggingContext.Builder
- java.lang.Object
-
- io.spine.logging.flogger.context.ScopedLoggingContext.Builder
-
- Enclosing class:
- ScopedLoggingContext
public abstract static class ScopedLoggingContext.Builder extends Object
A fluent builder API for creating and installing new context scopes. This API should be used whenever the metadata to be added to a scope is known at the time the scope is created.This class is intended to be used only as part of a fluent statement, and retaining a reference to a builder instance for any length of time is not recommended.
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedBuilder()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <R> Rcall(Callable<R> c)Calls aCallabledirectly within a new context installed from this builder.<R> RcallUnchecked(Callable<R> c)Calls aCallabledirectly within a new context installed from this builder, wrapping any checked exceptions with aRuntimeException.protected @Nullable LogLevelMapgetLogLevelMap()Returns the configured log level map, or null.protected @Nullable ContextMetadatagetMetadata()Returns the configured context metadata, or null.protected @Nullable TagsgetTags()Returns the configured tags, or null.abstract ScopedLoggingContext.LoggingContextCloseableinstall()Installs a new context based on the state of the builder.voidrun(Runnable r)Runs a runnable directly within a new context installed from this builder.ScopedLoggingContext.BuilderwithLogLevelMap(LogLevelMap logLevelMap)Sets the log level map to be used with the context being built.<T> ScopedLoggingContext.BuilderwithMetadata(FloggerMetadataKey<T> key, T value)Adds a single metadata key/value pair to the context.ScopedLoggingContext.BuilderwithTags(Tags tags)Sets the tags to be used with the context.Runnablewrap(Runnable r)Wraps a runnable so it will execute within a new context based on the state of the builder.<R> Callable<R>wrap(Callable<R> c)Wraps a callable so it will execute within a new context based on the state of the builder.
-
-
-
Method Detail
-
withTags
@CanIgnoreReturnValue public final ScopedLoggingContext.Builder withTags(Tags tags)
Sets the tags to be used with the context. This method can be called at most once per builder.
-
withMetadata
@CanIgnoreReturnValue public final <T> ScopedLoggingContext.Builder withMetadata(FloggerMetadataKey<T> key, T value)
Adds a single metadata key/value pair to the context. This method can be called multiple times on a builder.
-
withLogLevelMap
@CanIgnoreReturnValue public final ScopedLoggingContext.Builder withLogLevelMap(LogLevelMap logLevelMap)
Sets the log level map to be used with the context being built. This method can be called at most once per builder.
-
wrap
public final Runnable wrap(Runnable r)
Wraps a runnable so it will execute within a new context based on the state of the builder. Note that each time this runnable is executed, a new context will be installed extending from the currently installed context at the time of execution.- Throws:
InvalidLoggingScopeStateException- if the context created during this method cannot be closed correctly (e.g. if a nested context has also been opened, but not closed).
-
wrap
public final <R> Callable<R> wrap(Callable<R> c)
Wraps a callable so it will execute within a new context based on the state of the builder. Note that each time this runnable is executed, a new context will be installed extending from the currently installed context at the time of execution.- Throws:
InvalidLoggingScopeStateException- if the context created during this method cannot be closed correctly (e.g. if a nested context has also been opened, but not closed).
-
run
public final void run(Runnable r)
Runs a runnable directly within a new context installed from this builder.
-
call
@CanIgnoreReturnValue public final <R> R call(Callable<R> c) throws Exception
Calls aCallabledirectly within a new context installed from this builder.- Throws:
Exception
-
callUnchecked
@CanIgnoreReturnValue public final <R> R callUnchecked(Callable<R> c)
Calls aCallabledirectly within a new context installed from this builder, wrapping any checked exceptions with aRuntimeException.
-
install
@MustBeClosed public abstract ScopedLoggingContext.LoggingContextCloseable install()
Installs a new context based on the state of the builder. The caller is required to invokeclose()on the returned instances in the reverse order to which they were obtained. For JDK 1.7 and above, this is best achieved by using a try-with-resources construction in the calling code.try (LoggingContextCloseable ctx = ScopedLoggingContext.getInstance() .newContext().withTags(Tags.of("my_tag", someValue).install()) { // Logging by code called from within this context will contain the additional metadata. logger.atInfo().log("Log message should contain tag value..."); }To avoid the need to manage contexts manually, it is strongly recommended that the helper methods, such as
wrap(Runnable)orrun(Runnable)are used to simplify the handling of contexts. This method is intended primarily to be overridden by context implementations rather than being invoked as a normal part of context use.An implementation of scoped contexts must preserve any existing metadata when a context is opened, and restore the previous state when it terminates.
Note that the returned
ScopedLoggingContext.LoggingContextCloseableis not required to enforce the correct closure of nested contexts, and while it is permitted to throw aInvalidLoggingScopeStateExceptionin the face of mismatched or invalid usage, it is not required.
-
getTags
protected final @Nullable Tags getTags()
Returns the configured tags, or null. This method may do work and results should be cached by context implementations.
-
getMetadata
protected final @Nullable ContextMetadata getMetadata()
Returns the configured context metadata, or null. This method may do work and results should be cached by context implementations.
-
getLogLevelMap
protected final @Nullable LogLevelMap getLogLevelMap()
Returns the configured log level map, or null. This method may do work and results should be cached by context implementations.
-
-