Skip to content

Commit

Permalink
Make a few tweaks to log4j2 backend and tests.
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 398488800
  • Loading branch information
cgdecker authored and Flogger Team committed Sep 23, 2021
1 parent 6789d53 commit 4f0ea71
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -202,25 +202,23 @@ private static void appendLogData(LogData data, StringBuilder out) {
}

private static final MetadataHandler<MetadataKey.KeyValueHandler> HANDLER =
MetadataHandler.builder(
(MetadataHandler.ValueHandler<Object, MetadataKey.KeyValueHandler>)
(key, value, kvh) -> {
if (key.getClass().equals(LogContext.Key.TAGS.getClass())) {
processTags(key, value, kvh);
} else {
// In theory a user can define a custom tag and use it as a MetadataKey. Those
// keys shall be treated in the same way as LogContext.Key.TAGS when used as a
// MetadataKey. Might be removed if visibility of MetadataKey#clazz changes.
if (value instanceof Tags) {
processTags(key, value, kvh);
} else {
ValueQueue.appendValues(key.getLabel(), value, kvh);
}
}
})
.setDefaultRepeatedHandler(
(key, values, kvh) -> values.forEachRemaining(v -> kvh.handle(key.getLabel(), v)))
.build();
MetadataHandler.builder(Log4j2LogEventUtil::handleMetadata).build();

private static void handleMetadata(
MetadataKey<Object> key, Object value, MetadataKey.KeyValueHandler kvh) {
if (key.getClass().equals(LogContext.Key.TAGS.getClass())) {
processTags(key, value, kvh);
} else {
// In theory a user can define a custom tag and use it as a MetadataKey. Those
// keys shall be treated in the same way as LogContext.Key.TAGS when used as a
// MetadataKey. Might be removed if visibility of MetadataKey#clazz changes.
if (value instanceof Tags) {
processTags(key, value, kvh);
} else {
ValueQueue.appendValues(key.getLabel(), value, kvh);
}
}
}

private static void processTags(
MetadataKey<Object> key, Object value, MetadataKey.KeyValueHandler kvh) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
import static org.apache.logging.log4j.Level.INFO;
import static org.apache.logging.log4j.Level.TRACE;

import com.google.common.flogger.GoogleLogContext;
import com.google.common.flogger.GoogleLogger;
import com.google.common.flogger.LogContext;
import com.google.common.flogger.MetadataKey;
import com.google.common.flogger.backend.LoggerBackend;
import com.google.common.flogger.context.ContextDataProvider;
import com.google.common.flogger.context.ScopedLoggingContext;
import com.google.common.flogger.context.Tags;
import com.google.common.flogger.testing.TestLogger;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -67,16 +66,16 @@ public class Log4j2ScopedLoggingTest {
private static final MetadataKey<String> ID_KEY2 = MetadataKey.single("id", String.class);
private static final MetadataKey<String> TAGS = MetadataKey.single("tags", String.class);
private static final MetadataKey<String> REP_TAGS = MetadataKey.repeated("tags", String.class);
private static GoogleLogger googleLogger;

private Logger logger;
private CapturingAppender appender;
private LoggerBackend backend;
private List<LogEvent> events;
private TestLogger testLogger;

@BeforeClass
public static void init() throws IOException {
// As an alternative to loading the configuration file from file system.
final LoggerContext context = LoggerContext.getContext(false);
LoggerContext context = LoggerContext.getContext(false);
String log4j2Config =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><Configuration status=\"WARN\">"
+ " <Appenders><Console name=\"Console\" target=\"SYSTEM_OUT\"><PatternLayout"
Expand All @@ -90,15 +89,6 @@ public static void init() throws IOException {
context.updateLoggers();

Configurator.setRootLevel(Level.TRACE);
System.getProperties()
.put(
"flogger.backend_factory",
"com.google.common.flogger.backend.log4j2.Log4j2BackendFactory#getInstance");
System.getProperties()
.put(
"flogger.logging_context",
"com.google.common.flogger.grpc.GrpcContextDataProvider#getInstance");
googleLogger = GoogleLogger.forEnclosingClass();
}

@Before
Expand All @@ -112,8 +102,10 @@ public void setUpLoggerBackend() {
appender = new CapturingAppender();
logger.addAppender(appender);
logger.setLevel(TRACE);
backend = new Log4j2LoggerBackend(logger);
LoggerBackend backend = new Log4j2LoggerBackend(logger);
events = appender.events;

testLogger = TestLogger.create(backend);
}

@After
Expand Down Expand Up @@ -152,9 +144,7 @@ public void testTag() {
.newContext()
.withTags(Tags.builder().addTag("foo", "bar").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[foo=bar]");
assertLogCount(1);
Expand All @@ -171,9 +161,7 @@ public void testTags() {
.withTags(
Tags.builder().addTag("foo").addTag("bar", "baz").addTag("bar", "baz2").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[bar=baz, bar=baz2, foo]");
assertLogCount(1);
Expand All @@ -189,9 +177,7 @@ public void testSingleCustomTagAsMetadataKey() {
.newContext()
.withMetadata(MetadataKey.single("tags", Tags.class), Tags.of("foo", "bar"))
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[foo=bar]");
assertLogCount(1);
Expand All @@ -207,9 +193,7 @@ public void testSingleTagAsMetadataKey() {
.newContext()
.withMetadata(LogContext.Key.TAGS, Tags.of("foo", "bar"))
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[foo=bar]");
assertLogCount(1);
Expand All @@ -225,9 +209,7 @@ public void testEmptyCustomTags_doNotDisplay() {
.newContext()
.withMetadata(MetadataKey.single("tags", Tags.class), Tags.builder().build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
assertLogCount(1);
assertLogEntry(0, INFO, "test", contextMap);
Expand All @@ -242,9 +224,7 @@ public void testEmptyTags_doNotDisplay() {
.newContext()
.withMetadata(LogContext.Key.TAGS, Tags.builder().build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
assertLogCount(1);
assertLogEntry(0, INFO, "test", contextMap);
Expand All @@ -261,9 +241,7 @@ public void testClashOfTagsWithSingleNonRepeatableMetadata() {
.withTags(
Tags.builder().addTag("foo").addTag("bar", "baz").addTag("bar", "baz2").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[aTag, bar=baz, bar=baz2, foo]");
assertLogCount(1);
Expand All @@ -282,9 +260,7 @@ public void testClashOfTagsWithMultipleRepeatableMetadata() {
.withTags(
Tags.builder().addTag("foo").addTag("bar", "baz").addTag("bar", "baz2").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[aTag, anotherTag, bar=baz, bar=baz2, foo]");
assertLogCount(1);
Expand All @@ -303,9 +279,7 @@ public void testClashOfTagsWithMultipleNonRepeatableMetadata() {
.withTags(
Tags.builder().addTag("foo").addTag("bar", "baz").addTag("bar", "baz2").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[anotherTag, bar=baz, bar=baz2, foo]");
assertLogCount(1);
Expand All @@ -322,9 +296,7 @@ public void testClashOfTagsWithMetadataHoldingAList() {
.withMetadata(MetadataKey.single("tags", List.class), Arrays.asList(1, 2, 3))
.withTags(Tags.builder().addTag("foo").addTag("bar", "baz").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[[1, 2, 3], bar=baz, foo]");
assertLogCount(1);
Expand All @@ -343,9 +315,7 @@ public void testClashOfTagsWithMetadataHoldingAListAndRepeatableMetadata() {
.withMetadata(REP_TAGS, "b")
.withTags(Tags.builder().addTag("foo").addTag("bar", "baz").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[[1, 2, 3], a, b, bar=baz, foo]");
assertLogCount(1);
Expand All @@ -364,9 +334,7 @@ public void testTagsWithRepeatableMetadataClash() {
.withTags(
Tags.builder().addTag("foo").addTag("bar", "baz").addTag("bar", "baz2").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("tags", "[aValue, anotherValue, bar=baz, bar=baz2, foo]");
assertLogCount(1);
Expand All @@ -383,9 +351,7 @@ public void testMultipleRepeatableMetadata() {
.withMetadata(REP_KEY, 1)
.withMetadata(REP_KEY, 2)
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("rep", "[1, 2]");
assertLogCount(1);
Expand All @@ -402,9 +368,7 @@ public void testMultipleNonRepeatableMetadataSameMetadataKey() {
.withMetadata(ID_KEY, "001")
.withMetadata(ID_KEY2, "002")
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("id", "[001, 002]");
assertLogCount(1);
Expand All @@ -421,9 +385,7 @@ public void testMultipleNonRepeatableMetadataDifferentMetadataKey() {
.withMetadata(COUNT_KEY, 1)
.withMetadata(COUNT_KEY, 2)
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("count", 2);
assertLogCount(1);
Expand All @@ -439,9 +401,7 @@ public void testSingleNonRepeatableMetadata() {
.newContext()
.withMetadata(COUNT_KEY, 23)
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("count", 23);
assertLogCount(1);
Expand All @@ -457,9 +417,7 @@ public void testSingleNonRepeatableMetadataList() {
.newContext()
.withMetadata(MetadataKey.single("items", List.class), Arrays.asList(23))
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("items", Collections.singletonList(23));
assertLogCount(1);
Expand All @@ -477,9 +435,7 @@ public void testScopedLoggingContext() {
.withTags(
Tags.builder().addTag("foo").addTag("baz", "bar").addTag("baz", "bar2").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("count", 23);
contextMap.put("tags", "[baz=bar, baz=bar2, foo]");
Expand All @@ -504,9 +460,7 @@ public void testNestedScopedLoggingContext() {
.withMetadata(ID_KEY, "002")
.withTags(Tags.builder().addTag("foo").addTag("baz", "bar2").build())
.install()) {
GoogleLogContext<?, ?> logContext = (GoogleLogContext<?, ?>) googleLogger.atInfo();
logContext.log("test");
backend.log(logContext); // this event will be caught
testLogger.atInfo().log("test");
Map<String, Object> contextMap = new HashMap<>();
contextMap.put("id", "002");
contextMap.put("tags", "[baz=bar, baz=bar2, foo]");
Expand Down

0 comments on commit 4f0ea71

Please sign in to comment.