diff --git a/cmd/logcli/main.go b/cmd/logcli/main.go index 7dab2540f1017..869449a363189 100644 --- a/cmd/logcli/main.go +++ b/cmd/logcli/main.go @@ -20,20 +20,55 @@ import ( var ( app = kingpin.New("logcli", "A command-line for loki.").Version(version.Print("logcli")) - quiet = app.Flag("quiet", "suppress everything but log lines").Default("false").Short('q').Bool() + quiet = app.Flag("quiet", "suppress query metadata").Default("false").Short('q').Bool() statistics = app.Flag("stats", "show query statistics").Default("false").Bool() - outputMode = app.Flag("output", "specify output mode [default, raw, jsonl]").Default("default").Short('o').Enum("default", "raw", "jsonl") + outputMode = app.Flag("output", "specify output mode [default, raw, jsonl]. raw suppresses log labels and timestamp.").Default("default").Short('o').Enum("default", "raw", "jsonl") timezone = app.Flag("timezone", "Specify the timezone to use when formatting output timestamps [Local, UTC]").Default("Local").Short('z').Enum("Local", "UTC") queryClient = newQueryClient(app) - queryCmd = app.Command("query", "Run a LogQL query.") + queryCmd = app.Command("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. + +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, like 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.`) rangeQuery = newQuery(false, queryCmd) tail = queryCmd.Flag("tail", "Tail the logs").Short('t').Default("false").Bool() delayFor = queryCmd.Flag("delay-for", "Delay in tailing by number of seconds to accumulate logs for re-ordering").Default("0").Int() - instantQueryCmd = app.Command("instant-query", "Run an instant LogQL query") - instantQuery = newQuery(true, instantQueryCmd) + instantQueryCmd = app.Command("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://github.com/grafana/loki/blob/master/docs/logql.md`) + instantQuery = newQuery(true, instantQueryCmd) labelsCmd = app.Command("labels", "Find values for a given label.") labelName = labelsCmd.Arg("label", "The name of the label.").HintAction(hintActionLabelNames).String() @@ -118,7 +153,7 @@ func newQueryClient(app *kingpin.Application) *client.Client { app.Flag("tls-skip-verify", "Server certificate TLS skip verify.").Default("false").BoolVar(&client.TLSConfig.InsecureSkipVerify) app.Flag("cert", "Path to the client certificate. Can also be set using LOKI_CLIENT_CERT_PATH env var.").Default("").Envar("LOKI_CLIENT_CERT_PATH").StringVar(&client.TLSConfig.CertFile) app.Flag("key", "Path to the client certificate key. Can also be set using LOKI_CLIENT_KEY_PATH env var.").Default("").Envar("LOKI_CLIENT_KEY_PATH").StringVar(&client.TLSConfig.KeyFile) - app.Flag("org-id", "org ID header to be substituted for auth").StringVar(&client.OrgID) + app.Flag("org-id", "adds X-Scope-OrgID to API requests for representing tenant ID. Useful for requesting tenant data when bypassing an auth gateway.").StringVar(&client.OrgID) return client } @@ -153,11 +188,12 @@ func newQuery(instant bool, cmd *kingpin.CmdClause) *query.Query { return nil }) - cmd.Arg("query", "eg '{foo=\"bar\",baz=~\".*blip\"} |~ \".*error.*\"'").Required().StringVar(&query.QueryString) cmd.Flag("limit", "Limit on number of entries to print.").Default("30").IntVar(&query.Limit) if instant { + cmd.Arg("query", "eg 'rate({foo=\"bar\"} |~ \".*error.*\" [5m])'").Required().StringVar(&query.QueryString) cmd.Flag("now", "Time at which to execute the instant query.").StringVar(&now) } else { + cmd.Arg("query", "eg '{foo=\"bar\",baz=~\".*blip\"} |~ \".*error.*\"'").Required().StringVar(&query.QueryString) cmd.Flag("since", "Lookback window.").Default("1h").DurationVar(&since) cmd.Flag("from", "Start looking for logs at this absolute time (inclusive)").StringVar(&from) cmd.Flag("to", "Stop looking for logs at this absolute time (exclusive)").StringVar(&to) diff --git a/docs/getting-started/logcli.md b/docs/getting-started/logcli.md index a5153831c2e79..545a4d38a03d6 100644 --- a/docs/getting-started/logcli.md +++ b/docs/getting-started/logcli.md @@ -64,69 +64,158 @@ Configuration values are considered in the following order (lowest to highest): ### Details -```bash +```nohighlight $ logcli help usage: logcli [] [ ...] A command-line for loki. Flags: - --help Show context-sensitive help (also try --help-long and --help-man). - -q, --quiet suppress everything but log lines - -o, --output=default specify output mode [default, raw, jsonl] - --addr="https://logs-us-west1.grafana.net" - Server address. - --username="" Username for HTTP basic auth. - --password="" Password for HTTP basic auth. - --ca-cert="" Path to the server Certificate Authority. + --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] + --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. - --key="" Path to the client certificate key. - --org-id=ORG-ID org ID header to be substituted for auth + --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=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 [] [] + 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. + + 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, like 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://github.com/grafana/loki/blob/master/docs/logql.md + labels [