Skip to content

Adds examples for Log4j 2's JSON format log template layout #29985

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 70 additions & 6 deletions content/en/logs/log_collection/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,62 @@ Log4j 2 includes a JSON layout.
{{< /code-block >}}
{{% /collapse-content %}}

2. Add the JSON layout dependencies to your `pom.xml`. For example:
2. Add the JSON layout template file (such as `MyLayout.json`) in the `src/main/resources` directory of your Java project. For example:
```json
{
"timestamp":{
"$resolver":"timestamp",
"pattern":{
"format":"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"timeZone":"UTC"
}
},
"status":{
"$resolver":"level",
"field":"name"
},
"thread_name":{
"$resolver":"thread",
"field":"name"
},
"logger_name":{
"$resolver":"logger",
"field":"name"
},
"message":{
"$resolver":"message",
"stringified":true
},
"exception_class":{
"$resolver":"exception",
"field":"className"
},
"exception_message":{
"$resolver":"exception",
"field":"message"
},
"stack_trace":{
"$resolver":"exception",
"field":"stackTrace",
"stackTrace":{
"stringified":true
}
},
"host":"${hostName}",
"service":"${env:DD_SERVICE}",
"version":"${env:DD_VERSION}",
"dd.trace_id":{
"$resolver":"mdc",
"key":"dd.trace_id"
},
"dd.span_id":{
"$resolver":"mdc",
"key":"dd.span_id"
}
}
```

3. Add the JSON layout dependencies to your `pom.xml`. For example:
```xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
Expand Down Expand Up @@ -240,10 +295,6 @@ writer.field.dd.env = {context: dd.env}
{{% /tab %}}
{{< /tabs >}}

#### Inject trace IDs into your logs

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.

### Raw format

{{< tabs >}}
Expand Down Expand Up @@ -341,7 +392,20 @@ writer.file = log.txt

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].

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.
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.

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`:

```json
"dd.trace_id":{
"$resolver":"mdc",
"key":"dd.trace_id"
},
"dd.span_id":{
"$resolver":"mdc",
"key":"dd.span_id"
}
```


## Configure the Datadog Agent
Expand Down
Loading