Skip to content

Commit

Permalink
Removing deprecated methods
Browse files Browse the repository at this point in the history
RELNOTES=Removing deprecated methods
PiperOrigin-RevId: 362828751
  • Loading branch information
hagbard authored and Flogger Team committed Mar 14, 2021
1 parent 8442b81 commit c4dd4d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
* <em>required</em> 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.
*
* <pre>{@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...");
* }
* }</pre>
*
* <p>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.
*
* <p>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:
*
* <pre>{@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...");
* }</pre>
*
* <p>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 <R> Callable<R> wrap(final Callable<R> 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> R call(Callable<R> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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");
}
Expand All @@ -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");
}

Expand Down

0 comments on commit c4dd4d9

Please sign in to comment.