-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change OTel quickstart to use log4j2 instead of logback
Log4j2's `JsonTemplateLayout` supports most of the Cloud Logging special JSON keys directly through the [`GcpLayout` event template](https://logging.apache.org/log4j/2.x/manual/json-template-layout.html#event-templates). I still needed to map the OTel MDC keys for trace correlation, but am planning to contribute [this upstream](https://github.com/apache/logging-log4j2/blob/rel/2.17.2/log4j-layout-template-json/src/main/resources/GcpLayout.json). One outstanding issue is converting the `trace_flags` hex value to a boolean, but it doesn't break Cloud Console if `logging.googleapis.com/trace_sampled` key is unset/false. See apache/logging-log4j2#2482.
- Loading branch information
Showing
3 changed files
with
76 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
examples/instrumentation-quickstart/src/main/resources/log4j2.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<!-- | ||
Copyright 2024 Google LLC | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<Configuration status="WARN"> | ||
<Appenders> | ||
<Console name="Console" target="SYSTEM_OUT"> | ||
<!-- [START opentelemetry_instrumentation_setup_logging] --> | ||
<!-- Format JSON logs for the Cloud Logging agent | ||
https://cloud.google.com/logging/docs/structured-logging#special-payload-fields --> | ||
|
||
<!-- Log4j2's JsonTemplateLayout includes a template for Cloud Logging's special JSON fields | ||
https://logging.apache.org/log4j/2.x/manual/json-template-layout.html#event-templates --> | ||
<JsonTemplateLayout eventTemplateUri="classpath:GcpLayout.json"> | ||
<!-- Extend the included GcpLayout to include the trace and span IDs from Mapped | ||
Diagnostic Context (MDC) so that Cloud Logging can correlate Logs and Spans --> | ||
<EventTemplateAdditionalField | ||
key="logging.googleapis.com/trace" | ||
format="JSON" | ||
value='{"$resolver": "mdc", "key": "trace_id"}' | ||
/> | ||
<EventTemplateAdditionalField | ||
key="logging.googleapis.com/spanId" | ||
format="JSON" | ||
value='{"$resolver": "mdc", "key": "span_id"}' | ||
/> | ||
</JsonTemplateLayout> | ||
<!-- [END opentelemetry_instrumentation_setup_logging] --> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Root level="info"> | ||
<AppenderRef ref="Console" /> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |