What is Log Rotation? How Does it Work?

Ask about this page
Perplexity
Grok

As a system writes information to log files, the log file grows in size. We use log rotation to avoid large files that could create issues when opening them, which transfers log events to a new log file without interrupting the logging process. In many cases, the process involves renaming the current file to something new and then naming the new file the first file’s name. This process might seem simple, but it’s much more complicated than it sounds. The system responsible for logging must make the file change quickly without interruption.

For teams engaged in full stack monitoring, properly configured log rotation ensures critical logs don’t consume disk space or degrade performance across your observability pipeline.

How do Rotated Log Files Look?

“Log rotation” as a term is usually for Linux-based systems, but log rotation can happen in any environment. We’ll use the Linux log rotation to illustrate how it works. Linux has a directory named /var/log/ where we store logged events about the system. To see a list of log files in this directory, we can execute the following command:

ls /var/log

This command will show a list of logs in the log directory. You’ll notice several names of logs, some of them with the same names but with appended data values, and some with names and appended numerical values. The following is an example of what you might see in a Red Hat or CentOS environment:

cron

cron-20210807

cron-20210806

cron-20210805

Notice that the files have the same general name, but the name doesn’t have dates appended to it. . The logs with appended dates are older, already rotated log files. The file with the name “cron” and no date is the currently active file. In this example, the cron job log file is rotated every day. To can identify that it’s rotated every day because the appended date is a daily value. The old log file is given the name “cron” with the current date appended to it. After the rotated old file is stored, the new and existing cron file is created, which will contain log events for the current day.

Some logs compress to save disk space, so you might see a .gz file extension. As an example, you might see the following set of log files for a MySQL server running on the server:

mysql.log

mysql.log.1.gz

mysql.log.2.gz

mysql.log.3.gz

mysql.log.4.gz

Notice that the files have the .gz extension. This file extension indicates a compressed file. You can still access the log data within these files, but you need to extract the file content to view it. Compressed files reduce their size and save disk space, and they will take up less space when you archive or back up your logs to another location.

If you want to view the most recent logs in real-time, you can use the tail -f log command, which allows you to follow active log output even as rotation occurs.

What is The Purpose of Log Rotation?

We should log every event on a critical system. Logged events should include authentication and authorization errors, application errors, hardware failures, potential security incidents, and any other event that can prove helpful to administrators. This includes security logs, which are especially important for compliance and forensic analysis.

On a busy system, these logs can grow by terabytes per day. If a log file is already a terabyte at the end of the day, allowing it to grow another terabyte can create several issues. First, it can take up incredible amounts of disk space within only a few days. Second, opening the file can be impossible if the local device does not have the memory resources. It can also cause trouble for applications that must ingest the log data to process it and export it to a readable format.

Log rotation eliminates the main issues with large files. Administrators can rotate files at various times to keep them smaller in size and move them to another location. In many cases, administrators use log files in conjunction with analytic tools to visualize the data. The visualized data displays valuable information to analysts and administrators to take the necessary steps to remediate a specific problem. It also gives them status updates so that they know how efficiently the environment infrastructure is running.

What Happens to Old Log Files?

Although administrators rotate logs, the old files stay on the local system or a network device. When older files are no longer needed, they often still need to be stored for auditing purposes. To save on storage space, administrators take older files and archive them.

Archives are distinct from backups. When an administrator no longer needs to keep files on the server but must store them for potential audits, an archive will move the file from its current location to a new storage location. Because the cloud has virtually endless storage capacity, usually, this new location is the cloud. 

Administrators should also do backups to log files, making copies and storing them in another location. Backups recover the system should data corrupt, the system crashes, and in the aftermath of a cybersecurity event.“Log rotation” as a term is usually for Linux-based systems, but log rotation can happen in any environment. We’ll use the Linux log rotation to illustrate how it works. Linux has a directory named /var/log/ where we store logged events about the system. To see a list of log files in this directory, we can execute the following command:

Table of contents

    More articles

    How to Reduce Log Volume Without Losing Visibility
    How to Reduce Log Volume Without Losing Visibility
    Log Management
    What Is Log Rehydration? Understanding the Process and Benefits
    What Is Log Rehydration? Understanding the Process and Benefits
    Log Management
    Log Metrics: What are they? How can they be used? What insights can be garnered at scale?
    Log Metrics: What are they? How can they be used? What insights can be garnered at scale?
    Log Management
    What is MultiCloud Monitoring & Management?
    What is MultiCloud Monitoring & Management?
    Log Management
    Live Tail: What It Is, Why It’s Useful, How To Use It
    Live Tail: What It Is, Why It’s Useful, How To Use It
    Log Management
    Log Data: What it is and why it matters
    Log Data: What it is and why it matters
    Log Management
    Istio Logging 101
    Istio Logging 101
    Log Management
    How to Use JSON Logs
    How to Use JSON Logs
    Log Management
    Understanding and Leveraging AWS Cloudwatch Logs
    Understanding and Leveraging AWS Cloudwatch Logs
    Log Management
    What Is a Tail Log?
    What Is a Tail Log?
    Log Management
    How to Use S3 Access Logs
    How to Use S3 Access Logs
    Log Management
    Benefits of Data Logging
    Benefits of Data Logging
    Log Management
    What is Real-time Log Monitoring?
    What is Real-time Log Monitoring?
    Log Management
    Managing Digital Compliance During Digital Transformation
    Managing Digital Compliance During Digital Transformation
    Log Management
    A Comprehensive Guide to Kubernetes Monitoring Tools
    A Comprehensive Guide to Kubernetes Monitoring Tools
    Log Management
    The Role of Infrastructure Monitoring in DevOps
    The Role of Infrastructure Monitoring in DevOps
    Log Management
    5 Practical Ways to Build a Secure CI/CD Pipeline
    5 Practical Ways to Build a Secure CI/CD Pipeline
    Log Management
    Why and How to Analyze Deployment Health Through CI/CD Logs
    Why and How to Analyze Deployment Health Through CI/CD Logs
    Log Management
    What is Application Lifecycle Management
    What is Application Lifecycle Management
    Log Management
    What Is A Real-Time Dashboard?
    What Is A Real-Time Dashboard?
    Log Management
    Log Indexing and Rotation for Optimized Archival
    Log Indexing and Rotation for Optimized Archival
    Log Management
    Logging for Application Security
    Logging for Application Security
    Log Management
    How Do You Manage Logs?
    How Do You Manage Logs?
    Log Management
    How Custom Parsing Can Boost Developer Productivity
    How Custom Parsing Can Boost Developer Productivity
    Log Management
    7 Best Practices for Log Management and Analytics
    7 Best Practices for Log Management and Analytics
    Log Management
    Enhancing Communication Across Your Teams With Logging
    Enhancing Communication Across Your Teams With Logging
    Log Management
    Which Log Files Should Users Onboard to Build an Observability Platform?
    Which Log Files Should Users Onboard to Build an Observability Platform?
    Log Management
    What Information Does Log Aggregation Capture?
    What Information Does Log Aggregation Capture?
    Log Management
    The Key Benefits of Log Data
    The Key Benefits of Log Data
    Log Management
    Planning Your Log Collection
    Planning Your Log Collection
    Log Management
    Capturing the Most Critical Information Within Your Logs
    Capturing the Most Critical Information Within Your Logs
    Log Management
    The Importance of Data Privacy and Confidentiality for Log Management
    The Importance of Data Privacy and Confidentiality for Log Management
    Log Management
    SOC 2 and its Benefits
    SOC 2 and its Benefits
    Log Management
    Logging for Microservices
    Logging for Microservices
    Log Management
    System Logging Best Practices
    System Logging Best Practices
    Log Management
    Why HIPAA and Compliance Matters to Logging
    Why HIPAA and Compliance Matters to Logging
    Log Management
    Application Security and Compliance through Logging
    Application Security and Compliance through Logging
    Log Management
    Log Management Compliance for Saas Applications
    Log Management Compliance for Saas Applications
    Log Management
    What is Log Aggregation? Log Aggregation Explained
    What is Log Aggregation? Log Aggregation Explained
    Log Management
    What is Log Analysis?
    What is Log Analysis?
    Log Management
    What to Look for in a HIPAA-Compliant Log Management Tool
    What to Look for in a HIPAA-Compliant Log Management Tool
    Log Management
    What is Structured Logging?
    What is Structured Logging?
    Log Management
    Why Is Log Management Important?
    Why Is Log Management Important?
    Log Management
    Top Use Cases for Log Analysis
    Top Use Cases for Log Analysis
    Log Management