This topic describes how to use Simple Log Service (SLS) LoongCollector (Logtail) to incrementally collect text logs from servers, such as Elastic Compute Service (ECS) instances and self-managed Linux or Windows servers. To collect full logs, import historical logs.
Permissions
Alibaba Cloud account: This account has all permissions by default and can perform operations directly.
Resource Access Management (RAM) user: The Alibaba Cloud account must grant the RAM user the required access policies.
System policies
If you use system-defined policies, add the following permissions:
AliyunLogFullAccess
: Manages SLS.AliyunECSFullAccess
: Manages ECS.(Optional)
AliyunOOSFullAccess
: Required for one-click installation of LoongCollector (Logtail) using CloudOps Orchestration Service (OOS).
Custom policies (for fine-grained control)
If system policies do not meet the requirements of the principle of least privilege, create a custom policy. The following sample policy includes permissions to:
View projects: View the list of projects and the details of a specific project.
Manage logstores: Create, modify, or delete logstores in a project.
Manage collection configurations: Create, delete, and modify collection configurations.
View logs: Query and analyze data in a specific logstore within a specific project.
Replace${regionName}
,${uid}
,${projectName}
, and${logstoreName}
with your actual region name, Alibaba Cloud account ID, project, and logstore.
Permission | Corresponding operations | Resource |
Read-only access to projects |
|
|
Get a specific project |
|
|
Manage logstores |
|
|
Manage LoongCollector (Logtail) data collection |
|
|
Query saved searches |
|
|
Query dashboards |
|
|
Query logs in a specific logstore |
|
|
Permissions to operate ECS |
|
|
Permissions to operate OOS (Optional) Required only when you automatically install LoongCollector (Logtail) using OOS for an ECS instance in the same account and region as SLS. |
|
|
Collection configuration workflow
Create a project and a logstore: A project is a resource management unit that isolates logs from different services. A logstore is used to store logs.
Install LoongCollector: LoongCollector is a new-generation log collection agent and an upgraded version of Logtail.
Create a collection configuration:
This topic describes only common configuration parameters and core options for typical scenarios. For a complete list of parameters and their descriptions, see More information.
Global Configurations: Defines the name of the collection configuration.
Input Configurations: Defines the collection source.
Processor Configurations: Parses raw logs into structured data, enables multiline log collection, or performs data masking and filtering.
Output Configurations: Configures the compression method for log transmission.
Other advanced configurations: Configures the topic type, collection blacklist, and initial collection size, and allows a file to be collected by multiple configurations.
Create a project and a logstore
If you have already created a project and a logstore, skip this step and proceed to install LoongCollector (Logtail).
Log on to the Simple Log Service console.
Click Create Project.
Configure the following parameters:
Region: Select the region based on the log source. This setting cannot be changed after the project is created.
Project Name: Must be globally unique within Alibaba Cloud. This name cannot be changed after the project is created.
Keep the default settings for other parameters and click Create. For more information about other parameters, see Manage projects.
Click the project name to open the project.
In the navigation pane on the left, choose
and click +.
On the Create Logstore page, configure the following core parameters:
Logstore Name: Set a unique name within the project. This name cannot be changed after creation.
Logstore Type: Select Standard or Query based on the provided specification comparison.
Billing Mode:
Pay-by-feature: Billed independently for resources such as storage, indexing, and read/write operations. This mode is suitable for small-scale scenarios or scenarios with uncertain functional requirements.
Pay-by-ingested-data: Billing is based only on the amount of raw data written. This mode provides a 30-day free storage period and free features such as data transformation and delivery. The cost model is simple and suitable for scenarios where the storage period is close to 30 days or the data processing pipeline is complex.
Data Retention Period: Set the number of days to retain logs. The value can range from 1 to 3,650 days. A value of 3,650 indicates permanent storage. The default is 30 days.
Keep the default settings for other configurations and click OK. For more information about other configurations, see Manage logstores.
Install LoongCollector (Logtail)
This topic provides only the basic steps to install LoongCollector. For more information, see Install LoongCollector (Linux).
If you have already installed LoongCollector or Logtail, skip this step and proceed to create a collection configuration.
|
SLS provides multiple text log access templates, such as regular expression and single-line templates. These templates differ only in their parsing plugins. All other configurations are identical. Add or remove parsing plugins within a template. Select a template as needed, or select a template and then configure the plugins accordingly.
Complete the Machine Group Configuration, then click Next.
Scenario: Select Servers.
Installation Environment: Supports ECS, Self-managed Machine - Linux, and Self-managed Machine - Windows.
Click Create Machine Group:
ECS
Select one or more ECS instances that are in the same region as the project.
Click Install and Create Machine Group and wait for the installation to complete.
Configure the machine group name and click OK.
NoteIf the installation fails or remains pending, ensure that the ECS region is the same as the project region. If the ECS instance and the project are in different accounts or regions, see Install LoongCollector (Linux).
Self-managed machine - Linux
Copy the installation command that corresponds to your network type. Then, run the command on your server to download and install LoongCollector.
The command that you obtain from the console is a complete compound command. It includes steps for downloading the installation package, adding execution permissions, and installing LoongCollector. These steps are connected by semicolons (
;
), which lets you execute the command with a single click.Internet: Select this option to transmit data over the internet in the following two cases:
The ECS instance and the SLS project are in different regions.
The server is from another cloud provider or a self-managed data center.
Global Accelerator: If your business server is in a region within the Chinese mainland and your SLS project is in a region outside the Chinese mainland, or vice versa, transmitting data over the internet may cause high network latency and instability. Use transfer acceleration to transmit data.
You must first enable Accelerate cross-region log transfer for the project before you execute the installation command.
After installation, run the following command to check the startup status. If
loongcollector is running
is returned, LoongCollector has started successfully.sudo /etc/init.d/loongcollectord status
(Optional) Configure the Alibaba Cloud account ID as a user identifier. You need to configure a user ID only when you collect logs from an ECS instance that belongs to another account, a self-managed server, or a server from another cloud provider.
Copy the following command from the console:
touch /etc/ilogtail/users/155***********44
On the target server, run the command to create the user identifier file.
Configure the machine group:
On the server, write the custom string
user-defined-test-1
to the custom identifier file.# Write a custom string to the specified file. If the directory does not exist, create it manually. The file path and name are fixed by Simple Log Service and cannot be customized. echo "user-defined-test-1" > /etc/ilogtail/user_defined_id
In the Configure Machine Group section of the console, configure the following parameters and click OK:
Name: Set the machine group name. The name must be unique within the project, start and end with a lowercase letter or a digit, and contain only lowercase letters, digits, hyphens (-), and underscores (_). The name must be 3 to 128 characters in length.
Machine Group Identifier: Select Custom Identifier.
Custom Identifier: Enter the configured custom identifier. It must be the same as the custom string in the server's custom identifier file. In this example, it is
user-defined-test-1
.
Click Next. Check the machine group heartbeat status:
If the status is FAIL: It may take some time to establish the initial heartbeat. Wait for approximately two minutes and then refresh the heartbeat status.
If the status is still FAIL, see What should I do if the machine group heartbeat connection fails? for troubleshooting.
If the status is OK: The machine group connection is normal.
Click Next to go to the Logtail configuration page.
Self-managed machine - Windows
LoongCollector does not support Windows. To collect logs from a Windows server, you need to install Logtail.
In the console, download the installation package based on the region.
Unzip
loongcollector_installer.zip
to the current directory.Run Windows PowerShell or cmd as an administrator and navigate to the
loongcollector_installer
directory, which is where you extracted the installation package. In the console, copy the installation command that corresponds to your network type:Internet: Suitable for most scenarios, commonly used for cross-region or other cloud/self-managed servers, but it is subject to bandwidth limitations and potential instability.
Global Accelerator: Used for cross-region scenarios (such as from the Chinese mainland to outside China) to improve performance through CDN acceleration and avoid high latency and internet instability. However, traffic is billed separately.
You must first enable the Cross-Domain Log Transfer Acceleration feature for the project before you execute the installation command.
(Optional) Configure the Alibaba Cloud account ID as a user identifier. You need to configure a user ID only when you collect logs from an ECS instance that belongs to another account, a self-managed server, or a server from another cloud provider.
Create a file named after the Alibaba Cloud account ID in the C:\LogtailData\users directory. For example:
C:\LogtailData\users\155***********44
.Configure the machine group:
On the server, create the custom identifier file named user_defined_id in the
C:\LogtailData
directory.If the C:\LogtailData directory does not exist, create it manually.
Write the custom string
user-defined-test-1
to the file C:\LogtailData\user_defined_id.A machine group cannot contain both Linux and Windows servers. Do not configure the same custom identifier on both Linux and Windows servers. A server can be configured with multiple custom identifiers, separated by line breaks.
In the Configure Machine Group section of the console, configure the following parameters and click OK:
Name: Set the machine group name. The name must be unique within the project, start and end with a lowercase letter or a digit, and contain only lowercase letters, digits, hyphens (-), and underscores (_). The name must be 3 to 128 characters in length.
Machine Group Identifier: Select Custom Identifier.
Custom Identifier: Enter the configured custom identifier. It must be the same as the custom string in the server's custom identifier file. In this example, it is
user-defined-test-1
.
Click Next. Check the machine group heartbeat status:
If the status is FAIL: It may take some time to establish the initial heartbeat. Wait for approximately two minutes and then refresh the heartbeat status.
If the status is still FAIL, see What should I do if the machine group heartbeat connection fails? for troubleshooting.
If the status is OK: The machine group connection is normal.
Click Next to go to the Logtail configuration page.
Global Configurations
Configuration Name: The name of the collection configuration. It must be unique within its project. The name cannot be changed after creation. The name must follow these conventions:
It can contain only lowercase letters, digits, hyphens (-), and underscores (_).
It must start and end with a lowercase letter or a digit.
Input Configurations
Type: Text Log Collection.
File Path: The path for log collection.
Linux: Must start with a forward slash (/), such as
/data/mylogs/**/*.log
. This indicates all files with the .log extension in the/data/mylogs
directory and its subdirectories.Windows: Must start with a drive letter, such as
C:\Program Files\Intel\**\*.Log
.
Maximum Directory Monitoring Depth: The maximum directory depth that the wildcard character
**
in the File Path can match. The default is 0, which means only the current directory is monitored.
Processor Configurations
This section describes only native processing plugins that cover common log processing scenarios. For more features, see Extended processing plugins.
For Logtail 2.0 and later versions, and for the LoongCollector component, follow these plugin combination rules:
Use native plugins first.
If native plugins cannot meet your needs, configure extended plugins after the native ones.
Native plugins can be used only before extended plugins.
Structured configuration
If you selected a text log access template based on your log format when you installed LoongCollector (Logtail), the system automatically pre-configures the corresponding parsing plugin for you. However, this plugin still needs to be configured manually.
Click the plugin name to go to the configuration page. Configure the parsing plugin as described in the following sections. You can also add other parsing plugins or remove unnecessary ones as needed.
Regular expression parsing
Use regular expressions to extract log fields and parse the log into key-value pairs.
| Raw log:
|
Custom regular expression parsing: Regular expression
|
Delimiter-based parsing
Use a delimiter to structure the log content and parse it into multiple key-value pairs. Both single-character and multi-character delimiters are supported.
Click Add Processor and choose :
| Raw log:
|
Split fields by the specified character
|
Standard JSON parsing
Structure an object-type JSON log and parse it into key-value pairs.
Click Add Processor and choose :
| Raw Log:
|
Standard JSON key-value pairs are automatically extracted:
|
Nested JSON parsing
Parse a nested JSON log into key-value pairs by specifying the expansion depth.
Click Add Processor and choose :
| Raw log:
|
Expansion depth: 0, with the expansion depth used as a prefix.
| |
Expansion depth: 1, with the expansion depth used as a prefix.
|
JSON array parsing
Use the json_extract
function to extract JSON objects from a JSON array.
Set Processing Method to SPL:
| Raw log:
|
Extracted JSON array structure:
|
NGINX log parsing
Structure the log content into multiple key-value pairs based on the definition in log_format. If the default content does not meet your needs, use a custom format.
Click Add Processor and choose :
| Raw log:
|
Parsed into key-value pairs based on the
|
Apache log parsing
Structure the log content into multiple key-value pairs based on the definition in the Apache log configuration file.
Click Add Processor and choose :
| Raw log:
|
Apache Common Log Format
|
IIS log parsing
This plugin structures log content based on the IIS log format definition, parsing it into multiple key-value pairs.
Click Add Processing Plug-in and select :
| Raw log:
|
Adaptation for Microsoft IIS server-specific format:
|
Data masking
Mask sensitive data in logs.
In the Processor Configurations section, click Add Processor and choose :
| Raw log:
|
Masking result:
|
Content filtering
Match log field values based on regular expressions and collect only logs that meet the whitelist conditions.
In the Processor Configurations section, click Add Processor and choose :
| Raw log:
|
Filtered log: Set Field Name to
|
Time parsing
Parse the time field in the log and set the parsing result as the log's __time__
field.
In the Processor Configurations section, click Add Processor and choose :
| Raw log:
|
Time parsing: |
Multiline log collection
By default, SLS works in single-line mode, treating each line of text as a separate log. This can incorrectly split multiline logs containing content such as stack traces or JSON, leading to a loss of context.
To address this issue, enable Multi-line Mode and define a Regex to Match First Line. This allows SLS to accurately identify the starting line of a complete log, thereby merging multiple lines into a single log entry.
Processor Configurations:
| Raw log:
|
Single-line mode: Each line is a separate log, and the stack information is broken up, losing context. | |
Multi-line mode: A first-line regular expression identifies the complete log, preserving the full semantic structure.
|
Output Configurations
Configure the log compression method.
Only Logtail 1.3.4 and later versions support zstd compression.
lz4: Fast compression speed with a lower compression ratio.
zstd: High compression ratio with a slightly lower speed and higher memory usage.
Other advanced configurations
Configure topic types
: Select the topic generation method.
Machine Group Topic: Simple Log Service lets you apply one collection configuration to multiple machine groups. When LoongCollector reports data, it uses the machine group's topic as the log topic and uploads it to the Logstore. You can use topics to distinguish logs from different machine groups.
File Path Extraction: If different users or applications write logs to different top-level directories but with the same subdirectory paths and filenames, it becomes difficult to distinguish the log source from the filename. In this case, you can configure File Path Extraction. Use a regular expression to match the full file path and use the matched result (username or application name) as the log topic to be uploaded to the Logstore.
NoteIn the regular expression for the file path, you must escape the forward slash (/).
Extract using a file path regular expression
Use case: Different users record logs in different directories, but the log filenames are the same. The directory paths are as follows.
/data/logs ├── userA │ └── serviceA │ └── service.log ├── userB │ └── serviceA │ └── service.log └── userC └── serviceA └── service.log
If you only configure the file path as
/data/logs
and the filename asservice.log
in the Logtail Configuration, LoongCollector (Logtail) will collect the content from all threeservice.log
files into the same Logstore. This makes it impossible to distinguish which user produced which log. In this case, you can use a regular expression to extract values from the file path to generate different log topics.Regular expression
Extraction result
\/data\/logs\/(.*)\/serviceA\/.*
__topic__: userA __topic__: userB __topic__: userC
Extract using multiple capturing groups
Use case: If a single log topic is not enough to distinguish the source of the logs, you can configure multiple regular expression capturing groups in the log file path to extract key information. These capturing groups include named capturing groups (?P<name>) and unnamed capturing groups.
Named capturing group: The generated tag field is
__tag__:{name}
.Unnamed capturing group: The generated tag field is
__tag__:__topic_{i}__
, where{i}
is the sequence number of the capturing group.
NoteWhen there are multiple capturing groups in the regular expression, the
__topic__
field is not generated.For example, if the file path is
/data/logs/userA/serviceA/service.log
, you can extract multiple values from the file path in the following ways:Example
Regular expression
Extraction result
Use an unnamed capturing group for regular expression extraction.
\/data\/logs\/(.*?)\/(.*?)\/service.log
__tag__:__topic_1__: userA __tag__:__topic_2__: serviceA
Use a named capturing group for regular expression extraction.
\/data\/logs\/(?P<user>.*?)\/(?P<service>.*?)\/service.log
__tag__:user: userA __tag__:service: serviceA
Validation: After configuration, you can query logs based on the log topic.
On the log query and analysis page, enter the corresponding generated log topic, such as
__topic__: userA
or__tag__:__topic_1__: userA
, to query logs for that topic.Custom: Enter
customized:// + custom topic name
to use a custom static log topic.
Blacklist
: Enable Collection Blacklist, click Add, and configure the blacklist.
Supports full matching and wildcard matching for directories and filenames. Wildcard characters only support the asterisk (*) and the question mark (?).
File Path Blacklist: The file path to be ignored. Example:
/home/admin/private*.log
: During collection, ignore all files in the/home/admin/
directory that start with "private" and end with ".log"./home/admin/private*/*_inner.log
: During collection, ignore files ending with "_inner.log" within directories that start with "private" under the/home/admin/
directory.
File Blacklist: Configure the filenames to be ignored during collection. Example:
app_inner.log
: During collection, ignore all files namedapp_inner.log
.
Directory Blacklist: The directory path cannot end with a forward slash (/). Example:
/home/admin/dir1/
: The directory blacklist will not take effect./home/admin/dir*
: During collection, ignore files in subdirectories that start with "dir" under the/home/admin/
directory./home/admin/*/dir
: During collection, ignore all files in subdirectories named "dir" at the second level under the/home/admin/
directory. For example, files in the/home/admin/a/dir
directory are ignored, while files in the/home/admin/a/b/dir
directory are collected.
Configure initial collection size
This parameter configures the starting collection position relative to the end of the file when the configuration is first applied.
In the
section, configure the First Collection Size. The default is 1024 KB. The value range is from 0 to 10,485,760 KB.For the initial collection, if a file is smaller than 1024 KB, collection starts from the beginning of the file.
For the initial collection, if a file is larger than 1024 KB, collection starts from 1024 KB before the end of the file.
Allow multiple collections for a file
By default, a log file can match only one LoongCollector (Logtail) configuration. After you enable this option, the same file can be collected by multiple LoongCollector (Logtail) configurations.
In the
section, enable Allow File to Be Collected for Multiple Times.FAQ
How do I send logs from an ECS server to a project in another Alibaba Cloud account?
If you have not yet installed LoongCollector, see Install LoongCollector (Logtail) and choose the appropriate cross-account scenario for installation.
If you have already installed LoongCollector, follow the steps below to configure a user identifier. This identifier indicates that the server has permission to be accessed and for its logs to be collected by the account that owns the SLS project.
You need to configure a user identifier only when you collect logs from an ECS instance that belongs to another account, a self-managed data center, or a server from another cloud provider.
Copy the ID of the Alibaba Cloud account that owns SLS: Hover over your profile picture in the upper-right corner. In the tab that appears, view and copy the account ID.
Log on to the server from which you want to collect logs and create an Alibaba Cloud account ID file to configure the user identifier:
touch /etc/ilogtail/users/{Alibaba Cloud account ID} # If the /etc/ilogtail/users directory does not exist, create it manually. The user identifier configuration file only needs a filename, not a file extension.
How do I send logs from an ECS server to a project in a different region under the same account?
If you have not yet installed LoongCollector, see Install LoongCollector (Logtail) and choose the appropriate cross-region scenario for installation.
If you have already installed LoongCollector, you need to modify the LoongCollector configuration.
Run the
sudo /etc/init.d/ilogtaild stop
command to stop LoongCollector.Modify the LoongCollector startup configuration file
ilogtail_config.json
. Choose one of the following two methods based on your network requirements:Configuration file path:
/usr/local/ilogtail/ilogtail_config.json
Method 1: Transmit over the internet
See RegionID and replace the region in the configuration file with the region where the SLS project is located. The fields to be modified include the following:
primary_region
The region part in
config_servers
The
region
and the region part ofendpoint_list
indata_servers
Method 2: Use transfer acceleration
Replace the endpoint in the data_server_list parameter with
log-global.aliyuncs.com
. For more information about the file path, see Logtail network types, startup parameters, and configuration files.
Run the
sudo /etc/init.d/ilogtaild start
command to start LoongCollector.
What should I do if the machine group heartbeat status is FAIL?
Check the user identifier: If your server is not an ECS instance, or if the ECS instance and the project belong to different Alibaba Cloud accounts, check whether the correct user identifier exists in the specified directory according to the table below.
System
Specified directory
Solution
Linux
/etc/ilogtail/users/
Run the
cd /etc/ilogtail/users/ && touch <uid>
command to create the user identifier file.Windows
C:\LogtailData\users\
Go to the
C:\LogtailData\users\
directory and create an empty file named<uid>
.If a file named after the Alibaba Cloud account ID of the current project exists in the specified path, the user identifier is configured correctly.
Check the machine group identifier: If you are using a machine group with a custom identifier, check whether the
user_defined_id
file exists in the specified directory. If it exists, check whether the content of the file matches the custom identifier configured for the machine group.System
Specified directory
Solution
Linux
/etc/ilogtail/user_defined_id
# Configure a custom identifier. If the directory does not exist, create it manually. echo "user-defined-1" > /etc/ilogtail/user_defined_id
Windows
C:\LogtailData\user_defined_id
Create a
user_defined_id
file in theC:\LogtailData
directory and write the custom identifier into it. (If the directory does not exist, create it manually.)If both the user identifier and the machine group identifier are configured correctly, see Troubleshoot LoongCollector (Logtail) machine group issues for further troubleshooting.
No data is collected
Check for incremental logs: After you configure LoongCollector (Logtail) for collection, if no new logs are added to the target log file, LoongCollector (Logtail) does not collect any data from that file.
Check the machine group heartbeat status: Go to the
page, click the name of the target machine group, and in the section, check the heartbeat status.If the heartbeat is OK, the machine group is connected to the Simple Log Service project.
If the heartbeat is FAIL: See What should I do if the machine group heartbeat status is FAIL? for troubleshooting.
Confirm that the LoongCollector (Logtail) collection configuration has been applied to the machine group: Even if a LoongCollector (Logtail) collection configuration is created, logs are not collected if the configuration is not applied to a machine group.
Go to the
page and click the name of the target machine group to go to the Machine Group Configurations page.On the page, view Manage Configuration. The left side shows All Logtail Configurations, and the right side shows Applied Logtail Configurations. If the target LoongCollector (Logtail) collection configuration has been moved to the applied area on the right, the configuration is successfully applied to the target machine group.
If the target LoongCollector (Logtail) collection configuration has not been moved to the applied area on the right, click Modify. In the All Logtail Configurations list on the left, select the target LoongCollector (Logtail) configuration name, click
to move it to the applied area on the right, then click Save.
Log collection errors or format errors
Troubleshooting approach: This type of error indicates that the network and basic configuration are correct. The problem is typically a mismatch between the log content and the parsing rules. You must check the specific error message to identify the cause:
On the Logtail Configuration page, click the name of the LoongCollector (Logtail) configuration that has collection errors. Under the Log Collection Error tab, click Select Time Range to set the query time.
In the Common errors in data collection.
section, view the alarm metrics for the error log and find the corresponding solution in