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

Prerequisites

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

Configure an endpoint and an AccessKey pair for the default account

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

  1. Log on to the server on which the 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 and 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 file. If the configuration file contains content similar to the following information, the default account is configured.
    [main]
    access-id = LTAI******pLMZ
    access-key = XjAsP******eRqax
    region-endpoint = cn-hangzhou.log.aliyuncs.com
    sts-token =
    Note Find the .aliyunlogcli file in one of the following system-specific paths:
    • Linux: ~/.aliyunlogcli
    • Windows: C:\Users\UserName\.aliyunlogcli

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

Configure endpoints and AccessKey pairs for multiple accounts

If you want to manage log data across accounts, you must configure multiple accounts.

  1. Log on to the server on which the 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 and 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 configured.
  3. Verify the configuration result.
    Modify the ~/.aliyunlogcli file. If the configuration file contains content similar to the following information, the account is 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 Find the .aliyunlogcli file in one of the following system-specific paths:
    • Linux: ~/.aliyunlogcli
    • Windows: C:\Users\UserName\.aliyunlogcli

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

    If you use the 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 projects.

Configure the output format

You can use the Log Service CLI to format the output and escape characters in the output. To format the output and escape characters, use one of the following methods:

  • Format the JSON output
    By default, the output returned by the Log Service CLI is in the JSON format and displayed in one row. Therefore, the output is difficult to read. For better readability, you can format the JSON output by using one of the following methods:
    • Format the output of a specified command.

      For example, if you run the aliyunlog log get_log .... --format-output=json command, the JSON output of the get_log command is formatted.

    • Format the output of all commands.

      If you run the aliyunlog configure --format-output=json command, the JSON output of all commands is formatted.

  • Escape characters

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

    If you run the aliyunlog configure --format-output=no_escape command, the output of all commands in the Log Service CLI is not escaped. The original string is returned.

Escape special characters

If you specify a query statement that contains special characters such as dollar signs ($), grave accent (`), backslashes (\), or exclamation points (!) when you run a command of the Log Service CLI, an error may occur or the query result may be inaccurate.

In this case, you must use backslashes (\) to escape the special characters. You can also use single quotation marks (') to force the system to interpret the special characters.
  • 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.