You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you want to get logs from your Java application ingested into an OpenTelemetry-compatible logs backend, the easiest and recommended way is using an OpenTelemetry protocol (OTLP) exporter.
13
-
However, some scenarios require logs to be output to files or stdout due to organizational or reliability needs.
14
13
15
-
A common approach to centralize logs is to use unstructured logs, parse them with regular expressions, and add contextual attributes.
14
+
If you want to get logs from your Java application ingested into an
15
+
OpenTelemetry-compatible logs backend, the easiest and recommended way is using
16
+
an OpenTelemetry protocol (OTLP) exporter. However, some scenarios require logs
17
+
to be output to files or stdout due to organizational or reliability needs.
16
18
17
-
However, regular expression parsing is problematic. They become complex and fragile quickly when handling all log fields, line breaks in exceptions, and unexpected log format changes. Parsing errors are inevitable with this method.
19
+
A common approach to centralize logs is to use unstructured logs, parse them
20
+
with regular expressions, and add contextual attributes.
21
+
22
+
However, regular expression parsing is problematic. They become complex and
23
+
fragile quickly when handling all log fields, line breaks in exceptions, and
24
+
unexpected log format changes. Parsing errors are inevitable with this method.
18
25
19
26
## Solution
20
27
21
-
The OpenTelemetry Java Instrumentation agent and SDK now offer an easy solution to convert logs from frameworks like SLF4J/Logback or Log4j2 into OTel-compliant JSON logs on stdout with all resource and log attributes.
28
+
The OpenTelemetry Java Instrumentation agent and SDK now offer an easy solution
29
+
to convert logs from frameworks like SLF4J/Logback or Log4j2 into OTel-compliant
30
+
JSON logs on stdout with all resource and log attributes.
22
31
23
32
This is a true turnkey solution:
24
33
25
-
* No code or dependency changes, just a few configuration adjustments typical for production deployment.
26
-
* No complex field mapping in the log collector. Just use the [OTLP/JSON connector](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/connector/otlpjsonconnector) to ingest the payload.
27
-
* Automatic correlation between logs, traces, and metrics.
34
+
- No code or dependency changes, just a few configuration adjustments typical
35
+
for production deployment.
36
+
- No complex field mapping in the log collector. Just use the
Export the logs captured by the OTel Java instrumentation to stdout using the OTel JSON format (aka [OTLP/JSON](https://opentelemetry.io/docs/specs/otlp/#json-protobuf-encoding)).
59
-
Configuration parameters for [Logback](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/logback/logback-appender-1.0/javaagent) and [Log4j](https://github.com/open-telemetry/opentelemetry-java-instrumentation/tree/main/instrumentation/log4j/log4j-appender-2.17/javaagent) are optional but recommended.
73
+
Export the logs captured by the OTel Java instrumentation to stdout using the
The `-Dotel.logs.exporter=experimental-otlp/stdout` JVM argument and the environment variable `OTEL_LOGS_EXPORTER="experimental-otlp/stdout"` can be used interchangeably.
96
+
The `-Dotel.logs.exporter=experimental-otlp/stdout` JVM argument and the
97
+
environment variable `OTEL_LOGS_EXPORTER="experimental-otlp/stdout"` can be used
98
+
interchangeably.
76
99
77
100
{{% alert title="Note" color="info" %}}
78
101
79
-
The OTLP logs exporter is experimental and subject to change.
80
-
Check the [Specification PR](https://github.com/open-telemetry/opentelemetry-specification/pull/4183) for the latest updates.
102
+
The OTLP logs exporter is experimental and subject to change. Check the
Verify that OTLP/JSON logs are outputted to stdout. The logs are in the OTLP/JSON format, with a JSON object per line. The log records are nested in the `resourceLogs` array.
108
+
Verify that OTLP/JSON logs are outputted to stdout. The logs are in the
109
+
OTLP/JSON format, with a JSON object per line. The log records are nested in the
@@ -170,17 +198,20 @@ Verify the logs in the OpenTelemetry backend.
170
198
171
199
After the pipeline works end-to-end, ensure production readiness:
172
200
173
-
* Remove the `debug` exporter from the `logs` pipeline in the OTel Collector configuration.
174
-
* Disable file and console exporters in the logging framework (for example, `logback.xml`) but keep using the logging configuration to filter logs. The OTel Java agent will output JSON logs to stdout.
201
+
- Remove the `debug` exporter from the `logs` pipeline in the OTel Collector
202
+
configuration.
203
+
- Disable file and console exporters in the logging framework (for example,
204
+
`logback.xml`) but keep using the logging configuration to filter logs. The
205
+
OTel Java agent will output JSON logs to stdout.
175
206
176
207
```xml
177
208
<!-- tested with logback-classic v1.5.11 -->
178
209
<configuration>
179
210
<loggername="com.example"level="debug"/>
180
211
<rootlevel="info">
181
212
<!-- No appender as the OTel Agent emits otlpjson logs through stdout -->
182
-
<!--
183
-
IMPORTANT enable a console appender to troubleshoot cases were
213
+
<!--
214
+
IMPORTANT enable a console appender to troubleshoot cases where
184
215
logs are missing in the OTel backend
185
216
-->
186
217
</root>
@@ -189,21 +220,26 @@ After the pipeline works end-to-end, ensure production readiness:
189
220
190
221
## Configure an OpenTelemetry Collector in Kubernetes to handle container logs
191
222
192
-
To support Kubernetes and container specifics, add a standard parsing step in the pipeline without specific mapping configuration.
223
+
To support Kubernetes and container specifics, add a standard parsing step in
224
+
the pipeline without specific mapping configuration.
193
225
194
-
Use the OTel Collector File Log Receiver's [`container`](https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/pkg/stanza/docs/operators/container.md) parser to handle container logging specifics.
Replace `<<namespace>>`, `<<pod_name>>`, and `<<container_name>>` with the desired values or use a broader [glob pattern](https://pkg.go.dev/v.io/v23/glob) like `*`.
230
+
Replace `<<namespace>>`, `<<pod_name>>`, and `<<container_name>>` with the
231
+
desired values or use a broader [glob pattern](https://pkg.go.dev/v.io/v23/glob)
232
+
like `*`.
197
233
198
234
```yaml
199
235
receivers:
200
236
filelog/otlp-json-logs:
201
237
# start_at: beginning # for testing purpose, use "start_at: beginning"
# (i) no need for processors before the otlpjson connector
227
263
# Declare processors in the shared "logs" pipeline below
228
264
processors: []
@@ -244,7 +280,13 @@ exporters:
244
280
245
281
## Conclusion
246
282
247
-
This blog post showed how to collect file-based Java logs with OpenTelemetry. The solution is easy to set up and provides a turnkey solution for converting logs from frameworks like SLF4J/Logback or Log4j2 into OTel-compliant JSON logs on stdout with all resource and log attributes.
248
-
This JSON format is certainly verbose, but it generally has minimal impact on performances and offers a solid balance by providing highly contextualized logs that can be correlated with traces and metrics.
283
+
This blog post showed how to collect file-based Java logs with OpenTelemetry.
284
+
The solution is easy to set up and provides a turnkey solution for converting
285
+
logs from frameworks like SLF4J/Logback or Log4j2 into OTel-compliant JSON logs
286
+
on stdout with all resource and log attributes. This JSON format is certainly
287
+
verbose, but it generally has minimal impact on performances and offers a solid
288
+
balance by providing highly contextualized logs that can be correlated with
289
+
traces and metrics.
249
290
250
-
Any feedback or questions? Reach out on [GitHub](https://github.com/open-telemetry/opentelemetry-specification/pull/4183).
0 commit comments