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
-
An AccessKey pair is available to integrate the Log4j Appender into your application.
For security, use a RAM user instead of your Alibaba Cloud account and create an AccessKey pair for the RAM user.
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.
-
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.
-
-
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.
-
After the k8s-logstore is created, follow the on-screen prompts to ingest data.
-
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.
-
Download the demo-deployment.yaml file and configure the
JAVA_OPTSenvironment variable.The source file is available at demo-deployment.
JAVA_OPTSparameters:-
-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.
-
-
Create the Deployment.
kubectl create -f demo-deployment.yaml -
Download the demo-service.yaml file. You can keep the default configuration.
The source file is available at demo-Service.
-
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.
-
View the
EXTERNAL-IPof the Service.kubectl get svcExpected 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 -
Access the
/loginendpoint to generate a test log entry.Replace
K8s_SERVICE_IPwithEXTERNAL-IP.curl http://${K8S_SERVICE_IP}:8080/login?name=bruceFor 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.
Log on to the Simple Log Service console.
-
In the Projects section, click the target project to open its details page.
-
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
messagefield containsUser login successfully, requestID=hIAgSGKGyA name=bruce. This confirms that the application logs have been successfully collected to SLS.