All Products
Search
Document Center

Container Service for Kubernetes:Collect Log4j logs from Java applications to SLS

Last Updated:Jun 20, 2026

By configuring a Log4j Appender in your workload YAML, you can send logs from Java applications in your cluster directly to a Simple Log Service (SLS) project. This method enables agentless log collection.

Log4j is an open-source logging framework maintained by Apache and designed for Java applications. Its core components include Appenders (output targets), Layouts (output formats), and Filters.

Prerequisites

Step 1: Configure the Log4j Appender

This step shows you how to create an SLS project and a Logstore. After you configure the Log4j Appender as the data source for the Logstore, your Java application can send logs directly to SLS.

Create the SLS project in the same region as your cluster. When they are in the same region, log data is transmitted over the internal network, which avoids public bandwidth costs and reduces network latency.

For more information about SLS project billing, see Billing overview.
  1. Create an SLS project.

    This section describes only the key parameters. You can keep the default values for other parameters. For more information, see Manage a project.

    • Region: Select the same region as your ACK cluster. In this example, China (Hangzhou) is selected.

    • Project Name: Enter a project name. This example uses k8s-log4j.

  2. Create a Logstore in the project.

    In this example, the Logstore Name is k8s-logstore. You can keep the default values for other parameters. For more information, see Create a Logstore.

  3. After the k8s-logstore is created, follow the on-screen prompts to ingest data.

  4. Search for the Log4j ingestion card, and then follow the on-screen prompts to complete the ingestion.

    This example uses the default configuration. You can adjust the settings based on your specific use case. In the Log4j data source wizard, complete the following steps: Select a Logstore, Configure Data Source, and Configure Search & Analysis. In the instructions area, click the View Help link for detailed guidance, and then click Next to complete the setup.

Step 2: Integrate Log4j

This step deploys a sample Spring Boot application to show you how to integrate the Log4j Appender plug-in into a Java application. After integration, the container passes the SLS configuration through the JAVA_OPTS environment variable at startup. Log4j automatically reads these parameters and sends logs to SLS in real time.

  1. Download the demo-deployment.yaml file and configure the JAVA_OPTS environment variable.

    The source file is available at demo-deployment.

    View the sample file

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: log4j-appender-demo-spring-boot
      labels:
        app: log4j-appender
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: log4j-appender
      template:
        metadata:
          labels:
            app: log4j-appender
        spec:
          containers:
          - name: log4j-appender-demo-spring-boot
            image: registry.cn-hangzhou.aliyuncs.com/jaegertracing/log4j-appender-demo-spring-boot:0.0.2
            env:
              - name: JAVA_OPTS            #  Set the JAVA_OPTS environment variable.
                value: "-Dproject={your_project} -Dlogstore={your_logstore} -Dendpoint={your_endpoint} -Daccess_key_id={your_access_key_id} -Daccess_key={your_access_key_secret}"
            ports:
            - containerPort: 8080

    JAVA_OPTS parameters:

    • -Dproject: The name of your SLS project. In this example, k8s-log4j.

    • -Dlogstore: The name of your SLS Logstore. In this example, k8s-logstore.

    • -Dendpoint: The service endpoint of the region where your project is located. For more information, see Service Endpoints. This example uses cn-hangzhou.log.aliyuncs.com.

    • -Daccess_key_id: Replace with your AccessKey ID.

    • -Daccess_key: Replace with your AccessKey secret.

  2. Create the Deployment.

    kubectl create -f demo-deployment.yaml
  3. Download the demo-service.yaml file. You can keep the default configuration.

    The source file is available at demo-Service.

    View the sample file

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: log4j-appender
      name: log4j-appender-demo-spring-boot-svc
      namespace: default
    spec:
      ports:
      - port: 8080
        protocol: TCP
        targetPort: 8080
      selector:
        app: log4j-appender
      type: LoadBalancer
  4. Deploy the demo-service.yaml file to create the Service.

    kubectl create -f demo-service.yaml

Step 3: Test log collection

After the Deployment and Service are deployed, you can verify that logs are written to SLS by accessing the service through its external IP address (EXTERNAL-IP), which generates log entries.

  1. View the EXTERNAL-IP of the Service.

    kubectl get svc

    Expected output:

    NAME                                  TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)          AGE
    log4j-appender-demo-spring-boot-svc   LoadBalancer   172.21.XX.XX   120.55.XXX.XXX   8080:30398/TCP   1h
  2. Access the /login endpoint to generate a test log entry.

    Replace K8s_SERVICE_IP with EXTERNAL-IP.

    curl http://${K8S_SERVICE_IP}:8080/login?name=bruce
    For the complete set of APIs, see GitHub log4j-appender-demo.

Step 4: View logs

You can log in to the SLS console to query and analyze the collected logs.

  1. Log on to the Simple Log Service console.

  2. In the Projects section, click the target project to open its details page.

  3. Click the 图标 icon to the right of the Logstore name and select Query and Analysis to view the logs collected from your cluster.

    The query results show the log entry, where the message field contains User login successfully, requestID=hIAgSGKGyA name=bruce. This confirms that the application logs have been successfully collected to SLS.

Related documentation