From 0e3818bd6fed8a27b5d45ab0d397933a2c8473d4 Mon Sep 17 00:00:00 2001 From: David Beaumont Date: Mon, 31 Jul 2023 10:03:56 -0700 Subject: [PATCH] Adding a static helper to expose the ScopeType constructor for ScopedLoggingContext. This was omitted when ScopeType support was added. Fixes #353 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/flogger/pull/353 from hagbard:work 932467a0ecdb16414e2bd02829d99eccb99f9264 PiperOrigin-RevId: 552515076 --- .../context/ScopedLoggingContexts.java | 37 +++++++++++++------ 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/api/src/main/java/com/google/common/flogger/context/ScopedLoggingContexts.java b/api/src/main/java/com/google/common/flogger/context/ScopedLoggingContexts.java index 48277a1a..a5a64372 100644 --- a/api/src/main/java/com/google/common/flogger/context/ScopedLoggingContexts.java +++ b/api/src/main/java/com/google/common/flogger/context/ScopedLoggingContexts.java @@ -33,16 +33,12 @@ public final class ScopedLoggingContexts { private static boolean warnOnFailure(boolean wasSuccessful) { if (!wasSuccessful && !ScopedLoggingContext.getInstance().isNoOp()) { - logger - .atWarning() - .atMostEvery(5, MINUTES) - .withStackTrace(StackSize.SMALL) - .log( - "***** An attempt to add metadata to the current logging context failed. *****\n" - + "Calls to static methods in 'ScopedLoggingContexts' may fail when there is no" - + " existing context available.\n" - + "To ensure metadata is available to log statements, create a new context via" - + " 'ScopedLoggingContexts.newContext()' and add metadata to it explicitly.\n"); + logger.atWarning().atMostEvery(5, MINUTES).withStackTrace(StackSize.SMALL).log( + "***** An attempt to add metadata to the current logging context failed. *****\n" + + "Calls to static methods in 'ScopedLoggingContexts' may fail when there is no" + + " existing context available.\n" + + "To ensure metadata is available to log statements, create a new context via" + + " 'ScopedLoggingContexts.newContext()' and add metadata to it explicitly.\n"); } return wasSuccessful; } @@ -51,7 +47,7 @@ private ScopedLoggingContexts() {} /** * Creates a new {@link ScopedLoggingContext.Builder} to which additional logging metadata can be - * attached before being installed or used to wrap some existing code. + * attached, before being installed or used to wrap some existing code. * *
{@code
    * Foo result = ScopedLoggingContexts.newContext()
@@ -63,6 +59,25 @@ public static ScopedLoggingContext.Builder newContext() {
     return ScopedLoggingContext.getInstance().newContext();
   }
 
+  /**
+   * Creates a new {@link ScopedLoggingContext.Builder} bound to the given {@link ScopeType}, to
+   * which additional logging metadata can be attached, before being installed or used to wrap some
+   * existing code.
+   *
+   * 

Specifying a scope type means that any tasks run within this context will have a different + * scope attached, ensuring that stateful logging (e.g. rate limiting) will be handled separately + * for each invocation. This can help ensure that even rare situations will appear in logs. + * + *

{@code
+   * ScopedLoggingContexts.newContext(ScopeType.REQUEST)
+   *     .withTags(Tags.of("request_id", request.getId))
+   *     .run(() -> handleRequest(request));
+   * }
+ */ + public static ScopedLoggingContext.Builder newContext(ScopeType scopeType) { + return ScopedLoggingContext.getInstance().newContext(scopeType); + } + /** * Adds tags by modifying the current context (if one exists). *