brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: dateext puts the date in rotated log names instead of a number

Linux
~/tips/tip-logrotate-dateext-names

rotate 14

Name rotated log files after their date, so you find the right day at once.

Out of the box, rotated logs are called app.log.1, app.log.2.gz and so on, and every rotation renumbers the whole row. Looking for the log of one particular Saturday means opening files until you hit the right one.

/var/log/myapp/*.log {
    daily
    rotate 14
    compress
    dateext
    dateyesterday
}

dateext names each rotated file after the date, like app.log-20261127.gz, and a file keeps that name until it’s deleted. I like that for backups too, since yesterday’s file no longer looks changed just because it got a new number.

dateyesterday is optional, but I’d add it to daily logs. A daily run in the early morning would otherwise give the file full of Thursday’s lines Friday’s date. With it, the date in the name matches the timestamps inside.

You can pick your own format with dateformat, for example dateformat -%Y-%m-%d. Keep the year first: logrotate sorts the names to find the oldest file to delete, and a day-month-year format makes it pick the wrong ones.

The caveat: the default format only goes down to the day, so a log can rotate once a day at most. A second run on the same day finds the target name already taken and skips the rotation, which can surprise you when you combine dateext with maxsize. For hourly the default format includes the hour.

Further reading: logrotate(8) man page

More Linux tips a2query -m rewrite renice -n 10 -p 4242 cat config.yml | xsel -ib xvfb-run -a your-program --some-flag dpkg --get-selections > packages.txt All Linux tips (261) All topics

Link