Skip to content

DevOps Operations / Service monitoring · Step 2 of 7

Lesson: Service LogsSwitch
← DevOps Operations

Delivery and containers

Service Logs

Read and filter the logs a service writes so you can see what it did.

A log is a service's diary. Metrics tell you that errors went up; logs usually tell you which requests failed and what the program said about it.

Where logs live

On systems running systemd, the journal collects messages from services, including what they write to standard output and standard error. Read one unit with journalctl -u:

bash
$ sudo journalctl -u nginx -n 2
Mar 04 09:12:01 web1 systemd[1]: Starting nginx.service...
Mar 04 09:12:01 web1 systemd[1]: Started nginx.service.

Some programs also write their own files, often under /var/log. nginx, for example, normally writes access.log and error.log in /var/log/nginx/ when installed from distribution packages. Check the program's configuration to be sure.

Narrow the search

Filter by time and importance instead of scrolling:

bash
sudo journalctl -u nginx --since "1 hour ago"
sudo journalctl -u nginx -p err
sudo journalctl -u nginx -f

-p err shows messages of priority err and the more severe levels above it. -f follows the log and prints new entries as they arrive. For plain files, use tail -f and grep:

bash
$ sudo grep -c ' 500 ' /var/log/nginx/access.log
7

That counts matching lines. It is a quick estimate, because the same text could appear somewhere other than the status field.

Read a log line well

Look for the time, the host, the program with its process ID, and the message. Find the first error in a burst. Later lines are often consequences of it. Check the time zone before comparing logs from different machines.

Rotation and retention

Logs grow without limit unless something trims them. logrotate commonly renames and compresses old files, so yesterday's entries may be in access.log.1 or a .gz file, which zcat and zgrep can read.

The journal may be stored only in memory on some systems, which means it is lost at reboot. With the Storage=auto setting, entries are kept on disk only if /var/log/journal exists. The default setting differs between systemd versions and distributions.

Treat logs as sensitive. They can contain addresses and usernames, so limit who can read them and never log passwords.

Try it

Run journalctl --since today -p warning on a test machine. Pick one message, then show only that unit's entries with -u. Practise filtering text in the streams and text lab.

Check your understanding

Service Logs

All 3 sections · no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.