Skip to content

Task: Check logs

Every log command across Linux, containers, Kubernetes and cloud, in one place.

IndexUpdated

Also:logs · journalctl · tail -f · kubectl logs · docker logs · cloudwatch · stack trace

Start closest to the failure: the previous container’s logs on a crash loop, the unit’s journal for a failed service, then widen to events and cloud logs.

24 items

Linux6

Linux: Follow logs of a systemd service

Safe

Stream a service's journal, starting with the last 100 lines.

journalctl -u <service> -f -n 100
What each part does
  1. journalctlread the systemd journal
  2. -u <service>only this unit
  3. -ffollow new lines
  4. -n 100start with the last 100 lines
Example
$ journalctl -u nginx -f -n 100
Nov 20 09:12:01 web-1 nginx[812]: 10.0.3.4 - - "GET /healthz HTTP/1.1" 200

My note Add --since "10 min ago" when the service is chatty.

Also known as: journalctl · systemd logs · service logs

Tool page →

Linux: Show errors since last boot

Safe

Only priority err and worse from the current boot: fastest way to see what's wrong on a box.

journalctl -b -p err --no-pager
What each part does
  1. journalctlread the systemd journal
  2. -bthis boot only (-b -1 for the previous one)
  3. -p errpriority err and worse (crit, alert, emerg)
  4. --no-pagerprint directly

Also known as: boot errors · journalctl -p err

Tool page →

Linux: Follow a log file across rotation

Safe

Like tail -f but keeps following when logrotate replaces the file.

tail -F -n 200 <file>
What each part does
  1. tailprint the end of a file
  2. -Ffollow by name and retry: keeps going when the file is rotated or recreated (-f would stop)
  3. -n 200start with the last 200 lines
  4. <file>the log file

My note -F (capital) = --follow=name --retry. -f stops after rotation.

Also known as: tail -f · follow log

Tool page →

Linux: Search a log for an error with context

Safe

Case-insensitive search with line numbers and 3 lines of context around each hit.

grep -n -i -C 3 "<pattern>" <file>
What each part does
  1. grepsearch text
  2. -nshow line numbers
  3. -icase-insensitive
  4. -C 33 lines of context before and after each match
  5. "<pattern>" <file>what to look for, and where
Example
$ grep -n -i -C 3 "timeout" /var/log/app/app.log

Also known as: grep context · find error in log

Tool page →

Linux: Vacuum old journal logs

Caution

Delete archived journal files older than N days to reclaim disk.

sudo journalctl --vacuum-time=<days>d
What each part does
  1. sudo journalctlmanage the journal as root
  2. --vacuum-time=<days>ddelete archived journal files older than N days (or --vacuum-size=500M)

My note Check size first: journalctl --disk-usage

Also known as: journal too big · /var/log/journal

Tool page →

Linux: Delete old rotated logs

Destructive

Remove compressed rotated logs older than N days. Run without -delete first to preview.

sudo find /var/log -type f -name "*.gz" -mtime +<days> -delete
What each part does
  1. sudo find /var/logsearch under /var/log as root
  2. -type ffiles only
  3. -name "*.gz"compressed rotated logs
  4. -mtime +<days>last modified more than N days ago
  5. -deletedelete matches (run without it first to see the list)

Also known as: clean /var/log · no space left on device

Tool page →

Docker3

Docker: Follow logs of a container

Safe

Stream a container's stdout/stderr starting from the last 200 lines.

docker logs -f --tail 200 --timestamps <container>
What each part does
  1. docker logsshow a container's stdout/stderr
  2. -ffollow: keep streaming new lines
  3. --tail 200start with only the last 200 lines
  4. --timestampsprefix each line with its timestamp
  5. <container>container name or ID

Also known as: docker logs -f · container logs

Tool page →

Docker: Show container logs for a time window

Safe

Logs from the last N minutes/hours, e.g. --since 15m or --since 2h.

docker logs --since <duration> <container>
What each part does
  1. docker logsshow a container's output
  2. --since <duration>only lines newer than this: relative (30m, 2h) or a timestamp; pair with --until
  3. <container>container name or ID
Example
$ docker logs --since 15m api

Also known as: logs since

Tool page →

Docker: Follow logs of all compose services

Safe

Interleaved logs from every service in the compose project. Add a service name to narrow it.

docker compose logs -f --tail 100
What each part does
  1. docker compose logslogs of every service in the compose project, prefixed by service name
  2. -ffollow new lines
  3. --tail 100last 100 lines per service to start with

Also known as: docker-compose logs · compose logs

Tool page →

Podman1

Podman: Follow logs of a container

Safe

Stream a container's output starting from the last 200 lines.

podman logs -f --tail 200 <container>
What each part does
  1. podman logsshow a container's output
  2. -ffollow new lines
  3. --tail 200start with the last 200 lines
  4. <container>container name or ID

Also known as: podman logs

Tool page →

Kubernetes3

Kubernetes: Follow logs of a pod

Safe

Stream a pod's logs from the last 200 lines. Add -c <container> for multi-container pods.

kubectl logs -f <pod> -n <namespace> --tail=200
What each part does
  1. kubectl logsprint a container's logs
  2. -ffollow: keep streaming
  3. <pod>the pod (add -c <container> if it has several)
  4. -n <namespace>namespace of the pod
  5. --tail=200start with the last 200 lines
Example
$ kubectl logs -f api-7c9d8b6f4-x2k9p -n shop --tail=200

Also known as: k8s logs · kubectl logs -f · pod logs

Tool page →

Kubernetes: Show logs of the previous crashed container

Safe

Logs from the container instance that just died: the one with the actual error.

kubectl logs <pod> -n <namespace> --previous
What each part does
  1. kubectl logs <pod>print container logs
  2. -n <namespace>namespace of the pod
  3. --previouslogs of the last terminated instance, i.e. the one that crashed, not the restarted one

My note First thing to run on CrashLoopBackOff.

Also known as: CrashLoopBackOff · previous logs · container restarted

Tool page →

Kubernetes: List recent events sorted by time

Safe

Scheduling failures, probe failures, image pulls, evictions: newest last.

kubectl get events -n <namespace> --sort-by=.lastTimestamp
What each part does
  1. kubectl get eventslist events: scheduling, pulls, probe failures, kills
  2. -n <namespace>in this namespace
  3. --sort-by=.lastTimestampoldest first, newest at the bottom

Also known as: k8s events · Back-off restarting failed container · Readiness probe failed

Tool page →

OpenShift1

OpenShift: Follow build logs

Safe

Stream the logs of the latest build of a BuildConfig.

oc logs -f bc/<build-config>
What each part does
  1. oc logs -ffollow logs
  2. bc/<build-config>the latest build of this BuildConfig

Also known as: build logs · s2i

Tool page →

AWS1

AWS: Tail a CloudWatch log group

Safe

Stream CloudWatch Logs like tail -f. Add --filter-pattern "ERROR" to narrow.

aws logs tail <log-group> --follow --since 15m --region <region>
What each part does
  1. aws logs tailprint log events from a CloudWatch log group
  2. <log-group>the group name, e.g. /aws/lambda/orders-api
  3. --followkeep streaming new events (like tail -f)
  4. --since 15mstart 15 minutes back (s, m, h, d, w)
  5. --region <region>region of the log group, overrides the profile default
Example
$ aws logs tail /aws/lambda/orders-api --follow --since 15m --region eu-west-1

Also known as: cloudwatch logs · aws logs tail · lambda logs

Tool page →

Azure1

Azure: Stream App Service logs

Safe

Live application and web server logs from an App Service.

az webapp log tail -g <resource-group> -n <app>
What each part does
  1. az webapp log tailstream live logs from an App Service app
  2. -g <resource-group>resource group of the app
  3. -n <app>the web app name; app logging must be enabled (az webapp log config)

Also known as: webapp logs · app service logs

Tool page →

GCP1

GCP: Read recent error logs

Safe

Errors from Cloud Logging in the last hour across the project.

gcloud logging read 'severity>=ERROR' --freshness=1h --limit=50
What each part does
  1. gcloud logging readquery Cloud Logging
  2. 'severity>=ERROR'filter: ERROR, CRITICAL, ALERT, EMERGENCY
  3. --freshness=1honly entries from the last hour
  4. --limit=50return at most 50 entries

Also known as: stackdriver · cloud logging

Tool page →

Terraform1

Terraform: Enable debug logging

Safe

Write provider/API level debug logs to a file.

TF_LOG=DEBUG TF_LOG_PATH=./tf-debug.log terraform plan
What each part does
  1. TF_LOG=DEBUGlog level for this run: TRACE, DEBUG, INFO, WARN, ERROR
  2. TF_LOG_PATH=./tf-debug.logwrite the log to a file instead of stderr
  3. terraform planthe command to debug

Also known as: TF_LOG · tf debug

Tool page →

GitHub Actions1

GitHub Actions: Show logs of failed steps

Safe

Only the log output of failed steps. gh run list shows recent run ids.

gh run view <run-id> --log-failed
What each part does
  1. gh run view <run-id>show a workflow run (gh run list gives the IDs)
  2. --log-failedprint only the logs of the failed steps

Also known as: gha logs · workflow failed · gh run view

Tool page →

GitLab CI1

GitLab CI: Show a job's log

Safe

Stream the log of a CI job (pick interactively if you omit the id).

glab ci trace <job-id>
What each part does
  1. glab ci tracestream a CI job's log (follows it while running)
  2. <job-id>the job ID; leave it out to pick a job interactively

Also known as: job log · glab trace

Tool page →

Jenkins1

Jenkins: Tail a build's console log

Safe

Last 100 lines of the latest build's console without opening the UI.

curl -sS -u <user>:<api-token> "<jenkins-url>/job/<job>/lastBuild/consoleText" | tail -n 100
What each part does
  1. curl -sSquiet, but still show errors
  2. -u <user>:<api-token>basic auth with a Jenkins API token
  3. /job/<job>/lastBuild/consoleTextplain-text console output of the latest build (use a build number instead of lastBuild)
  4. | tail -n 100only the last 100 lines

Also known as: jenkins console · build log

Tool page →

Elasticsearch1

Elasticsearch: Search recent error logs

Safe

Last 20 error-level documents. Change the field to match your log schema.

curl -s "<es-url>/<index-pattern>/_search?q=level:error&size=20&sort=@timestamp:desc&pretty"
What each part does
  1. curl -sHTTP request, quiet
  2. <index-pattern>/_searchsearch across matching indices, e.g. logs-*
  3. q=level:errorLucene query string: documents where level is error
  4. size=20return 20 hits
  5. sort=@timestamp:descnewest first
  6. prettyindent the JSON

Also known as: kibana search · elk logs

Tool page →

Linkerd1

Linkerd: Tap live requests to a deployment

Safe

Stream individual requests and responses flowing through the proxies.

linkerd viz tap deploy/<deployment> -n <namespace>
What each part does
  1. linkerd viz tapstream live requests and responses passing through the proxies
  2. deploy/<deployment>the deployment to watch
  3. -n <namespace>its namespace

Also known as: linkerd tap

Tool page →

Envoy1

Envoy: Raise the log level at runtime

Caution

Turn on debug logs without a restart. Set it back to info afterwards.

curl -X POST "localhost:<admin-port>/logging?level=debug"
What each part does
  1. curl -X POSTadmin API changes need POST
  2. localhost:<admin-port>/loggingthe logging endpoint
  3. ?level=debugset every logger to debug; set it back to info when done, or use ?<logger>=debug for one

Also known as: envoy debug logs

Tool page →

My DevOps Playbook by Angelo John Calleja

New runbook

GitHub not connected

Saving is handled by the site: the GitHub token is stored in Cloudflare, not in your browser. Enter the editor password (the EDITOR_PASSWORD secret) to unlock saving in this browser.

Saving commits to gelogrammer/My_DevOps_Playbook. Create a fine-grained token with access to only this repo andContents: Read and write, then paste it here. It is stored only in this browser.

src/content/docs/…