Mastering Log Rotation on Ubuntu: Keeping Your Logs Under Control

Mastering Log Rotation on Ubuntu: Keeping Your Logs Under Control

Let’s say you wake up one morning, check on your servers, and suddenly have a panic attack. Your services have crashed, and everything feels unstable. You look everywhere trying to figure out what went wrong. Then, you notice something: your disk space is almost full, and a massive 100GB log file sitting there, slowly eating up all your resources.

This is exactly why log rotation is so important.

If you're running any kind of system or application on Linux, you’ve probably had to deal with log files. They give you an insight into everything happening on the system, helping you monitor performance, spot errors, and troubleshoot issues when things go wrong. But log files can grow fast. they can fill up your disk and make finding useful information a nightmare.

That’s where log rotation comes in. It’s a simple concept. I’ll walk you through how log rotation works on Linux Ubuntu and why it’s something you’ll want to set up if you haven’t already.

What is Log Rotation precisely?

Log rotation is the process of archiving your old log files and starting fresh with new ones. Instead of letting one file grow, the system will rotate the logs based on file size or time so they don’t get out of hand.

In Ubuntu, this is handled by a tool called logrotate. It’s a powerful little tool that runs automatically and ensures your log files don’t clog up your disk space or slow down your system.

Let’s straight dive into a simple example:

Say you have an application that generates a lot of logs, and you want to make sure the logs don’t get out of control. Here’s a simple example of how you’d configure log rotation for your application:

  1. Create a new file in /etc/logrotate.d/ for your app’s logs. Let’s call it abdullahApp.
  2. Inside that file, you’d write something like this:

/var/log/abdullahApp/*.log {
    daily             # Rotate logs every day
    missingok         # Ignore if the log file is missing
    rotate 7          # Keep 7 days’ worth of logs
    compress          # Compress old logs to save space
    delaycompress     # Don’t compress the most recent rotated log
    notifempty        # Don’t rotate if the log file is empty
    create 640 root adm # Set permissions on the new log file
    postrotate        # Commands to run after rotation
    systemctl reload abdullahApp  # Restart the app after log rotation
    endscript
}        

The comments above are easily understandable what the specific line does. This config tells Ubuntu to rotate your app’s logs every day, keeping the last 7 days, compressing older logs, and reloading your app after each rotation.


Log rotation might be a small thing, but it can have a huge impact on your system’s performance and stability. Whether you're managing a large-scale production environment or just running a few personal projects on your Ubuntu machine, having a solid log rotation strategy in place is key.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了