log rotation (rotatelogs)

最後更新: 2020-06-16

目錄

  • Win32 上最簡單的logrotate方式
  • Program - rotatelogs
  • Log Path
  • Vhost Disable Log
  • Access Requests Per X
  • Download log

 


Win32 上最簡單的logrotate方式

 

logrotate.bat

net stop Apache2

:: 用了 shell 的 vars

move D:\Apache2\logs\access.log D:\Apache2\logs\access%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%.log
move D:\Apache2\logs\error.log D:\Apache2\logs\error%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%.log

net start Apache2

 


Program - rotatelogs

 

  • Piped logging program
  • time interval / maximum size

win32:

ErrorLog "| D:/Apache2/bin/rotatelogs.exe D:/Apache2/logs/error_%y%m%d.log 86400"
CustomLog "| D:/Apache2/bin/rotatelogs.exe D:/Apache2/logs/access_%y%m%d.log 86400" combined

linux:

rotatelogs [ -l ] logfile  < rotationtime|filesizeM >

-l          local time

logfile 的 format:

/var/logs/logfile.%Y.%m.%d         <-- yyyy.mm.dd

    ErrorLog "| /usr/sbin/rotatelogs -l /var/log/apache2/datahunter.org/error.log.%Y.%m.%d 86400"
    LogLevel warn
    Customlog "| /usr/sbin/rotatelogs -l /var/log/apache2/datahunter.org/access.log.%Y.%m.%d 5M" combined

Remark:

Use "||" instead of "|" to spawn without invoking a shell(usually with /bin/sh -c)

 


Log Path

 

Syntax: ErrorLog file-path | syslog[:facility]

Context: server config, virtual host

Module: core

Example

ErrorLog /var/log/httpd/error_log
ErrorLog syslog:user
ErrorLog "|/usr/local/bin/httpd_errors"

 


Vhost Disable Log

 

If you don't specify a log on vhosts, Apache will log to Global log file

( /var/log/httpd/access_log, /var/log/httpd/error_log )

#ErrorLog "logs/error.log"
#CustomLog "logs/access.log" common

# Disable log

ErrorLog  /dev/null
CustomLog /dev/null combined

 


Access Requests Per X

 

Requests per second

grep '20/Jul/2015:' access_log | awk {'print $4'} | cut -d: -f1-3|sort|uniq -c

"awk {'print $4'}" result

[06/11/2014:00:17:43

"cut -d: -f1-3" result

[06/11/2014:00:17

 


Download log

 

Firefox

# 取消 / 成功下載才會寫 log, 下載中是沒有 log 的, 而且每次都係 Full Size !!

192.168.88.177 - - [22/Jul/2015:13:25:36 +0800] "GET /test.bin HTTP/1.1" 200 1073741824 "-" "Mozilla/5.0 
  (Windows NT 6.1; WOW64; rv:39.0) Gecko/20100101 Firefox/39.0"

 

 


LogLevel

 

# Default: warn
# emerg > alert > crit > error > warn

LogLevel warn

 


many 404 error log

 

Err 1:

[Thu May 08 16:56:10 2014] [error] [client x.x.x.x] File does not exist: /var/www/html/somefile.html

方案 1: 404 handler

# 它是相對於 DocumentRoot 的 !!

Setting

ErrorDocument 404 /404.html

唔 work, 只是 display 唔同左, 仍是有 log 的.

 

 

Creative Commons license icon Creative Commons license icon