Associate trace IDs with your Java application's business logs so that you can quickly find relevant logs by trace ID when an issue occurs and resolve the problem faster.
Prerequisites
This feature is not supported in the Basic edition of Application Real-Time Monitoring Service (ARMS).
The agent is version 2.6.1.2 or later. For more information, see Update the Java agent.
Log collection is configured by using Simple Log Service (SLS). For more information, see Data collection overview.
Background information
ARMS associates trace IDs with business logs by using the Mapped Diagnostic Context (MDC) mechanism and supports Log4j, Log4j2, and Logback.
Enable the association
-
Log on to the ARMS console. In the left-side navigation pane, choose .
Select a region in the top navigation bar and click the application.
NoteIcons in the Language column indicate the application's programming language:
: Java
: Go
: Python- (Hyphen): an application monitored in Managed Service for OpenTelemetry
In the top navigation bar, choose .
On the Custom Configurations tab, in the Application Log Association Configuration section, set Log Source to SLS. Turn on the Associate Business Logs with trace ID switch, select the region of your SLS project, and then bind the project and Logstore.
You can also turn on the Logs are automatically populated with trace ID and Logs are automatically populated with span ID switches as needed.
The Logs are automatically populated with trace ID and Logs are automatically populated with span ID features require agent version 4.1.6 or later. When you bind SLS, you must also select an Associated Index. You can specify either a field index or a full-text index.
NoteAfter you turn on this switch, trace IDs are automatically added to your business logs and you can skip the manual configuration in Step 5.
If you also want to pinpoint business exceptions, see Troubleshoot business exceptions by using traces and log analysis.
(Optional) Manually associate the trace ID.
In the
Patternattribute of your business log layout, add%X{EagleEye-TraceID}. If you use the SkyWalking protocol, add%X{Skywalking-TraceID}instead. If you onboard your application by using ACK OnePilot (the ARMS in-house agent), the trace ID format is%X{EagleEye-TraceID}.To also associate a span ID, add
%X{span_id}.ImportantSpan ID association requires agent version 4.x or later.
To learn how to obtain
{EagleEye-TraceID}in your business code, see ARMS SDK for Java (Not Recommended).
The following examples show how to modify the configuration files for Log4j, Log4j2, and Logback:
Example modification to the Log4j configuration file log4j.properties:
log4j.appender.warn.layout=org.apache.log4j.PatternLayout log4j.appender.warn.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss}-[%p]-(%C:%L) - traceId:%X{EagleEye-TraceID} - spanId:%X{span_id} - %m%nExample modification to the Log4j2 configuration file log4j2.xml:
PatternLayout
<console name="Console" target="SYSTEM_OUT"> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - traceId:%X{EagleEye-TraceID} - spanId:%X{span_id} - %m%n"/> </console>JsonLayout
<JsonLayout complete="false" compact="true" eventEol="true" properties="false" locationInfo="true" includeStacktrace="true" stacktraceAsString="true" objectMessageAsJsonObject="true" > <KeyValuePair key="EagleEye-TraceID" value="${ctx:EagleEye-TraceID}" /> <KeyValuePair key="span_id" value="${ctx:span_id}" /> </JsonLayout>Example modification to the Logback configuration file logback.xml:
PatternLayout
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - traceId:%X{EagleEye-TraceID} - spanId:%X{span_id} - %msg%n</pattern> </encoder>JsonLayout
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"> <providers> <timestamp/> <threadName/> <logLevel/> <loggerName/> <message/> <callerData/> <stackTrace/> <pattern> <pattern> { "EagleEye-TraceID": "%X{EagleEye-TraceID}", "span_id": "%X{span_id}" } </pattern> </pattern> </providers> </encoder>
Restart your application.
If the trace ID appears in your business logs, the association is successful. Example output:
2023-02-15 15:03:43-[INFO]-(com.example....HelloController:17) - traceId:ea1e0fe79b16764446 this is an info message 2023-02-15 15:03:43-[ERROR]-(com.example....HelloController:19) - traceId:ea1e0fe79b16764446 - this is an error message 2023-02-15 15:03:43-[WARN]-(com.example....HelloController:21) - traceId:ea1e0fe79b16764446 this is a warn message(Optional) Configure SLS log collection to upload application logs to the project and Logstore that you bound in the previous steps.
By default, ARMS does not collect application logs.