After you configure Log Service CLI, you do not need to configure global parameters each time you run a command. The global parameters include the AccessKey pair, endpoint, and output format. This topic describes how to configure Log Service CLI.

Prerequisites

Log Service CLI is installed. For more information, see Install Log Service CLI.

Use the credentials of Alibaba Cloud CLI

Log Service CLI reads credentials from the following sources in descending order of priority: subcommands of Log Service CLI, configuration file of Log Service CLI, configuration file of Alibaba Cloud CLI, and environment variables.

Credential source in descending order of priority Description
The global parameters that are passed to the subcommands of Log Service CLI, such as an AccessKey pair and a region When a subcommand is run, Log Service CLI preferentially reads credentials from the parameters in the subcommand for verification. This type of source has the highest priority. For more information, see Global parameters.
The profile parameter that is configured for Log Service CLI When a subcommand is run, Log Service CLI reads credentials based on the following setting: --profile=Name of a credential configuration in the config.json file of Alibaba Cloud CLI. For more information about the --profile parameter, see Global parameters.
The ALIYUN_LOG_CLI environment variable that is configured for Log Service CLI Log Service CLI reads credentials from the ALIYUN_LOG_CLI environment variable that is configured in the system.
The configuration file of Log Service CLI The file is named in the .aliyunlogcli format. For more information, see Configure an endpoint and an AccessKey pair for the default account and Configure endpoints and AccessKey pairs for multiple accounts.
The ALIBABACLOUD environment variable that is configured for Alibaba Cloud CLI Log Service CLI reads credentials from the ALIBABACLOUD environment variable that is configured in the system.
The ALICLOUD environment variable that is configured for Alibaba Cloud CLI Log Service CLI reads credentials from the ALICLOUD environment variable that is configured in the system.
The configuration file of Alibaba Cloud CLI Log Service CLI reads credentials from the configuration file of Alibaba Cloud CLI. Log Service CLI can use the credentials that are configured for Alibaba Cloud CLI to verify accounts. For more information, see Configure Alibaba Cloud CLI.
Note The path to the configuration file varies based on the operating system that is in use.
  • Linux: ~/.aliyun/config.json
  • Windows: C:\Users\UserName\.aliyun\config.json

Configure an endpoint and an AccessKey pair for the default account

By default, Log Service CLI uses the main account to perform all operations. Before you can use Log Service CLI, you must configure the main account.

  1. Log on to the server on which Log Service CLI is installed.
  2. Configure an endpoint and an AccessKey pair for the default account.
    Run the following command:
    aliyunlog configure AccessKey ID AccessKey Secret Endpoint
    • Replace AccessKey ID with the AccessKey ID of an account that has permissions on Log Service. Replace AccessKey Secret with the AccessKey secret of the account. For more information, see AccessKey pair.
    • Replace Endpoint with the endpoint of a project. For more information, see Endpoints.
  3. Verify the configuration result.
    Modify the .aliyunlogcli configuration file. If the file contains information similar to the following example, the default account is configured.
    [main]
    access-id = LTAI******pLMZ
    access-key = XjAsP******eRqax
    region-endpoint = cn-hangzhou.log.aliyuncs.com
    sts-token =
    Note The path to the .aliyunlogcli configuration file varies based on the operating system that is in use.
    • Linux: ~/.aliyunlogcli
    • Windows: C:\Users\UserName\.aliyunlogcli

    If the default account fails to be configured, you can fix the error based on the error code that is returned.

Configure endpoints and AccessKey pairs for multiple accounts

If you want to manage log data across multiple accounts, you must configure endpoints and AccessKey pairs for the accounts.

  1. Log on to the server on which Log Service CLI is installed.
  2. Configure AccessKey pairs and endpoints for multiple accounts.
    Run the following command:
    aliyunlog configure AccessKey ID AccessKey Secret Endpoint testName
    • Replace AccessKey ID with the AccessKey ID of an account that has permissions on Log Service. Replace AccessKey Secret with the AccessKey secret of the account. For more information, see AccessKey pair.
    • Replace Endpoint with the endpoint of a project. For more information, see Endpoints.
    • Replace testName with the name of an account that you configure.
  3. Verify the configuration result.
    Modify the ~/.aliyunlogcli configuration file. If the file contains information similar to the following example, 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 =
    Note The path to the .aliyunlogcli configuration file varies based on the operating system that is in use.
    • Linux: ~/.aliyunlogcli
    • Windows: C:\Users\UserName\.aliyunlogcli

    If the accounts fail to be configured, you can fix the error based on the error code that is returned.

    If you use Log Service CLI to run a command, you can use --client-name=testName to specify the account that you want to use. For example, if you run the aliyunlog log create_project ..... --client-name=test command, the test account is used to create the project.

Specify the output format

Log Service CLI can format output and escape characters in the output. If you want to format output and escape characters, you can use the following methods:

  • Format JSON output
    By default, the output of Log Service CLI is in the JSON format and is displayed in one line. In this case, the output is difficult to read. To improve readability, you can use one of the following methods to format the JSON output:
    • Format the output of a specified command

      For example, you can use the aliyunlog log get_log .... --format-output=json command to format the JSON output of the get_log command.

    • Format the output of all commands

      You can use the aliyunlog configure --format-output=json command to format the JSON output of all commands.

  • Escape characters

    By default, the non-English characters in the output of Log Service CLI are escaped characters. If you want Log Service CLI to return the original characters, such as Chinese strings, you can specify no_escape for --format-output.

    If you run the aliyunlog configure --format-output=no_escape command, Log Service CLI does not escape characters in the output of commands. The original characters are returned.

Escape special characters

If you run a command in Log Service CLI and the query statement contains dollar signs ($), grave accents (`), backslashes (\), or exclamation points (!), an error occurs during the execution, or the query result is inaccurate.

In this case, you must use backslashes (\) to escape the special characters or use single quotation marks (') to reference the special characters. Examples:
  • If you want to escape the special characters in the query parameter by using backslashes (\), run the following command:

    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"

  • If you want to reference the special characters in the query parameter by using single quotation marks ('), run the following command:

    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"

For more information, see Quoting.