Sub topic 1
Where Logs Come From: journald, syslog and files
Prerequisites
You should be comfortable with the Linux command line, editing files with sudo, and basic systemd use (systemctl and unit status). Familiarity with /var/log helps.
Where logs come from
Linux programs record messages in three places. The systemd journal is a structured, indexed store managed by systemd-journald. Syslog is a message format and routing service, usually rsyslog on modern distributions, that writes messages to files. Many applications also write plain text files directly under /var/log.
Facilities and severities
Each syslog message carries a facility, which names the subsystem that sent it (for example auth, cron or kern), and a severity from 0 to 7. rsyslog rules select messages by these two values.
| Severity | Number | Meaning |
|---|---|---|
| emerg | 0 | System unusable |
| alert | 1 | Immediate action needed |
| crit | 2 | Critical condition |
| err | 3 | Error |
| warning | 4 | Warning |
| notice | 5 | Normal but significant event |
| info | 6 | Informational message |
| debug | 7 | Debugging detail |
The systemd journal
The journal stores entries with metadata such as the unit name and process ID. Read it with journalctl:
journalctl -u nginx.service --since "1 hour ago"With the default Storage=auto setting, journald writes to /var/log/journal when that directory exists. Otherwise it keeps entries in memory under /run, which is lost at reboot.
Why logs must be rotated
Log files grow without limit. A busy server can fill its disk, and then services fail to write, databases stop, and the system can become unstable. Rotation renames the current file, starts a fresh one, and compresses or deletes older copies.
Writing logrotate configurations
logrotate runs once a day from a systemd timer or cron job and reads each file in /etc/logrotate.d/. This example rotates an application's logs:
# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 myapp adm
sharedscripts
postrotate
/usr/bin/systemctl kill -s HUP myapp.service
endscript
}dailyandrotate 14rotate once a day and keep 14 old copies.compressanddelaycompressgzip old copies, but leave the newest one uncompressed for one cycle.missingokandnotifemptyskip missing files and empty files without raising an error.create 0640 myapp admsets the mode, owner and group of the new file.postrotateruns a command after rotation. Here,HUPtells the application to reopen its log file.
Check a configuration with a dry run before relying on it: sudo logrotate -d /etc/logrotate.d/myapp.
Exercise
Exercise
Why does the example use delaycompress?
Show answer
It leaves the newest rotated file uncompressed for one more cycle, so a process that still has that file open can finish writing before the file is compressed.