How to Configure Daily Log Rotation for Apache on Windows Using rotatelogs


2 views

When running Apache 2.2.9 (Win32) with multiple virtual hosts, managing growing log files becomes crucial. The default configuration where each virtual host writes to dedicated [domain-name]-error.log and [domain-name]-access.log files can quickly consume disk space.

Apache provides the rotatelogs utility that solves this problem elegantly. Unlike Linux solutions requiring cron jobs, this works natively on Windows installations.


# Example configuration for virtual hosts
ErrorLog "|bin/rotatelogs logs/example-error.%Y-%m-%d.log 86400"
CustomLog "|bin/rotatelogs logs/example-access.%Y-%m-%d.log 86400" common

The rotatelogs utility offers several rotation strategies:


# Time-based rotation (daily)
ErrorLog "|bin/rotatelogs logs/error.%Y-%m-%d.log 86400"

# Size-based rotation (5MB)
CustomLog "|bin/rotatelogs logs/access.%Y-%m-%d.log 5M" common

# Combined time and size
ErrorLog "|bin/rotatelogs logs/error.%Y-%m-%d-%H_%M_%S.log 86400 5M"

You can customize the log filename format using strftime parameters:

  • %Y - Full year (e.g., 2023)
  • %m - Month (01-12)
  • %d - Day of month (01-31)
  • %H - Hour (00-23)
  • %M - Minute (00-59)
  • %S - Second (00-59)

Here's a complete virtual host configuration demonstrating daily rotation:


<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "C:/websites/example"
    
    # Error log - rotates daily at midnight
    ErrorLog "|C:/Apache2.2/bin/rotatelogs.exe C:/Apache2.2/logs/example-error.%Y-%m-%d.log 86400"
    
    # Access log - rotates daily and maintains common format
    CustomLog "|C:/Apache2.2/bin/rotatelogs.exe C:/Apache2.2/logs/example-access.%Y-%m-%d.log 86400" common
    
    # Additional directives...
</VirtualHost>

If rotatelogs isn't working as expected:

  1. Verify the full path to rotatelogs.exe is correct
  2. Ensure Apache has write permissions to the log directory
  3. Check for spaces in paths (use quotes if present)
  4. Test rotation parameters independently (time vs size)

For more control, you can combine rotatelogs with Windows Task Scheduler:


# Batch script to force log rotation
@echo off
net stop Apache2.2
move C:\Apache2.2\logs\*.log C:\Apache2.2\logs\archive\
net start Apache2.2

Schedule this to run daily using Windows Task Scheduler for predictable rotation times.


When running Apache 2.x on Windows with multiple virtual hosts, log files can quickly become unmanageable. Each virtual host typically generates its own access and error logs, leading to massive files that are difficult to analyze and maintain. Unlike IIS which has built-in log rotation features, Apache requires manual configuration for proper log management.

The most efficient way to handle log rotation on Windows is through Apache's rotatelogs utility. This piped logging program comes bundled with Apache and can be configured directly in your httpd-vhosts.conf file:


# For error logs
ErrorLog "|bin/rotatelogs.exe logs/example-error.%Y-%m-%d.log 86400"

# For access logs  
CustomLog "|bin/rotatelogs.exe logs/example-access.%Y-%m-%d.log 86400" common

You can choose between time-based or size-based rotation:

Daily Rotation (Time-based)


ErrorLog "|bin/rotatelogs.exe logs/%Y%m%d-error.log 86400"

Where 86400 seconds = 1 day. The log will rotate at midnight each day.

Size-based Rotation


CustomLog "|bin/rotatelogs.exe logs/access-%Y%m%d.log 5M" combined

This creates a new log file when current reaches 5MB.

For multiple virtual hosts, you can use environment variables to make the configuration more maintainable:


<VirtualHost *:80>
    ServerName example.com
    SetEnv VHOST_NAME "example"
    ErrorLog "|bin/rotatelogs.exe logs/%{VHOST_NAME}e-error-%Y%m%d.log 86400"
    CustomLog "|bin/rotatelogs.exe logs/%{VHOST_NAME}e-access-%Y%m%d.log 86400" common
</VirtualHost>

Remember these important Windows-specific notes:

  • Always use rotatelogs.exe (not just rotatelogs)
  • Paths should use forward slashes or escaped backslashes
  • The rotatelogs.exe must be in your system PATH or use full path
  • Consider creating a batch file wrapper for more complex rotations

For more advanced log rotation needs, consider:

  1. Windows Task Scheduler with log archiving scripts
  2. Third-party tools like LogRotateWin
  3. Apache modules like mod_log_rotate (requires compilation)