--- title: LogCLI getting started menuTItle: Getting started description: Installation and reference for LogCLI, a command-line tool for querying and exploring logs in Grafana Loki. aliases: - ../query/logcli/ weight: 150 --- # LogCLI getting started logcli is a command-line client for Loki that lets you run [LogQL](https://grafana.com/docs/loki//query) queries against your Loki instance. The `query` command will output extra information about the query and its results, such as the API URL, set of common labels, and set of excluded labels. This is useful, for example, if you want to download a range of logs from Loki. Or want to perform analytical administration tasks, for example, discover the number of log streams to understand your label cardinality, or find out the estimated volume of data that a query will search over. You can also use logcli as part of shell scripts. If you are a Grafana Cloud user, you can also use logcli to query logs that you have exported to long-term storage with [Cloud Logs Export](https://grafana.com/docs/grafana-cloud/send-data/logs/export/), or any other Loki formatted log data. {{< admonition type="note" >}} Note that logcli is a querying tool, it cannot be used to ingest logs. {{< /admonition >}} ## Install logcli As a best practice, you should download the version of logcli that matches your Loki version. And upgrade your logcli when you upgrade your version of Loki. ### Binary (Recommended) Download the `logcli` binary from the [Loki releases page](https://github.com/grafana/loki/releases). Builds are available for Linux, Mac, and Windows. ### Build LogCLI from source Clone the Loki repository and build `logcli` from source: ```bash git clone https://github.com/grafana/loki.git cd loki make logcli ``` Optionally, move the binary into a directory that is part of your `$PATH`. ```bash cp cmd/logcli/logcli /usr/local/bin/logcli ``` ## Set up command completion You can set up tab-completion for `logcli` with one of the two options, depending on your shell: - For bash, add this to your `~/.bashrc` file: ```bash eval "$(logcli --completion-script-bash)" ``` - For zsh, add this to your `~/.zshrc` file: ```bash eval "$(logcli --completion-script-zsh)" ``` ## LogCLI usage Once you have installed logcli, you can run it in the following way: `logcli [, ...]` `` points to one of the commands, detailed in the [command reference](https://grafana.com/docs/loki//query/logcli/getting-started/#logcli-command-reference) below. `` is one of the subcommands available for each command. `` is a list of space separated arguments. Arguments can optionally be overridden using environment variables. Environment variables will always take precedence over command line arguments. ### Authenticate to Loki To connect to a Loki instance, set the following argument: - `--addr=http://loki.example.com:3100` or the `LOKI_ADDR` environment variable For example, to query a local Loki instance directly without needing a username and password: ```bash export LOKI_ADDR=http://localhost:3100 logcli query '{service_name="website"}' ``` To connect to a Loki instance which requires authentication, you will need to additionally set the following arguments: - `--username` or the `LOKI_USERNAME` environment variable - `--password` or the `LOKI_PASSWORD` environment variable For example, to query Grafana Cloud: ```bash export LOKI_ADDR=https://logs-us-west1.grafana.net export LOKI_USERNAME= export LOKI_PASSWORD= logcli query '{service_name="website"}' ``` To specify a particular tenant, set the following argument: - `--org-id` or the `LOKI_ORG_ID` environment variable {{< admonition type="note" >}} If you are running Loki behind a proxy server and you have [authentication](https://grafana.com/docs/loki//operations/authentication/) configured, you will also have to pass in LOKI_USERNAME and LOKI_PASSWORD, LOKI_BEARER_TOKEN or LOKI_BEARER_TOKEN_FILE accordingly. {{< /admonition >}} ## LogCLI command reference The output of `logcli help`: ```shell usage: logcli [][ ...] A command-line for Loki. Flags: --help Show context-sensitive help (also try --help-long and --help-man). --version Show application version. -q, --quiet Suppress query metadata --stats Show query statistics -o, --output=default Specify output mode [default, raw, jsonl]. raw suppresses log labels and timestamp. -z, --timezone=Local Specify the timezone to use when formatting output timestamps [Local, UTC] --cpuprofile="" Specify the location for writing a CPU profile. --memprofile="" Specify the location for writing a memory profile. --stdin Take input logs from stdin --addr="http://localhost:3100" Server address. Can also be set using LOKI_ADDR env var. --username="" Username for HTTP basic auth. Can also be set using LOKI_USERNAME env var. --password="" Password for HTTP basic auth. Can also be set using LOKI_PASSWORD env var. --ca-cert="" Path to the server Certificate Authority. Can also be set using LOKI_CA_CERT_PATH env var. --tls-skip-verify Server certificate TLS skip verify. --cert="" Path to the client certificate. Can also be set using LOKI_CLIENT_CERT_PATH env var. --key="" Path to the client certificate key. Can also be set using LOKI_CLIENT_KEY_PATH env var. --org-id="" adds X-Scope-OrgID to API requests for representing tenant ID. Useful for requesting tenant data when bypassing an auth gateway. Commands: help [...] Show help. query [] Run a LogQL query. The "query" command is useful for querying for logs. Logs can be returned in a few output modes: raw: log line default: log timestamp + log labels + log line jsonl: JSON response from Loki API of log line The output of the log can be specified with the "-o" flag, for example, "-o raw" for the raw output format. The "query" command will output extra information about the query and its results, such as the API URL, set of common labels, and set of excluded labels. This extra information can be suppressed with the --quiet flag. By default we look over the last hour of data; use --since to modify or provide specific start and end times with --from and --to respectively. Notice that when using --from and --to then ensure to use RFC3339Nano time format, but without timezone at the end. The local timezone will be added automatically or if using --timezone flag. Example: logcli query --timezone=UTC --from="2021-01-19T10:00:00Z" --to="2021-01-19T20:00:00Z" --output=jsonl 'my-query' The output is limited to 30 entries by default; use --limit to increase. While "query" does support metrics queries, its output contains multiple data points between the start and end query time. This output is used to build graphs, similar to what is seen in the Grafana Explore graph view. If you are querying metrics and just want the most recent data point (like what is seen in the Grafana Explore table view), then you should use the "instant-query" command instead. instant-query [] Run an instant LogQL query. The "instant-query" command is useful for evaluating a metric query for a single point in time. This is equivalent to the Grafana Explore table view; if you want a metrics query that is used to build a Grafana graph, you should use the "query" command instead. This command does not produce useful output when querying for log lines; you should always use the "query" command when you are running log queries. For more information about log queries and metric queries, refer to the LogQL documentation: https://grafana.com/docs/loki//logql/ labels [] [