From 11c69d2a2ddb15860ac6998e2b0fe6d9bc7bf797 Mon Sep 17 00:00:00 2001 From: Felix Barnsteiner Date: Wed, 11 Nov 2020 13:54:22 +0100 Subject: [PATCH] Serialize thread name for JUL if record is from current thread --- .../co/elastic/logging/AbstractEcsLoggingTest.java | 2 +- .../java/co/elastic/logging/jul/EcsFormatter.java | 6 +++++- .../java/co/elastic/logging/jul/JulLoggingTest.java | 13 +------------ 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java b/ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java index ba2cdeb5..63b95941 100644 --- a/ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java +++ b/ecs-logging-core/src/test/java/co/elastic/logging/AbstractEcsLoggingTest.java @@ -48,7 +48,7 @@ void testMetadata() throws Exception { assertThat(getLastLogLine().get("process.thread.name").textValue()).isEqualTo(Thread.currentThread().getName()); assertThat(getLastLogLine().get("service.name").textValue()).isEqualTo("test"); assertThat(Instant.parse(getLastLogLine().get("@timestamp").textValue())).isCloseTo(Instant.now(), within(1, ChronoUnit.MINUTES)); - assertThat(getLastLogLine().get("log.level").textValue()).isEqualTo("DEBUG"); + assertThat(getLastLogLine().get("log.level").textValue()).isIn("DEBUG", "FINE"); assertThat(getLastLogLine().get("log.logger")).isNotNull(); assertThat(getLastLogLine().get("event.dataset").textValue()).isEqualTo("testdataset.log"); } diff --git a/jul-ecs-formatter/src/main/java/co/elastic/logging/jul/EcsFormatter.java b/jul-ecs-formatter/src/main/java/co/elastic/logging/jul/EcsFormatter.java index 940c5a54..b2781efc 100644 --- a/jul-ecs-formatter/src/main/java/co/elastic/logging/jul/EcsFormatter.java +++ b/jul-ecs-formatter/src/main/java/co/elastic/logging/jul/EcsFormatter.java @@ -61,7 +61,11 @@ public String format(final LogRecord record) { EcsJsonSerializer.serializeMDC(builder, mdcSupplier.getMDC()); EcsJsonSerializer.serializeServiceName(builder, serviceName); EcsJsonSerializer.serializeEventDataset(builder, eventDataset); - EcsJsonSerializer.serializeThreadId(builder, record.getThreadID()); + if (Thread.currentThread().getId() == record.getThreadID()) { + EcsJsonSerializer.serializeThreadName(builder, Thread.currentThread().getName()); + } else { + EcsJsonSerializer.serializeThreadId(builder, record.getThreadID()); + } EcsJsonSerializer.serializeLoggerName(builder, record.getLoggerName()); if (includeOrigin && record.getSourceClassName() != null && record.getSourceMethodName() != null) { EcsJsonSerializer.serializeOrigin(builder, buildFileName(record.getSourceClassName()), record.getSourceMethodName(), -1); diff --git a/jul-ecs-formatter/src/test/java/co/elastic/logging/jul/JulLoggingTest.java b/jul-ecs-formatter/src/test/java/co/elastic/logging/jul/JulLoggingTest.java index 0f7e1de2..0c6592e6 100644 --- a/jul-ecs-formatter/src/test/java/co/elastic/logging/jul/JulLoggingTest.java +++ b/jul-ecs-formatter/src/test/java/co/elastic/logging/jul/JulLoggingTest.java @@ -140,18 +140,7 @@ void testLogException() throws Exception { .collect(Collectors.joining("\n", "", "\n")); assertThat(stackTrace).contains("at co.elastic.logging.jul.JulLoggingTest.testLogException"); } - - @Test - void testMetadata() throws Exception { - debug("test"); - assertThat(getLastLogLine().get("process.thread.id").longValue()).isEqualTo(Thread.currentThread().getId()); - assertThat(getLastLogLine().get("service.name").textValue()).isEqualTo("test"); - assertThat(Instant.parse(getLastLogLine().get("@timestamp").textValue())).isCloseTo(Instant.now(), within(1, ChronoUnit.MINUTES)); - assertThat(getLastLogLine().get("log.level").textValue()).isEqualTo("FINE"); - assertThat(getLastLogLine().get("log.logger")).isNotNull(); - assertThat(getLastLogLine().get("event.dataset").textValue()).isEqualTo("testdataset.log"); - } - + @Test void testLogOrigin() throws Exception { debug("test");