Class 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(...));
     
    See Also:
    Original Java code of Google Flogger
    • 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:

        
         ScopedLoggingContext.getInstance().newScope(REQUEST).run(() -> scopedMethod(x, y, z));
         
        which runs scopedMethod with a new "request" scope for the duration of the context.

        Then use per-request rate limiting using:

        
         logger.atWarning().atMostEvery(5, SECONDS).per(REQUEST).log("Some error message...");
         
        Note that in order for the request scope to be applied to a log statement, the 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").