http://httpd.apache.org/docs/2.2/logs.html
Log Rotation
日志轮替
On even a moderately busy server, the quantity of information stored in the log files is very large. The access log file typically grows 1 MB or more per 10,000 requests.
即使在一个适度繁忙的服务器上,存储在日志文件里的信息数量是非常大的。访问日志通常每10000次请求增长1M或更多。
It will consequently be necessary to periodically rotate the log files by moving or deleting the existing logs.
因此移动或删除已经存在的日志来周期性的轮替日志文件是很有必要的
This cannot be done while the server is running, because Apache will continue writing to the old log file as long as it holds the file open.
当server运行的时候不能轮替,因为只要apache保持文件打开就会继续写老的文件。
Instead, the server must be restarted after the log files are moved or deleted so that it will open new log files.
所以,server必须在日志文件移动或删除后重启以便server打开新的日志文件。
By using a graceful restart, the server can be instructed to open new log files without losing any existing or pending connections from clients.
使用一个优雅的重启,server能被指示打开新的日志文件而不用丢失任何来自客户端的已存在或试图访问的连接。
However, in order to accomplish this, the server must continue to write to the old log files while it finishes serving old requests.
然而,为了实现这个目标,server必须继续完成服务老的请求的同时写老的文件。
It is therefore necessary to wait for some time after the restart before doing any processing on the log files.
因些在重启之后,对日志文件做任何处理之前等待一些时间是必须的。
A typical scenario that simply rotates the logs and compresses the old logs to save space is:
简单地轮替日志并压缩老的日志来节约空间的一个典型的场景是:
mv access_log access_log.old
mv error_log error_log.old
apachectl graceful
sleep 600
gzip access_log.old error_log.old
Another way to perform log rotation is using piped logs as discussed in the next section.
另一种完成日志轮替的方法是在下一部分要讨论的管道日志。
Piped Logs
管道日志
Apache httpd is capable of writing error and access log files through a pipe to another process, rather than directly to a file.
apache httpd 有能为通过一个管道写错误与访问日志到另一个进程,而非直接至一个文件。
This capability dramatically increases the flexibility of logging, without adding code to the main server.
这个能力显著地增加了记录的灵活性,不用添加代码到主server。
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.
为了写日志给一个管道,简单地用管道符|替换文件名,跟随可执行程序名字,这个程序应该在它的标准输入上接受日志项。
Apache will start the piped-log process when the server starts, and will restart it if it crashes while the server is running. (This last feature is why we can refer to this technique as "reliable piped logging".)
当server启动时,apache将启动一个管道日志进程,并且服务器运行的同时,假如管道当机可以重启。(最后一个特性就是为什么我们能称这项技术为“可靠的管道日志”)
Piped log processes are spawned by the parent Apache httpd process, and inherit the userid of that process. This means that piped log programs usually run as root.
管道日志进程由httpd父进程派生,继承了父进程的uid。这意味着管道日志程序通常以root运行。
It is therefore very important to keep the programs simple and secure.
因此保持程序简单和安全是非常重要的。
One important use of piped logs is to allow log rotation without having to restart the server.
管道日志的一个重要用法是允许轮替而不用重启server。
The Apache HTTP Server includes a simple program called rotatelogs for this purpose. For example, to rotate the logs every 24 hours, you can use:
server包含一个简单的程序,叫做rotatelogs来达到这个目的。例如,每24小时轮替日志,你能使用:
CustomLog "|/usr/local/apache/bin/rotatelogs /var/log/access_log 60" common 这个可能不能正常使用,所以用下面的那一条,加入下面的一条指令,然后访问页面,日志就会在一分钟后替换。
Notice that quotes are used to enclose the entire command that will be called for the pipe. Although these examples are for the access log, the same technique can be used for the error log.
注意双引号被用来封闭由管道调用的整个命令。尽管这个例子被用于访问日志,同样的技术也能用于error_log。
As with conditional logging, piped logs are a very powerful tool, but they should not be used where a simpler solution like off-line post-processing is available.
By default the piped log process is spawned using a shell. (usually with /bin/sh -c). Depending on the shell specifics invocation via shell might lead to an additional shell process for the lifetime of the logging pipe program and signal handling problems during restart.
Use "||" instead of "|" to spawn without invoking a shell:
# Invoke "rotatelogs" without using a shell
CustomLog "||/usr/local/apache/bin/rotatelogs /var/log/access_log 60" common