Class ScopeType
- java.lang.Object
-
- com.google.common.flogger.context.ScopeType
-
- All Implemented Interfaces:
LoggingScopeProvider
public final class ScopeType extends Object implements LoggingScopeProvider
Singleton keys which identify different types of scopes which scoped contexts can be bound to.To bind a context to a scope type, create the context with that type:
ScopedLoggingContext.getInstance().newScope(REQUEST).run(() -> someTask(...));
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ScopeTypecreate(String name)Creates a new Scope type, which can be used as a singleton key to identify a scope during scoped context creation or logging.@Nullable LoggingScopegetCurrentScope()Returns the current scope (most likely via global or thread local state) ornullif there is no current scope.
-
-
-
Field Detail
-
REQUEST
public static final ScopeType REQUEST
The built in "request" scope. This can be bound to a scoped context in order to provide a distinct request scope for each context, allowing stateful logging operations (e.g. rate limiting) to be scoped to the current request.Enable a request scope using:
which runsScopedLoggingContext.getInstance().newScope(REQUEST).run(() -> scopedMethod(x, y, z));scopedMethodwith a new "request" scope for the duration of the context.Then use per-request rate limiting using:
Note that in order for the request scope to be applied to a log statement, thelogger.atWarning().atMostEvery(5, SECONDS).per(REQUEST).log("Some error message...");per(REQUEST)method must still be called; just being inside the request scope isn't enough.
-
-
Method Detail
-
create
public static ScopeType create(String name)
Creates a new Scope type, which can be used as a singleton key to identify a scope during scoped context creation or logging. Callers are expected to retain this key in a static field or return it via a static method. Scope types have singleton semantics and two scope types with the same name are NOT equivalent.- Parameters:
name- a debug friendly scope identifier (e.g. "my_batch_job").
-
getCurrentScope
public @Nullable LoggingScope getCurrentScope()
Description copied from interface:LoggingScopeProviderReturns the current scope (most likely via global or thread local state) ornullif there is no current scope.- Specified by:
getCurrentScopein interfaceLoggingScopeProvider
-
-