Configure SLS CLI to set default credentials, endpoint, and output format for all commands.
Credential sources and priority
SLS CLI reuses Alibaba Cloud CLI credentials when available—no additional account setup is needed. Configure Alibaba Cloud CLI.
Credential resolution priority (highest to lowest):
|
Credential source |
Description |
|
Subcommand parameters (AccessKey pair, region) |
Credentials passed directly in the subcommand. |
|
The profile parameter in SLS CLI |
Uses the profile specified by |
|
ALIYUN_LOG_CLI environment variable |
Uses the ALIYUN_LOG_CLI environment variable. |
|
SLS CLI configuration file |
Named .aliyunlogcli. For more information, see Configure the default account and Configure multiple accounts. |
|
ALIBABACLOUD environment variable (Alibaba Cloud CLI) |
Uses the ALIBABACLOUD environment variable. |
|
ALICLOUD environment variable (Alibaba Cloud CLI) |
Uses the ALICLOUD environment variable. |
|
Alibaba Cloud CLI configuration file |
Uses the Alibaba Cloud CLI configuration file. Note
Configuration file paths:
|
Configure the default account
SLS CLI uses the main account by default. Configure an AccessKey pair and endpoint for the main account and grant it permissions to manage projects.
-
Log on to the SLS CLI server.
-
Configure the default account credentials.
Run the following command:
aliyunlog configure "AccessKey ID" "AccessKey Secret" Endpoint-
Replace AccessKey ID and AccessKey Secret with the credentials of an account with SLS permissions. For more information, see AccessKey pair and Create a RAM user and grant permissions.
-
Replace Endpoint with a project endpoint. Use a public endpoint. For more information, see Endpoint.
-
-
Verify the configuration.
Open the .aliyunlogcli configuration file. If it contains output similar to the following, the default account is configured.
[main] access-id = LTAI******pLMZ access-key = XjAsP******eRqax region-endpoint = cn-hangzhou.log.aliyuncs.com sts-token =NoteThe .aliyunlogcli file path by operating system:
-
Linux: ~/.aliyunlogcli
-
Windows: C:\Users\UserName\.aliyunlogcli
If configuration fails, fix the error based on the returned error code.
-
Configure multiple accounts
To manage log data across accounts, configure credentials and endpoints for each account.
-
Log on to the SLS CLI server.
-
Configure credentials for additional accounts.
Run the following command:
aliyunlog configure "AccessKey ID" "AccessKey Secret" Endpoint Name-
Replace AccessKey ID and AccessKey Secret with the credentials of an account with SLS permissions. For more information, see AccessKey pair and Create a RAM user and grant permissions.
-
Replace Endpoint with a project endpoint. Use a public endpoint. For more information, see Endpoint.
-
Replace Name with a custom account name.
-
-
Verify the configuration.
Open the ~/.aliyunlogcli configuration file. If it contains output similar to the following, the accounts are configured.
[main] access-id = LTAI******pLMZ access-key = XjAsP******eRqax region-endpoint = cn-hangzhou.log.aliyuncs.com sts-token = [test] access-id = As******sPzvb access-key = FtagJeR******bQqax region-endpoint = cn-shanghai.log.aliyuncs.com sts-token =NoteThe .aliyunlogcli file path by operating system:
-
Linux: ~/.aliyunlogcli
-
Windows: C:\Users\UserName\.aliyunlogcli
If configuration fails, fix the error based on the returned error code.
Use
--client-name=testNameto run a command with a specific account. For example,aliyunlog log create_project ..... --client-name=testruns the command as the test account. -
Format output
SLS CLI supports JSON formatting and character escaping for output.
-
Format JSON output
By default, SLS CLI outputs JSON in a single line. Format the output using one of these methods:
-
Format the output of a specific command
Example:
aliyunlog log get_log .... --format-output=jsonformats the JSON output of the get_log command. -
Format the output of all commands
Run
aliyunlog configure --format-output=jsonto format all command output globally.
-
-
Escape characters
By default, non-English characters in output are escaped. To return original characters, set
--format-outputtono_escape.Run
aliyunlog configure --format-output=no_escapeto disable escaping globally.
Escape special characters
Queries containing dollar signs ($), grave accents (`), backslashes (\), or exclamation points (!) may fail or return incorrect results.
Escape these characters with backslashes (\) or single quotes ('):
-
Escape special characters with backslashes (\):
aliyunlog log get_log_all --project="my-project" --logstore="my-logstore" --query="event_name:\$_enter" --from_time="2021-06-24 15:00:00+08:00" --to_time="2021-06-24 16:00:00+08:00" -
Enclose the query in single quotes ('):
aliyunlog log get_log_all --project="my-project" --logstore="my-logstore" --query='event_name:$_enter' --from_time="2021-06-24 15:00:00+08:00" --to_time="2021-06-24 16:00:00+08:00"
The Bash Quoting manual covers additional escaping rules.