Querying Loki with LogCLI

If you prefer a command line interface, LogCLI also allows users to run LogQLqueries against a Loki server.

Installation

Navigate to the Loki Releases pageand download the logcli binary for your OS:

  1. # download a binary (adapt app, os and arch as needed)
  2. # installs v0.3.0. For up to date URLs refer to the release's description
  3. $ curl -fSL -o "/usr/local/bin/logcli.gz" "https://github.com/grafana/logcli/releases/download/v0.3.0/logcli-linux-amd64.gz"
  4. $ gunzip "/usr/local/bin/logcli.gz"
  5. # make sure it is executable
  6. $ chmod a+x "/usr/local/bin/logcli"

From source

Use go get to install logcli to $GOPATH/bin:

  1. $ go get github.com/grafana/loki/cmd/logcli

Usage

Example

If you are running on Grafana Cloud, use:

  1. $ export GRAFANA_ADDR=https://logs-us-west1.grafana.net
  2. $ export GRAFANA_USERNAME=<username>
  3. $ export GRAFANA_PASSWORD=<password>

Otherwise you can point LogCLI to a local instance directlywithout needing a username and password:

  1. $ export GRAFANA_ADDR=http://localhost:3100

Note: If you are running Loki behind a proxy server and you haveauthentication configured, you will also have to pass in GRAFANA_USERNAMEand GRAFANA_PASSWORD accordingly.

  1. $ logcli labels job
  2. https://logs-dev-ops-tools1.grafana.net/api/prom/label/job/values
  3. cortex-ops/consul
  4. cortex-ops/cortex-gw
  5. ...
  6. $ logcli query '{job="cortex-ops/consul"}'
  7. https://logs-dev-ops-tools1.grafana.net/api/prom/query?query=%7Bjob%3D%22cortex-ops%2Fconsul%22%7D&limit=30&start=1529928228&end=1529931828&direction=backward&regexp=
  8. Common labels: {job="cortex-ops/consul", namespace="cortex-ops"}
  9. 2018-06-25T12:52:09Z {instance="consul-8576459955-pl75w"} 2018/06/25 12:52:09 [INFO] raft: Snapshot to 475409 complete
  10. 2018-06-25T12:52:09Z {instance="consul-8576459955-pl75w"} 2018/06/25 12:52:09 [INFO] raft: Compacting logs from 456973 to 465169

Configuration

Configuration values are considered in the following order (lowest to highest):

  • Environment variables
  • Command line flags

Details

  1. $ logcli help
  2. usage: logcli [<flags>] <command> [<args> ...]
  3. A command-line for loki.
  4. Flags:
  5. --help Show context-sensitive help (also try --help-long and --help-man).
  6. -q, --quiet suppress everything but log lines
  7. -o, --output=default specify output mode [default, raw, jsonl]
  8. --addr="https://logs-us-west1.grafana.net"
  9. Server address.
  10. --username="" Username for HTTP basic auth.
  11. --password="" Password for HTTP basic auth.
  12. --ca-cert="" Path to the server Certificate Authority.
  13. --tls-skip-verify Server certificate TLS skip verify.
  14. --cert="" Path to the client certificate.
  15. --key="" Path to the client certificate key.
  16. Commands:
  17. help [<command>...]
  18. Show help.
  19. query [<flags>] <query> [<regex>]
  20. Run a LogQL query.
  21. labels [<label>]
  22. Find values for a given label.
  23. $ logcli help query
  24. usage: logcli query [<flags>] <query> [<regex>]
  25. Run a LogQL query.
  26. Flags:
  27. --help Show context-sensitive help (also try --help-long and --help-man).
  28. -q, --quiet suppress everything but log lines
  29. -o, --output=default specify output mode [default, raw, jsonl]
  30. --addr="https://logs-us-west1.grafana.net"
  31. Server address.
  32. --username="" Username for HTTP basic auth.
  33. --password="" Password for HTTP basic auth.
  34. --ca-cert="" Path to the server Certificate Authority.
  35. --tls-skip-verify Server certificate TLS skip verify.
  36. --cert="" Path to the client certificate.
  37. --key="" Path to the client certificate key.
  38. --limit=30 Limit on number of entries to print.
  39. --since=1h Lookback window.
  40. --from=FROM Start looking for logs at this absolute time (inclusive)
  41. --to=TO Stop looking for logs at this absolute time (exclusive)
  42. --forward Scan forwards through logs.
  43. -t, --tail Tail the logs
  44. --delay-for=0 Delay in tailing by number of seconds to accumulate logs for re-ordering
  45. --no-labels Do not print any labels
  46. --exclude-label=EXCLUDE-LABEL ...
  47. Exclude labels given the provided key during output.
  48. --include-label=INCLUDE-LABEL ...
  49. Include labels given the provided key during output.
  50. --labels-length=0 Set a fixed padding to labels
  51. Args:
  52. <query> eg '{foo="bar",baz="blip"}'
  53. [<regex>]