Websites and web tools
Logging: what to automate and how
Logs record what a website or server did, request by request and error by error. Left alone they fill disks and bury the one line that explains an outage. This page covers the parts of logging a small team should automate, using common Linux tools, with sizing math to plan disk space.
Automate these four jobs
| Job | Why | Typical tool |
|---|---|---|
| Rotation | Stops one file growing forever | logrotate, or the journal's own limits |
| Compression | Old logs shrink a lot because text repeats | gzip, run by logrotate |
| Retention | Deletes logs past the period you need | logrotate rotate count, journald MaxRetentionSec |
| Alerting | Turns a pattern into a message | A monitoring service or a small script on a schedule |
A basic logrotate rule
On many Linux servers, a file in /etc/logrotate.d/ controls rotation for one application. A typical rule rotates daily, keeps a set number of old files, compresses them, and skips missing or empty logs. Directives you will see are daily, rotate 14, compress, delaycompress, missingok and notifempty. Test a new rule with logrotate -d for a dry run before relying on it.
Worked example: how much disk you need
Suppose a booking site serves 20,000 requests a day and each access log line is about 300 bytes.
- Raw log per day: 20,000 x 300 = about 6 MB
- Keeping 14 days uncompressed: about 84 MB
- If compression shrinks old files to about 10% of their size, 14 days take about 14 MB, counting today's file uncompressed
Compression ratios depend on the content, so measure yours with ls -lh after a week. The point of the math is to set retention on purpose instead of discovering a full disk.
Make logs searchable
Structured logs write fields such as time, level, request path, status code and duration in a consistent format, often JSON. That lets you filter for every 500 error on the booking page in one command instead of reading by eye. Always include a timestamp with time zone, and log in UTC if servers sit in different places.
Alerts people will read
- Alert on symptoms customers feel: the site is down, checkout or booking errors spike, pages slow sharply.
- Set thresholds from a normal week, not from guesses.
- Send each alert to one named owner.
- Review alerts monthly and delete any that nobody acted on.
What not to log
- Passwords, full card numbers and authentication tokens.
- More personal data than you need. Mask phone numbers and emails where possible.
- Debug-level detail in production for weeks after a fix.
A weekly five-minute review
Automation handles the routine, but a person should still glance at the logs once a week. Look at the count of server errors, the slowest pages, and any new user agents hitting the site hard. A sudden rise in 404 responses often means a broken internal link or a removed page that still gets traffic and needs a redirect.