Skip to content

Commit 279171b

Browse files
authored
Include pipeline and plugin IDs to the JSON logs. (#18470)
* Include pipeline and plugin IDs to the JSON logs. * Add unit tests for plugin and pipeline IDs.
1 parent 4ef52c2 commit 279171b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

logstash-core/src/main/java/org/logstash/log/CustomLogEventSerializer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.logging.log4j.Logger;
3232
import java.io.IOException;
3333
import java.util.Map;
34+
import java.util.Objects;
3435

3536
import static org.logstash.ObjectMappers.LOG4J_JSON_MAPPER;
3637

@@ -48,6 +49,17 @@ public void serialize(CustomLogEvent event, JsonGenerator generator, SerializerP
4849
generator.writeObjectField("loggerName", event.getLoggerName());
4950
generator.writeObjectField("timeMillis", event.getTimeMillis());
5051
generator.writeObjectField("thread", event.getThreadName());
52+
53+
final String pipelineId = event.getContextData().getValue("pipeline.id");
54+
if (Objects.nonNull(pipelineId) && !pipelineId.isEmpty()) {
55+
generator.writeStringField("pipeline.id", pipelineId);
56+
}
57+
58+
final String pluginId = event.getContextData().getValue("plugin.id");
59+
if (Objects.nonNull(pluginId) && !pluginId.isEmpty()) {
60+
generator.writeStringField("plugin.id", pluginId);
61+
}
62+
5163
generator.writeFieldName("logEvent");
5264
generator.writeStartObject();
5365

logstash-core/src/test/java/org/logstash/log/CustomLogEventTests.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@
4747
import com.fasterxml.jackson.core.JsonProcessingException;
4848
import org.apache.logging.log4j.LogManager;
4949
import org.apache.logging.log4j.Logger;
50+
import org.apache.logging.log4j.ThreadContext;
5051
import org.apache.logging.log4j.junit.LoggerContextRule;
5152
import org.apache.logging.log4j.test.appender.ListAppender;
5253
import org.jruby.RubyHash;
5354
import org.jruby.runtime.builtin.IRubyObject;
55+
import org.junit.After;
5456
import org.junit.ClassRule;
5557
import org.junit.Test;
5658
import org.logstash.ObjectMappers;
@@ -59,7 +61,6 @@
5961
import static junit.framework.TestCase.assertFalse;
6062
import static junit.framework.TestCase.assertEquals;
6163
import static junit.framework.TestCase.assertNotNull;
62-
import static org.junit.Assert.assertTrue;
6364

6465
public class CustomLogEventTests {
6566
private static final String CONFIG = "log4j2-test1.xml";
@@ -68,6 +69,11 @@ public class CustomLogEventTests {
6869
@ClassRule
6970
public static LoggerContextRule CTX = new LoggerContextRule(CONFIG);
7071

72+
@After
73+
public void tearDown() {
74+
ThreadContext.clearAll();
75+
}
76+
7177
@Test
7278
public void testPatternLayout() {
7379
ListAppender appender = CTX.getListAppender("EventLogger").clear();
@@ -206,4 +212,29 @@ public void testJSONLayoutWhenParamsContainsAnotherMessageField() throws JsonPro
206212
System.setProperty(STRICT_JSON_PROPERTY_NAME, prevSetting);
207213
}
208214
}
215+
216+
@Test
217+
@SuppressWarnings("unchecked")
218+
public void testJSONLayoutWithPipelineIdAndPluginIds() throws JsonProcessingException {
219+
ListAppender appender = CTX.getListAppender("JSONEventLogger").clear();
220+
Logger logger = LogManager.getLogger("JSONEventLogger");
221+
222+
ThreadContext.put("pipeline.id", "main-pipeline");
223+
ThreadContext.put("plugin.id", "elasticsearch-output-xyz");
224+
logger.debug("Both context fields test");
225+
ThreadContext.remove("pipeline.id");
226+
ThreadContext.remove("plugin.id");
227+
228+
List<String> messages = appender.getMessages();
229+
assertEquals(1, messages.size());
230+
231+
Map<String, Object> result = ObjectMappers.JSON_MAPPER.readValue(messages.get(0), Map.class);
232+
233+
assertEquals("DEBUG", result.get("level"));
234+
assertEquals("main-pipeline", result.get("pipeline.id"));
235+
assertEquals("elasticsearch-output-xyz", result.get("plugin.id"));
236+
237+
Map<String, Object> logEvent = (Map<String, Object>) result.get("logEvent");
238+
assertEquals("Both context fields test", logEvent.get("message"));
239+
}
209240
}

0 commit comments

Comments
 (0)