All Products
Search
Document Center

Application Real-Time Monitoring Service:Associate trace IDs with logs for a Java application

Last Updated:Dec 18, 2024

Application Real-Time Monitoring Service (ARMS) allows you to associate trace IDs with the logs of a Java application. This way, if an error occurs in the application, you can access the logs associated with the trace IDs to locate and troubleshoot the error.

Prerequisites

Note

Application Monitoring Basic Edition does not support this feature.

Background information

In ARMS, trace IDs can be associated with the 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

  1. Log on to the ARMS console. In the left-side navigation pane, choose Application Monitoring > Application List.

  2. 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.

    Note

    Icons displayed in the Language column indicate languages in which applications are written.

    Java图标: Java application

    image: Go application

    image: Python application

    Hyphen (-): application monitored in Managed Service for OpenTelemetry.

  3. In the top navigation bar, choose Configuration > Custom Configurations.

  4. In the Application log Association configuration section of the Custom Configurations tab, set Log Source to Log service SLS. Turn on Automatically associate business logs with TraceId, select the region where your Simple Log Service is deployed, and select a project and a Logstore.

    You can also turn on Logs are automatically populated with traceId and Logs are automatically populated with spanId as required.

    image

    Note
  5. (Optional) Add %X{EagleEye-TraceID} to the pattern properties in the layout of the logs.

    You can also associate a span ID by adding %X{span_id}.

    Important
    • Association 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>
  6. Restart the application.

    If trace IDs are displayed in the application logs, the logs are associated with the trace IDs, as shown in the following figure.dg_am_log_traceid

  7. (Optional) Configure log collection and upload application logs to the project and Logstore.

    By default, ARMS does not collect application logs.