Thursday, December 22, 2016

How To Rotate Apache error_log & access_log Logs?

There are two methods available for rotating Apache Httpd Server logs.

     1) Manual Log Rotation
     2) Log Rotation Using Piped Logs OR rotatelogs Utility

Method 1)  Manual Log Rotation 


By using a graceful restart, the server can be instructed to open new log files without losing any existing or pending connections from clients. However, in order to accomplish this, the server must continue to write to the old log files while it finishes serving old requests. It is therefore necessary to wait for some time after the restart before doing any processing on the log files

a) Rename access_log & error_log

  cd /etc/httpd/logs
  mv access_log access_log.old
  mv error_log error_log.old



b) Do the graceful restart of Apache & check whether new log files error_log & access_log generated or not.

  service httpd graceful



c) Zip old log files after some time because existing instructions are writing to old files.

  sleep 600
  gzip access_log.old error_log.old




For More Information Please refer link: https://httpd.apache.org/docs/2.4/logs.html#rotation

Method 2) Log Rotation Using  rotatelogs Utility (Piped Logs Method)


Apache httpd is capable of writing error and access log files through a pipe to another process, rather than directly to a file. In order to write logs to a pipe, simply replace the filename with the pipe character "|", followed by the name of the executable which should accept log entries on its standard input. The server will start the piped-log process when the server starts, and will restart it if it crashes while the server is running.

One important use of piped logs is to allow log rotation without having to restart the server. The Apache HTTP Server includes a simple program called rotatelogs for this purpose.


Syntax:

rotatelogs [ -l ] [ -L linkname ] [ -p program ] [ -f ] [ -t ] [ -v ] [ -e ] [ -c ] [ -n number-of-files ] logfile rotationtime|filesize(B|K|M|G) [ offset ]


To know more about Options click on link https://httpd.apache.org/docs/2.4/programs/rotatelogs.html


Example 1: Below option rotates access_log every 2 minutes (you can set your own time value) & create log file in the format access_log.yyyy.mm.dd.HH.MM


CustomLog "|/usr/sbin/rotatelogs /etc/httpd/logs/access_log.%Y.%m.%d.%H.%M 120" combined






Example 2: Below option rotates error_log for every 4KB (you can set your own size value).

ErrorLog "|/usr/sbin/rotatelogs /etc/httpd/logs/error_log.%Y-%m-%d-%H_%M_%S 4K"








For More Information Please Refere below Links:
https://httpd.apache.org/docs/2.4/programs/rotatelogs.html

https://httpd.apache.org/docs/2.4/logs.html#piped


Apache Log Format Common & Combined: https://httpd.apache.org/docs/1.3/logs.html#common

No comments:

Post a Comment