Skip to content

Commit 713cae8

Browse files
Adds examples for Log4j 2's JSON format log template layout (#29985)
* Adds examples for Log4j 2's JSON format log template layout - Adds a full example for Log4j 2's JSON format log template layout file, MyLayout.json - Adds an instruction to remove dd.trace_id and dd_span.id from the template layout examples, if the log and trace correlation is not used. * Update java.md * Update content/en/logs/log_collection/java.md --------- Co-authored-by: Esther Kim <[email protected]>
1 parent 67d02fd commit 713cae8

File tree

1 file changed

+70
-6
lines changed
  • content/en/logs/log_collection

1 file changed

+70
-6
lines changed

content/en/logs/log_collection/java.md

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,62 @@ Log4j 2 includes a JSON layout.
152152
{{< /code-block >}}
153153
{{% /collapse-content %}}
154154

155-
2. Add the JSON layout dependencies to your `pom.xml`. For example:
155+
2. Add the JSON layout template file (such as `MyLayout.json`) in the `src/main/resources` directory of your Java project. For example:
156+
```json
157+
{
158+
"timestamp":{
159+
"$resolver":"timestamp",
160+
"pattern":{
161+
"format":"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
162+
"timeZone":"UTC"
163+
}
164+
},
165+
"status":{
166+
"$resolver":"level",
167+
"field":"name"
168+
},
169+
"thread_name":{
170+
"$resolver":"thread",
171+
"field":"name"
172+
},
173+
"logger_name":{
174+
"$resolver":"logger",
175+
"field":"name"
176+
},
177+
"message":{
178+
"$resolver":"message",
179+
"stringified":true
180+
},
181+
"exception_class":{
182+
"$resolver":"exception",
183+
"field":"className"
184+
},
185+
"exception_message":{
186+
"$resolver":"exception",
187+
"field":"message"
188+
},
189+
"stack_trace":{
190+
"$resolver":"exception",
191+
"field":"stackTrace",
192+
"stackTrace":{
193+
"stringified":true
194+
}
195+
},
196+
"host":"${hostName}",
197+
"service":"${env:DD_SERVICE}",
198+
"version":"${env:DD_VERSION}",
199+
"dd.trace_id":{
200+
"$resolver":"mdc",
201+
"key":"dd.trace_id"
202+
},
203+
"dd.span_id":{
204+
"$resolver":"mdc",
205+
"key":"dd.span_id"
206+
}
207+
}
208+
```
209+
210+
3. Add the JSON layout dependencies to your `pom.xml`. For example:
156211
```xml
157212
<dependency>
158213
<groupId>org.apache.logging.log4j</groupId>
@@ -240,10 +295,6 @@ writer.field.dd.env = {context: dd.env}
240295
{{% /tab %}}
241296
{{< /tabs >}}
242297

243-
#### Inject trace IDs into your logs
244-
245-
If APM is enabled for this application, you can correlate logs and traces by enabling trace ID injection. See [Connecting Java Logs and Traces][3] for more information.
246-
247298
### Raw format
248299

249300
{{< tabs >}}
@@ -341,7 +392,20 @@ writer.file = log.txt
341392

342393
If APM is enabled for this application, you can correlate logs and traces by enabling trace ID injection. See [Connecting Java Logs and Traces][3].
343394

344-
If you are _not_ correlating logs and traces, you can remove the MDC placeholders (`%X{dd.trace_id} %X{dd.span_id}`) from the log patterns included in the above configuration examples.
395+
If you are _not_ correlating logs and traces, remove the MDC placeholders (`%X{dd.trace_id} %X{dd.span_id}`) from the log patterns included in the previous configuration examples.
396+
397+
For example, if you are using Log4j 2 but not correlating logs and traces, remove the following block from the example log layout template, `MyLayout.json`:
398+
399+
```json
400+
"dd.trace_id":{
401+
"$resolver":"mdc",
402+
"key":"dd.trace_id"
403+
},
404+
"dd.span_id":{
405+
"$resolver":"mdc",
406+
"key":"dd.span_id"
407+
}
408+
```
345409

346410

347411
## Configure the Datadog Agent

0 commit comments

Comments
 (0)