You can associate trace IDs with the business logs of an application. This way, if an error occurs in the application, you can access the business logs associated with trace IDs to find out and troubleshoot the error.
Prerequisites
Only Application Monitoring Pro Edition supports this feature.
The Application Real-Time Monitoring Service (ARMS) agent is upgraded to V2.6.1.2 or later. For more information, see Update the ARMS agent for Java applications.
Logs are collected by using Simple Log Service (SLS). For more information, see Data collection overview.
Background information
In ARMS, trace IDs can be associated with the business logs of an application based on the Mapped Diagnostic Context (MDC) mechanism. Mainstream log frameworks such as Log4j, Log4j 2, and Logback are supported.
Procedure
Log on to the ARMS console. In the left-side navigation pane, choose .
On the Application List page, select a region in the top navigation bar and click the name of the application that you want to manage.
NoteIf the icon is displayed in the Language column, the application is connected to Application Monitoring. If a hyphen (-) is displayed, the application is connected to Managed Service for OpenTelemetry.
In the top navigation bar, choose
.In the Application log Association configuration section of the Custom Configurations tab, set the Log Source parameter to Log service SLS, turn on Automatically associate business logs with TraceId, select the region where your SLS service is deployed, and then select a project and a Logstore.
You can also turn on Logs are automatically populated with traceId and Logs are automatically populated with spanId.
NoteIf you enable Link Business Logs with TraceId, trace IDs are automatically generated in the business logs. In this case, ignore Step 5.
For information about how to troubleshoot errors, see Identify business exceptions by analyzing traces and logs.
Optional. Add
%X{EagleEye-TraceID}
to the pattern properties in the layout of business logs.You can also associate a span ID by adding
%X{span_id}
.ImportantAssociation of span IDs is supported only by the ARMS agent V4.x and later.
For information about how to obtain
{EagleEye-TraceID}
in the code, see ARMS SDKs.
The following examples show how to modify the configuration files of Log4j, Log4j2, and Logback frameworks:
Modify 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%n
Modify 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>
Modify 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 the application.
If trace IDs are displayed in the business logs of the application, the business logs are associated with the trace IDs, as shown in the following figure.
Optional. Configure log collection and upload application logs to the project and Logstore.
By default, ARMS does not collect application logs.