Apache | Log Files | Log Rotation (rotatelogs)

If no separate setting is specified, logs continue to be written to one file. With additional configuration, logs can be switched to separate files at regular intervals. For example, recording logs in separate files by day makes them easier to manage.

Log rotation (rotatelogs)

Apache provides a utility program for rotating logs. Use the rotatelogs program in the {Apache installation directory}/Apache24/bin directory.

Usage is as follows.

rotatelogs target-log-file specified-time

The specified time is the interval for splitting logs, in seconds. For one day, specify 60 x 60 x 24 = 86400.

Log rotation can be configured automatically in httpd.conf. To configure it, set CustomLog as follows.

CustomLog "| rotatelogs target-log-file specified-time" log-format-name

For example, to configure daily log rotation using the common log format, write the following.

CustomLog logs/access.log common
CustomLog "|bin/rotatelogs.exe logs/access.log 86400" common

On Windows, .exe must be added after rotatelogs.

With this, whenever the specified time has elapsed while logs are being saved, another log file is created and recording continues there.

Now try it. Configure the setting above in httpd.conf, restart Apache, and access a file published by Apache.

C:\apache\Apache24\logs>dir
 C 드라이브의 볼륨에는 이름이 없습니다.
 볼륨 일련 번호: XXXX-XXXX

 C:\apache\Apache24\logs 디렉터리

2019-12-07  오전 01:40    <DIR>          .
2019-12-07  오전 01:40    <DIR>          ..
2019-12-07  오전 01:40               427 access.log
2019-12-07  오전 01:40                 0 access.log.1575590400
2019-12-07  오전 01:40               532 error.log
2019-12-07  오전 01:40                 6 httpd.pid
               4개 파일                 965 바이트
               2개 디렉터리  450,662,526,976 바이트 남음

C:\apache\Apache24\logs>

As shown above, a new file named access.log.1575590400 is created and the log is stored there. The .1575590400 suffix represents the time since January 1, 1970 in seconds.

Now advance the client’s configured date by two days and access an Apache file again.

C:\apache\Apache24\logs>dir
 C 드라이브의 볼륨에는 이름이 없습니다.
 볼륨 일련 번호: XXXX-XXXX

 C:\apache\Apache24\logs 디렉터리

2019-12-09  오전 01:51    <DIR>          .
2019-12-09  오전 01:51    <DIR>          ..
2019-12-07  오전 01:40               427 access.log
2019-12-07  오전 01:41               475 access.log.1575590400
2019-12-09  오전 01:51                 0 access.log.1575763200
2019-12-07  오전 01:40               532 error.log
2019-12-07  오전 01:40                 6 httpd.pid
               5개 파일               1,440 바이트
               2개 디렉터리  450,650,722,304 바이트 남음

C:\apache\Apache24\logs>

More than 86400 seconds have elapsed since the previous log was created, so a new log file is created and recording continues there.

Specifying the log file format

If no separate setting is specified, the time since January 1, 1970 is appended to the log file name in seconds. Because this is hard to read, you can specify the format of the appended part to distinguish logs. For example, to display the date instead of seconds, write the following.

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

Enter a period (.) after the target log file name and specify the format. In this example, the date is specified in a hyphen-separated format.

Now try it. Configure the setting above in httpd.conf, restart Apache, and access a file published by Apache.

C:\apache\Apache24\logs>dir
 C 드라이브의 볼륨에는 이름이 없습니다.
 볼륨 일련 번호: XXXX-XXXX

 C:\apache\Apache24\logs 디렉터리

2019-12-07  오전 02:05    <DIR>          .
2019-12-07  오전 02:05    <DIR>          ..
2019-12-07  오전 02:04               122 access.log
2019-12-07  오전 02:04               122 access.log.2019-12-06
2019-12-07  오전 02:05             1,287 error.log
               3개 파일               1,531 바이트
               2개 디렉터리  450,652,733,440 바이트 남음

C:\apache\Apache24\logs>

This time, the date was added in the specified format.

Reference