Skip to content

Nicholas Duffy

Count Filtered Stackdriver Log Lines from the Command Line

gcp, stackdriver, gcloud, logging, jq1 min read

You can easily get log lines from Stackdriver with the gcloud CLI. Tack on jq to count the lines for a quick way to get a sense of log volume.

In this example, we're counting lines ingested from a Kubernetes cluster, but you can query whatever types of lines you like, but watch out for the timestamp! If you have a large volume of logs, this operation could take a long time.

1gcloud logging read 'resource.type=container AND
2 resource.labels.cluster_name=my-cluster AND
3 resource.labels.namespace_id=default AND
4 logName=projects/my-project/logs/my-log-group AND
5 textPayload:"your operation failed" AND
6 timestamp>="2020-07-01T00:00:00Z"' --format=json | jq '. | length'