8.5. Application Logs and Log Rotation

The journal is indexed, size-capped, and cleaned automatically - but it is not the only place records end up. Debian's default stack duplicates every journal record to rsyslog, which writes the traditional text files under /var/log, and many services keep plain files of their own beside or instead of either path. Files do not manage themselves: unchecked, an application log grows until it fills the disk that holds /var. This section traces a record through all three destinations, configures logrotate for a service that writes its own file, and sets the wire format that Section 8.6's collector depends on.

8.5.1. Journald, Syslog, and Application Log Destinations

A record written to the journal does not stay only in the journal. Debian's default logging stack forwards a copy of everything to rsyslog, which keeps the classic text files, and many applications additionally write their own files. Figure 8.3 shows the whole flow.

Where a log record travels on a Debian system

Figure 8.3. Conceptual view of logging destinations. Journald indexes every record for journalctl; rsyslog receives a duplicate through the syslog socket and writes rotating text files or forwards to a collector; applications may also write files of their own, which logrotate manages.

Trace one record through both paths. From john's shell on client1:

$ logger -t handbook-syslog "destination probe"

logger writes one syslog message and prints nothing. It landed in the journal, signed with the tag:

$ journalctl -t handbook-syslog -n 1 --no-pager
Nov 19 09:02:16 client1 handbook-syslog[66401]: destination probe

The same record also reached rsyslog's file destinations, because journald duplicates everything it receives to the /run/systemd/journal/syslog socket, from which rsyslog reads. As root:

# grep -F "handbook-syslog" /var/log/syslog | tail -1
2026-11-19T09:02:16.675598+00:00 client1 handbook-syslog: destination probe

The pipeline selects the probe's line with grep -F (fixed-string match) and keeps the last of any repeats with tail -1. Note the timestamp format: Debian 13's rsyslog writes files with full RFC 3339 precision including microseconds, not the older Nov 19 09:02:16 style - which is why the collected-log layouts in Section 8.6 can preserve sub-second ordering. The third destination is the application's own file. A second demonstration service writes structured event lines to /var/log/handbook-app/app.log and reopens the file on SIGHUP, which makes it compatible with log rotation. Save this as /opt/handbook-heartbeat/logwriter.py, next to the heartbeat:

#!/usr/bin/env python3
import signal
import time

path = "/var/log/handbook-app/app.log"
stream = open(path, "a", buffering=1)


def reopen(signum, frame):
    global stream
    stream.close()
    stream = open(path, "a", buffering=1)
    stream.write("reopened log file after rotation\n")


signal.signal(signal.SIGHUP, reopen)
count = 0
while True:
    count += 1
    stream.write(f"{time.strftime('%Y-%m-%dT%H:%M:%S%z')} "
                 f"event={count} component=logwriter\n")
    time.sleep(0.2)

And this as /etc/systemd/system/handbook-logwriter.service:

[Unit]
Description=Handbook file log writer
After=local-fs.target

[Service]
Type=simple
User=handbook-app
Group=handbook-app
LogsDirectory=handbook-app
ExecStart=/opt/handbook-heartbeat/logwriter.py
Restart=on-failure

LogsDirectory=handbook-app creates /var/log/handbook-app owned by the service account, exactly as StateDirectory did in Section 8.3.2, so the script can create its file there on first run. Make the script executable, then load and start, as root:

# chmod 755 /opt/handbook-heartbeat/logwriter.py
# systemctl daemon-reload
# systemctl start handbook-logwriter.service

The event stream is now flowing; from john's shell:

$ ls -l /var/log/handbook-app/
total 4
-rw-r--r-- 1 handbook-app handbook-app 2853 Nov 19 09:02 app.log
$ tail -3 /var/log/handbook-app/app.log
2026-11-19T09:02:32+0000 event=51 component=logwriter
2026-11-19T09:02:32+0000 event=52 component=logwriter
2026-11-19T09:02:32+0000 event=53 component=logwriter

Three destinations, three toolsets: journalctl for the journal, text tools for rsyslog's files, and whatever the application documents for its own. What file-based logs do not get for free is rotation - the next subsection provides it.

8.5.2. Configuring logrotate for File-Based Logs

logrotate runs daily from logrotate.timer, applies the policy fragments under /etc/logrotate.d/, and remembers what it has already rotated in the state file /var/lib/logrotate/status. The options used here:

Option Effect
-d, --debug Dry run: say what would be done, change nothing
-f, --force Rotate now, ignoring the time-based condition
-v, --verbose Narrate the actions actually taken
-s FILE Use a different state file

Contents of /etc/logrotate.d/handbook-app:

/var/log/handbook-app/app.log {
    size 1k
    rotate 3
    compress
    delaycompress
    missingok
    notifempty
    create 0640 handbook-app adm
    sharedscripts
    postrotate
        systemctl kill -s HUP handbook-logwriter.service
    endscript
}

Line by line: size 1k rotates once the file passes one kilobyte (the threshold is deliberately tiny for this exercise; production settings either use a larger threshold such as 100M or rotate on a daily or weekly schedule instead of size); rotate 3 keeps three old files; compress gzips rotated files, and delaycompress holds the most recent one uncompressed for one cycle; missingok tolerates the file's absence; notifempty skips empty logs; create 0640 handbook-app adm creates the new file with the service account as owner and the adm group able to read - matching the readable-by-administrators convention of /var/log/syslog. The postrotate hook is the part that makes rotation safe: after the rename, systemctl kill -s HUP delivers SIGHUP to every process in the service's cgroup, which the script above answers by reopening the file. systemctl kill targets the whole unit, unlike the ExecReload/$MAINPID pattern of Section 8.3.2, and sharedscripts runs the hook once even when several logs rotated. Preview the policy first:

# logrotate --debug /etc/logrotate.d/handbook-app
warning: logrotate in debug mode does nothing except printing debug messages!  Consider using verbose mode (-v) instead if this is not what you want.

reading config file /etc/logrotate.d/handbook-app
Reading state from file: /var/lib/logrotate/status
Allocating hash table for state file, size 64 entries
Creating new state

Handling 1 logs

rotating pattern: /var/log/handbook-app/app.log 1024 bytes empty log files are not rotated, (3 rotations), old logs are removed
considering log /var/log/handbook-app/app.log
  Now: 2026-11-19 09:02
  Last rotated at 2026-11-19 08:45
  log needs rotating
rotating log /var/log/handbook-app/app.log, log->rotateCount is 3
dateext suffix '-20261119'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
previous log /var/log/handbook-app/app.log.1 does not exist
renaming /var/log/handbook-app/app.log.3.gz to /var/log/handbook-app/app.log.4.gz (rotatecount 3, logstart 1, i 3), 
renaming /var/log/handbook-app/app.log.2.gz to /var/log/handbook-app/app.log.3.gz (rotatecount 3, logstart 1, i 2), 
renaming /var/log/handbook-app/app.log.1.gz to /var/log/handbook-app/app.log.2.gz (rotatecount 3, logstart 1, i 1), 
log /var/log/handbook-app/app.log.4.gz doesn't exist -- won't try to dispose of it
renaming /var/log/handbook-app/app.log to /var/log/handbook-app/app.log.1
creating new /var/log/handbook-app/app.log mode = 0640 uid = 999 gid = 4
running postrotate script
running script with args /var/log/handbook-app/app.log : "
        systemctl kill -s HUP handbook-logwriter.service
"

The dry run narrates the whole plan: rename .3.gz out, shift the others down, move the live file to .1, create the new empty file, and run the systemctl kill. The state file said the log was last rotated at 08:45 and has since passed the size threshold, so a real run will rotate. --debug changed nothing; force the real one:

# logrotate --force /etc/logrotate.d/handbook-app

Silent - logrotate only narrates under --debug or --verbose. The result, from john's shell:

$ ls -l /var/log/handbook-app/
total 12
-rw-r----- 1 handbook-app adm           735 Nov 19 09:02 app.log
-rw-r--r-- 1 handbook-app handbook-app 4149 Nov 19 09:02 app.log.1

The old log became app.log.1 and a fresh file appeared - created, not merely moved, which is why it carries the configured 0640 handbook-app adm identity while the renamed original keeps its old mode. The writer kept running throughout: its events continue into the new file because of the SIGHUP. Rotating again exercises the shift and the compressor:

# logrotate --force /etc/logrotate.d/handbook-app

Again silent:

$ ls -l /var/log/handbook-app/
total 12
-rw-r----- 1 handbook-app adm          1023 Nov 19 09:02 app.log
-rw-r----- 1 handbook-app adm          1386 Nov 19 09:02 app.log.1
-rw-r--r-- 1 handbook-app handbook-app  341 Nov 19 09:02 app.log.2.gz

The previous app.log moved to .1 again, and the earlier .1 was compressed into .2.gz - delaycompress held it uncompressed for one cycle, and this second rotation was that cycle's end. Read a compressed generation with zcat:

$ zcat /var/log/handbook-app/app.log.2.gz | tail -2
2026-11-19T09:02:37+0000 event=76 component=logwriter
2026-11-19T09:02:37+0000 event=77 component=logwriter

The created-file identity, confirmed:

# stat -c "%U:%G %a %n" /var/log/handbook-app/app.log
handbook-app:adm 640 /var/log/handbook-app/app.log

One further detail of the same config explains itself if forced a third time within a quiet minute: with no new events, the new app.log is empty and notifempty skips the rotation entirely - empty logs are not worth shuffling.

8.5.3. Forwarding Logs and Preserving Useful Timestamps

The third leg of Figure 8.3 - forwarding records off the host - is where formats start to matter. Debian's rsyslog reads local messages through the socket shown in Section 8.5.1 and can send any of them onward with a forwarding action. The full TLS-protected collector build is Section 8.6; what belongs here is the one line that decides what a forwarded message looks like:

*.* action(type="omfwd" target="collector" port="6514" protocol="tcp"
    template="RSYSLOG_SyslogProtocol23Format")

*.* selects every facility and severity; omfwd is the forwarding output; and the template names the wire format. The default legacy format truncates timestamps to second precision and drops structure; RSYSLOG_SyslogProtocol23Format wraps each message in RFC 5424, which carries ISO 8601 timestamps with sub-second precision, structured-data elements, and the original hostname and tag. The difference becomes visible on the collector in Section 8.6.6, where an RFC 5424 message written by logger --rfc5424 arrives with its structured fields intact. Forwarding also duplicates every record: the journal copy stays local, the rsyslog file copy stays local, and the collector keeps a third. That redundancy is deliberate - a host that loses its network still has its own logs.