Log Service (abbreviated to LOG) is a platform service for log scenarios. You can collect, distribute, ship, and query logs quickly without development, which is applicable to scenarios such as log transfer, monitoring, performance diagnosis, log analysis, and audit. Container Service integrates with Log Service, which allows you to send the application logs to Log Service.
Note: On the cluster management page, click Enable Log Service and click OK in the displayed dialog box. After Log Service is successfully enabled, the log index is created for each automatically created Logstore by using the built-in Resource Access Management (RAM) account. With this feature enabled, you will be charged for the Alibaba Cloud Log Service usage after configuring the following settings. For more information, see Log Service billing method. Make sure you know your log volume to avoid large unexpected costs.
Enable Log Service
Log on to the Container Service console.
Click Swarm > Clusters in the left-side navigation pane.
Click Manage at the right of the cluster.
Click Enable Log Service in the upper-right corner.
Click OK in the displayed dialog box.
Before enabling Log Service in Container Service, activate the RAM service and Log Service first. Click Activate It to activate the RAM service and Log Service if they are not activated yet.
The created Log Service project is displayed after Log Service is successfully enabled.
Check installation result of acslogging service
Container Service installs the Agent required by Log Service on your machine if this is the first time Log Service is enabled. You can find this application on the Application List page. You can use Log Service after the application is installed successfully.
Log on to the Container Service console.
Click Swarm > Applications in the left-side navigation pane.
Select the cluster from the Cluster list and clear the Hide System Applications check box.
The acslogging application is successfully installed.
The system creates a corresponding project in Alibaba Cloud Log Service. You can view the project in Log Service console. The project name contains the Container Service cluster ID.
Use Log Service in orchestration files
Most Docker applications write the logs directly to stdout, now you can do this as well (for the scenarios of writing logs to files, see Use file logs in the following section). After enabling Log Service, stdout logs are automatically collected and sent to Alibaba Cloud Log Service.
Use two kinds of compose orchestration to create a WordPress application, which contains two services: WordPress service and MySQL service. Logs are collected to Alibaba Cloud Log Service.
Compose V3 orchestration
version: '3'
services:
web:
image: wordpress:4
ports:
- 80
depends_on:
- mysql
deploy:
replicas: 2
restart_policy:
condition: on-failure
labels:
aliyun.routing.port_80: wordpress-with-log
aliyun.log_store_dbstdout: stdout # Collect stdout logs to the dbstdout Logstore.
aliyun.log_ttl_dbstdout: 30 # Set the data retention time for the dbstdout Logstore to 30 days.
mysql:
image: mysql:5.7
environment:
- MYSQL_ROOT_PASSWORD=password
deploy:
restart_policy:
condition: on-failure
Compose V1/V2 orchestration
wordpress:
image: registry.aliyuncs.com/jiangjizhong/wordpress
ports:
- 80
labels:
aliyun.routing.port_80: wordpress-with-log
aliyun.log_store_dbstdout: stdout # Collect stdout logs to the dbstdout Logstore.
aliyun.log_ttl_dbstdout: 30 # Set the data retention time for the dbstdout Logstore to 30 days.
links:
- mysql
mysql:
image: mysql
ports:
- 80
labels:
aliyun.scale: "1"
environment:
- MYSQL_ROOT_PASSWORD=password
In the preceding orchestration file:
aliyun.log_store_dbstdout: stdout
indicates to write the container standard to the Logstoreacslog-wordpress-dbstdout
. The label format isaliyun.log_store_{name}: {logpath}
.Wherein:
name
is the name of the Alibaba Cloud Log Service Logstore. The actually created Logstore name isacslog-${app}-${name}
.app
is the application name.logpath
is the log path in the container.stdout
is a speciallogpath
, indicating the standard output.
aliyun.log_ttl_<logstore_name>
is used to set the data retention time (in days) for the Logstore. The value range is 1–365. If left empty, logs are kept in the Logstore for two days by default.Note: The value configured here is the initial configuration value. To modify the data retention time later, modify it in the Log Service console.
You can create an application named wordpress
in the Container Service console by using the preceding orchestration file. After the application is started, you can find the Logstore acslog-wordpress-dbstdout
in the Log Service console, in which stores the logs of application wordpress
.
View logs in Log Service console
After deploying an application by using the preceding orchestration file, you can view the collected logs in the Alibaba Cloud Log Service console.
Log on to the Log Service console.
Click the name of the Log Service project that corresponds to the cluster. You can view the Logstore
acslog-wordpress-dbstdout
used in the orchestration file.Click Search at the right of the Logstore to view the logs.
Use file logs
To write the logs directly to files (for example, /var/log/app.log
) instead of stdout, configure as follows:
aliyun.log_store_name: /var/log/app.log
name
is the Logstore name. /var/log/app.log
is the log path in the container.
To output multiple log files to Log Service, configure as follows to put the files under multiple directories:
aliyun.log_store_s1: /data/logs/access/access.log
aliyun.log_store_s2: /data/logs/error/error.log
aliyun.log_store_s3: /data/logs/exception/*.log #Wildcards are supported.
Note: Currently, multiple Logstores cannot correspond to the same log directory. The log files corresponding to the three Logstores s1, s2, and s3 in the preceding example must be under three directories.
Enable timestamp
You can select whether or not to add timestamp when Docker is collecting logs. Configure timestamp by using the aliyun.log.timestamp
label in Container Service. The timestamp is added by default.
Add timestamp
aliyun.log.timestamp: "true"
Remove timestamp
aliyun.log.timestamp: "false"