From c4dd4d902efb4c0bc8e6fd832cc3d23cdf7dcecb Mon Sep 17 00:00:00 2001 From: dbeaumont Date: Sun, 14 Mar 2021 14:50:56 -0700 Subject: [PATCH] Removing deprecated methods RELNOTES=Removing deprecated methods PiperOrigin-RevId: 362828751 --- .../flogger/context/ScopedLoggingContext.java | 104 ------------------ .../context/ScopedLoggingContextTest.java | 17 ++- 2 files changed, 11 insertions(+), 110 deletions(-) diff --git a/api/src/main/java/com/google/common/flogger/context/ScopedLoggingContext.java b/api/src/main/java/com/google/common/flogger/context/ScopedLoggingContext.java index 7f950ef8..01befc20 100644 --- a/api/src/main/java/com/google/common/flogger/context/ScopedLoggingContext.java +++ b/api/src/main/java/com/google/common/flogger/context/ScopedLoggingContext.java @@ -303,110 +303,6 @@ protected ScopedLoggingContext() {} @CheckReturnValue public abstract Builder newScope(); - /** - * Installs a new context to which additional logging metadata can be attached. The caller is - * required to invoke {@link LoggingContextCloseable#close() close()} 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. - * - *
{@code
-   * ScopedLoggingContext ctx = ScopedLoggingContext.getInstance();
-   * try (LoggingContextCloseable context = ctx.withNewScope()) {
-   *   // Now add metadata to the installed context (returns false if not supported).
-   *   ctx.addTags(Tags.of("my_tag", someValue));
-   *
-   *   // Any 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 {@link #wrap(Runnable)} or {@link #run(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. In particular: - * - *

{@code
-   * ScopedLoggingContext ctx = ScopedLoggingContext.getInstance();
-   * logger.atInfo().log("Some log statement with existing tags and behaviour...");
-   * try (LoggingContextCloseable context = ctx.withNewScope()) {
-   *   logger.atInfo().log("This log statement is the same as the first...");
-   *   ctx.addTags(Tags.of("my_tag", someValue));
-   *   logger.atInfo().log("This log statement has the new tag present...");
-   * }
-   * logger.atInfo().log("This log statement is the same as the first...");
-   * }
- * - *

Note that the returned {@link LoggingContextCloseable} is not required to enforce the - * correct closure of nested contexts, and while it is permitted to throw a {@link - * InvalidLoggingScopeStateException} in the face of mismatched or invalid usage, it is not - * required. - * - * @deprecated Prefer using {@link #newScope()} and the builder API to configure contexts before - * they are installed. - */ - // TODO(dbeaumont): Consider using @InlineMe to migrate these depending on usage patterns - // (e.g. don't just inline if it's a common pattern to do ctx.withNewScope() --> ctx.addTags() in - // the same bit of code, or maybe catch that with an errorprone check). - @Deprecated - @MustBeClosed - @CheckReturnValue - public final LoggingContextCloseable withNewScope() { - return newScope().install(); - } - - /** - * Wraps a runnable so it will execute within a new context. - * - * @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). - * @deprecated Prefer using {@link #newScope()} and the builder API to configure contexts before - * they are installed. - */ - @Deprecated - @CheckReturnValue - public final Runnable wrap(final Runnable r) { - return newScope().wrap(r); - } - - /** - * Wraps a callable so it will execute within a new context. - * - * @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). - * @deprecated Prefer using {@link #newScope()} and the builder API to configure contexts before - * they are installed. - */ - @Deprecated - @CheckReturnValue - public final Callable wrap(final Callable c) { - return newScope().wrap(c); - } - - /** - * Runs a runnable directly within a new context. - * - * @deprecated Prefer using {@link #newScope()} and the builder API to configure contexts before - * they are installed. - */ - @Deprecated - public final void run(Runnable r) { - newScope().run(r); - } - - /** - * Calls a callable directly within a new context. - * - * @deprecated Prefer using {@link #newScope()} and the builder API to configure contexts before - * they are installed. - */ - @Deprecated - public final R call(Callable c) throws Exception { - return newScope().call(c); - } - /** * Adds tags to the current set of log tags for the current context. Tags are merged together and * existing tags cannot be modified. This is deliberate since two pieces of code may not know diff --git a/api/src/test/java/com/google/common/flogger/context/ScopedLoggingContextTest.java b/api/src/test/java/com/google/common/flogger/context/ScopedLoggingContextTest.java index 57daa521..e32d3c21 100644 --- a/api/src/test/java/com/google/common/flogger/context/ScopedLoggingContextTest.java +++ b/api/src/test/java/com/google/common/flogger/context/ScopedLoggingContextTest.java @@ -25,7 +25,8 @@ import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -// TODO: Implement an abstract test suite to allow new implementations to be tested easily. +// Most of the real functionality is tested for each implementation via +// testing.AbstractScopedLoggingContextTest. @RunWith(JUnit4.class) public class ScopedLoggingContextTest { @@ -59,7 +60,9 @@ public boolean addTags(Tags tags) { @Test public void testErrorHandlingWithoutUserError() { InvalidLoggingContextStateException e = - assertThrows(InvalidLoggingContextStateException.class, () -> ERROR_CONTEXT.run(() -> {})); + assertThrows( + InvalidLoggingContextStateException.class, + () -> ERROR_CONTEXT.newScope().run(() -> {})); assertThat(e).hasCauseThat().isInstanceOf(IllegalArgumentException.class); assertThat(e).hasCauseThat().hasMessageThat().isEqualTo("BAD CONTEXT"); } @@ -70,10 +73,12 @@ public void testErrorHandlingWithUserError() { assertThrows( IllegalArgumentException.class, () -> - ERROR_CONTEXT.run( - () -> { - throw new IllegalArgumentException("User error"); - })); + ERROR_CONTEXT + .newScope() + .run( + () -> { + throw new IllegalArgumentException("User error"); + })); assertThat(e).hasMessageThat().isEqualTo("User error"); }