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 fde53a3..37fe63b 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 @@ -96,15 +96,16 @@ public static final class ScopeList { * the type already exists in the list, the original (potentially {@code null}) list reference * is returned. */ - @NullableDecl public static ScopeList addScope( - @NullableDecl ScopeList list, @NullableDecl ScopeType type) { + @NullableDecl + public static ScopeList addScope(@NullableDecl ScopeList list, @NullableDecl ScopeType type) { return (type != null && lookup(list, type) == null) ? new ScopeList(type, type.newScope(), list) : list; } /** Finds a scope instance for the given type in a possibly null scope list. */ - @NullableDecl public static LoggingScope lookup(@NullableDecl ScopeList list, ScopeType type) { + @NullableDecl + public static LoggingScope lookup(@NullableDecl ScopeList list, ScopeType type) { while (list != null) { if (type.equals(list.key)) { return list.scope; @@ -141,22 +142,26 @@ protected Builder() {} /** * Sets the tags to be used with the context. This method can be called at most once per - * builder. + * builder. Calling with a null value does nothing. */ @CanIgnoreReturnValue - public final Builder withTags(Tags tags) { + public final Builder withTags(@NullableDecl Tags tags) { checkState(this.tags == null, "tags already set"); - checkNotNull(tags, "tags"); - this.tags = tags; + if (tags != null) { + this.tags = tags; + } return this; } /** * Adds a single metadata key/value pair to the context. This method can be called multiple - * times on a builder. + * times on a builder. Calling with a null value does nothing. */ @CanIgnoreReturnValue - public final Builder withMetadata(MetadataKey key, T value) { + public final Builder withMetadata(MetadataKey key, @NullableDecl T value) { + if (value == null) { + return this; + } if (metadata == null) { metadata = ContextMetadata.builder(); } @@ -166,13 +171,14 @@ public final Builder withMetadata(MetadataKey key, T value) { /** * Sets the log level map to be used with the context being built. This method can be called at - * most once per builder. + * most once per builder. Calling with a null value does nothing. */ @CanIgnoreReturnValue - public final Builder withLogLevelMap(LogLevelMap logLevelMap) { + public final Builder withLogLevelMap(@NullableDecl LogLevelMap logLevelMap) { checkState(this.logLevelMap == null, "log level map already set"); - checkNotNull(logLevelMap, "log level map"); - this.logLevelMap = logLevelMap; + if (logLevelMap != null) { + this.logLevelMap = logLevelMap; + } return this; } diff --git a/api/src/test/java/com/google/common/flogger/testing/AbstractScopedLoggingContextTest.java b/api/src/test/java/com/google/common/flogger/testing/AbstractScopedLoggingContextTest.java index e72a94f..b780abf 100644 --- a/api/src/test/java/com/google/common/flogger/testing/AbstractScopedLoggingContextTest.java +++ b/api/src/test/java/com/google/common/flogger/testing/AbstractScopedLoggingContextTest.java @@ -128,6 +128,21 @@ public void testLoggedTags_areMerged() { backend.assertLogged(0).metadata().containsUniqueEntry(TAGS, expected); } + @Test + public void testNewContext_withNullTags_ignored() { + assertThat(getTagMap()).isEmpty(); + context + .newContext() + .withTags(null) + .run( + () -> { + assertThat(getTagMap()).isEmpty(); + markTestAsDone(); + }); + assertThat(getTagMap()).isEmpty(); + checkDone(); + } + @Test public void testNewContext_withMetadata() { assertThat(getMetadata()).hasSize(0); @@ -144,6 +159,21 @@ public void testNewContext_withMetadata() { checkDone(); } + @Test + public void testNewContext_withNullMetadata_ignored() { + assertThat(getMetadata()).hasSize(0); + context + .newContext() + .withMetadata(FOO_KEY, null) + .run( + () -> { + assertThat(getMetadata()).hasSize(0); + markTestAsDone(); + }); + assertThat(getMetadata()).hasSize(0); + checkDone(); + } + @Test public void testNewContext_withLogLevelMap() { assertLogging("foo.bar.Bar", Level.FINE).isFalse(); @@ -160,6 +190,21 @@ public void testNewContext_withLogLevelMap() { checkDone(); } + @Test + public void testNewContext_withNullLogLevelMap_ignored() { + assertLogging("foo.bar.Bar", Level.FINE).isFalse(); + context + .newContext() + .withLogLevelMap(null) + .run( + () -> { + assertLogging("foo.bar.Bar", Level.FINE).isFalse(); + markTestAsDone(); + }); + assertLogging("foo.bar.Bar", Level.FINE).isFalse(); + checkDone(); + } + @Test public void testNewContext_withMergedTags() { assertThat(getTagMap()).isEmpty();