-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for logging markers as tags (#14)
Logback (SLF4J) and Log4j 2 support Markers, which can contain useful information. This change adds support for optionally logging these markers as tags in the created/encoded log messages. By default, this feature is disabled and can be enabled by setting the `includeMarkers` configuration setting to `true` in the Log4j 2 `EcsLayout` and Logback `EcsEncoder`. See also: * https://logging.apache.org/log4j/2.0/manual/markers.html * https://www.slf4j.org/api/org/slf4j/Marker.html
- Loading branch information
1 parent
283f2b7
commit d95ad5d
Showing
14 changed files
with
461 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
log4j2-ecs-layout/src/test/java/co/elastic/logging/log4j2/AbstractLog4j2EcsLayoutTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/*- | ||
* #%L | ||
* Java ECS logging | ||
* %% | ||
* Copyright (C) 2019 Elastic and contributors | ||
* %% | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* #L% | ||
*/ | ||
package co.elastic.logging.log4j2; | ||
|
||
import co.elastic.logging.AbstractEcsLoggingTest; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import org.apache.logging.log4j.Marker; | ||
import org.apache.logging.log4j.MarkerManager; | ||
import org.apache.logging.log4j.ThreadContext; | ||
import org.apache.logging.log4j.core.Logger; | ||
import org.apache.logging.log4j.message.StringMapMessage; | ||
import org.apache.logging.log4j.test.appender.ListAppender; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
abstract class AbstractLog4j2EcsLayoutTest extends AbstractEcsLoggingTest { | ||
protected Logger root; | ||
protected ListAppender listAppender; | ||
|
||
@AfterEach | ||
void tearDown() throws Exception { | ||
ThreadContext.clearAll(); | ||
} | ||
|
||
@Test | ||
void globalLabels() throws Exception { | ||
putMdc("trace.id", "foo"); | ||
debug("test"); | ||
assertThat(getLastLogLine().get("cluster.uuid").textValue()).isEqualTo("9fe9134b-20b0-465e-acf9-8cc09ac9053b"); | ||
assertThat(getLastLogLine().get("node.id").textValue()).isEqualTo("foo"); | ||
assertThat(getLastLogLine().get("404")).isNull(); | ||
} | ||
|
||
@Test | ||
void testMarker() throws Exception { | ||
Marker parent = MarkerManager.getMarker("parent"); | ||
Marker child = MarkerManager.getMarker("child").setParents(parent); | ||
Marker grandchild = MarkerManager.getMarker("grandchild").setParents(child); | ||
root.debug(grandchild, "test"); | ||
|
||
assertThat(getLastLogLine().get("tags")).contains( | ||
TextNode.valueOf("parent"), | ||
TextNode.valueOf("child"), | ||
TextNode.valueOf("grandchild")); | ||
} | ||
|
||
@Test | ||
void testMapMessage() throws Exception { | ||
root.info(new StringMapMessage(Map.of("foo", "bar"))); | ||
assertThat(getLastLogLine().get("labels.foo").textValue()).isEqualTo("bar"); | ||
} | ||
|
||
@Override | ||
public void putMdc(String key, String value) { | ||
ThreadContext.put(key, value); | ||
} | ||
|
||
@Override | ||
public boolean putNdc(String message) { | ||
ThreadContext.push(message); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void debug(String message) { | ||
root.debug(message); | ||
} | ||
|
||
@Override | ||
public void error(String message, Throwable t) { | ||
root.error(message, t); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
...j2-ecs-layout/src/test/java/co/elastic/logging/log4j2/Log4j2EcsLayoutIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/*- | ||
* #%L | ||
* Java ECS logging | ||
* %% | ||
* Copyright (C) 2019 Elastic and contributors | ||
* %% | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
* #L% | ||
*/ | ||
package co.elastic.logging.log4j2; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import org.apache.logging.log4j.core.LoggerContext; | ||
import org.apache.logging.log4j.test.appender.ListAppender; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
||
import java.io.IOException; | ||
|
||
class Log4j2EcsLayoutIntegrationTest extends AbstractLog4j2EcsLayoutTest { | ||
@BeforeEach | ||
void setUp() { | ||
root = LoggerContext.getContext().getRootLogger(); | ||
listAppender = (ListAppender) root.getAppenders().get("TestAppender"); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() throws Exception { | ||
super.tearDown(); | ||
listAppender.clear(); | ||
} | ||
|
||
@Override | ||
public JsonNode getLastLogLine() throws IOException { | ||
return objectMapper.readTree(listAppender.getMessages().get(0)); | ||
} | ||
} |
Oops, something went wrong.