๐Ÿš€ Day 14: Effective Log Management and Rotation Strategies

Ritesh SinghRitesh Singh
2 min read

Welcome to Day 14 of my 100 Days of DevOps journey!
Today I explored log management and log rotation โ€” an essential DevOps skill for system stability, debugging, auditing, and monitoring.


๐Ÿ“‚ What Are Logs in Linux?

Logs are system-generated messages that record everything from boot information, user actions, process statuses, and service errors.

๐Ÿ—‚๏ธ Common Linux log files:

Log FilePurpose
/var/log/syslogGeneral system activity logs
/var/log/auth.logAuthentication (login, sudo) logs
/var/log/kern.logKernel-related messages
/var/log/dmesgBoot & hardware events
/var/log/cron.logScheduled job logs (if enabled)

๐Ÿ‘€ View logs using:

sudo tail -f /var/log/syslog
sudo journalctl -u ssh.service
grep "error" /var/log/auth.log

๐Ÿ” Why Log Rotation Matters

Without rotation, log files can grow indefinitely and consume disk space.
Log rotation helps by:

  • Archiving old logs

  • Compressing them to save space

  • Automatically deleting older ones

  • Keeping systems clean and readable


๐Ÿ”ง Tool: logrotate

Most Linux systems use logrotate to automate log rotation.

๐Ÿ“Œ Default config:

  • Global: /etc/logrotate.conf

  • Per-app: /etc/logrotate.d/


๐Ÿ“ My Custom Log Rotation Setup

โœ… 1. Simulate a Log File

mkdir -p ~/logs
echo "Log started at $(date)" > ~/logs/myapp.log

Then simulate appending:

nohup bash -c 'while true; do echo "$(date): app log entry" >> ~/logs/myapp.log; sleep 2; done' &

๐Ÿ“ 2. Create Logrotate Config

Create a file:

sudo nano /etc/logrotate.d/myapp

Paste:

Paste:

/home/ritesh/logs/myapp.log {
    daily
    rotate 5
    compress
    missingok
    notifempty
    create 644 ritesh ritesh
    su ritesh ritesh
}

๐Ÿ“Œ Explanation:

  • daily: rotate once per day

  • rotate 5: keep 5 old logs

  • compress: gzip old logs

  • create: re-create log after rotation

  • su: run rotation as my user


๐Ÿงช 3. Test It

๐Ÿ“Œ Dry run:

sudo logrotate -d /etc/logrotate.d/myapp

๐Ÿ“Œ Force rotation:

sudo logrotate -f /etc/logrotate.d/myapp

๐Ÿ“ Result:

ls -lh ~/logs/
# myapp.log         -> current log file
# myapp.log.1.gz    -> rotated & compressed

๐Ÿง  Extras

๐Ÿงน Clean old systemd journal logs:

sudo journalctl --vacuum-time=2weeks

๐Ÿงฐ Useful tools:


๐Ÿ“Œ Key Learnings

โœ… Understood core Linux logs and their importance
โœ… Learned how to manage logs using logrotate
โœ… Created a real-time rotating app log
โœ… Prepared for production-grade log hygiene


๐Ÿ“š References:


๐Ÿ”— Hashnode Blog: Day 14 โ€“ Log Management and Rotation
๐Ÿ“˜ GitHub Repo: DevOps Journal

0
Subscribe to my newsletter

Read articles from Ritesh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ritesh Singh
Ritesh Singh

Hi, Iโ€™m Ritesh ๐Ÿ‘‹ Iโ€™m on a mission to become a DevOps Engineer โ€” and Iโ€™m learning in public every single day.With a full-time commitment of 8โ€“10 hours daily, Iโ€™m building skills in: โœ… Linuxโœ… Git & GitHubโœ… Docker & Kubernetesโœ… AWS EC2, S3โœ… Jenkins, GitHub Actionsโœ… Terraform, Prometheus, Grafana I post daily blogs on Hashnode, push projects to GitHub, and stay active on LinkedIn and Twitter/X. Letโ€™s connect, collaborate, and grow together ๐Ÿš€ #100DaysOfDevOps #LearningInPublic #DevOps