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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
72 changes: 71 additions & 1 deletion content/en/logs/log_collection/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,62 @@
{{< /code-block >}}
{{% /collapse-content %}}

2. Add the JSON layout dependencies to your `pom.xml`. For example:
2. Add the JSON layout template file (e.g. `MyLayout.json`) in the `src/main/resources` directory of your Java project. For example:

Check warning on line 155 in content/en/logs/log_collection/java.md

View workflow job for this annotation

GitHub Actions / vale

Datadog.abbreviations

Use 'for example' instead of abbreviations like 'e.g.'.
```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 @@ -244,6 +299,21 @@

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.

If you are _not_ correlating logs and traces, **remove** the MDC placeholders (`dd.trace_id` and `dd.span_id`) from the log patterns and/or configurations in the above 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"
}
```

### Raw format

{{< tabs >}}
Expand Down
Loading