All Products
Search
Document Center

Cloud Monitor:Associate trace IDs with logs for a Python application

Last Updated:Oct 10, 2025

Application Real-Time Monitoring Service (ARMS) allows you to associate trace IDs with the logs of a Python 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

Background information

The capability of associating trace IDs with Python logs in ARMS relies on the instrumentation of log frameworks. Currently, only Logging, the official Python framework, is supported.

Procedure

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

  2. Select a region in the top navigation bar and click the application.

    Note

    Icons in the Language column indicate the application's programming language:

    • Java图标: Java

    • image: Go

    • image: Python

    • - (Hyphen): an 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. Select the region where your Simple Log Service is deployed, and bind a project and a Logstore.

    image

  5. Configure the ARMS agent for Python.

    1. Set the environment variable OTEL_PYTHON_LOG_CORRELATION to true to enable the feature for associating business logs with trace IDs in the agent.

      • The default value of OTEL_PYTHON_LOG_CORRELATION is false.

      • If you set OTEL_PYTHON_LOG_CORRELATION to false or leave this variable empty, the feature is disabled.

    2. Set the environment variable OTEL_PYTHON_LOG_LEVEL to warning, debug, info, or error based on your business requirements. This variable specifies the level of the business logs to be associated with trace IDs.

    3. Set the environment variable OTEL_PYTHON_LOG_FORMAT. An example value is %(asctime)s %(levelname)s [%(name)s] [%(filename)s:%(lineno)d] [trace_id=%(otelTraceID)s span_id=%(otelSpanID)s resource.service.name=%(otelServiceName)s trace_sampled=%(otelTraceSampled)s] - %(message)s. You can customize the value based on your business requirements.

      The following defines some of the fields:

      • otelTraceID: The trace ID.

      • otelSpanID: The ID of the span generated by the current log.

      • otelTraceSampled: The sampling identifier.

      • otelServiceName: The application name.

    After you configure the preceding environment variables, the following log is returned:

    2025-01-03_11-02-42

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

    By default, the ARMS agent for Python does not collect application logs.