Associate application logs

Updated at:

In distributed tracing, you can associate logs with your application. This lets you view the logs for a specific span when a trace is displayed.

How log association works

  • Modify the log output pattern in your application's log output configuration to include the TraceId and SpanId.

  • In your cloud application, set the application name, log name, and log path to collect application logs.

  • When you click an application node in a trace, the service searches the logs by application name, TraceId, and SpanId, and then displays the matching logs.

Procedure

Configure local application log printing

To associate traces with application logs, you must configure your local application logs to correctly output the TraceId and SpanId. The distributed tracing Tracer integrates with the SLF4J Mapped Diagnostic Context (MDC) feature. You can do this by modifying the log output pattern in your log configuration file to include the TraceId and SpanId from the Tracer context.

  1. Ensure that your SOFABoot application imports the Tracer dependency.

    <dependency>
        <groupId>com.alipay.sofa</groupId>
        <artifactId>tracer-enterprise-sofa-boot-starter</artifactId>
    </dependency>
  2. Add the %X{SOFA-TraceId} and %X{SOFA-SpanId} configuration to the `PatternLayout`.

    <PatternLayout>
        <pattern>%d %-5p %-32t [%X{SOFA-TraceId},%X{SOFA-SpanId}] - %m%n</pattern>
    </PatternLayout>

After you complete this configuration, the application log output resembles the following example:

2019-12-1023:59:50.214,[0a19018e157599359021261502137,0.1.1], spring mvc is invoked.

Add application logs in the console

  1. Log on to the distributed tracing console. In the navigation pane on the left, click Settings.

  2. If this is your first time using the log association feature, navigate to the RAM console to create and obtain an AccessKey and AccessKeySecret with the AliyunLogFullAccess access policy. For more information, see Create an AccessKey.

  3. After you obtain the AccessKey and AccessKeySecret, return to Distributed Tracing > Settings and enter the AccessKey and AccessKeySecret.

  4. On the Log Configuration page, click Add Application. In the window that appears, enter the correct application name and click OK.

    Note

    The application name must match the appName in the log output. Otherwise, application data cannot be retrieved.

  5. After the application is added, click Add Log.

  6. In the Add Log window, set the log name, full path, and other information, and then click OK.

View associated application logs

Navigate to the trace search page, find a trace for the application, and view its details. On the trace details page, click the log icon next to the application name to view the associated logs, as shown in the following figure.应用日志关联