使用 php 写入 Apache access_log 文件
我需要将统计数据写入实时 Apache access_log 文件(我有另一个进程对 access_log 文件中的特定行进行计数,定期向另一个进程报告)。
目前,我只是通过在 php 中执行以下操作来强制在 access_log 文件中输入一个条目:
file("http://127.0.0.1/logme.php?stuff_that_I_can_watch_here");
logme.php 不执行任何操作,并返回空并成功 200。
上述技术的问题在于,对于对 Apache 服务器的每个请求,都会生成另一个请求来写入日志 - 因此所需的 apache 服务器数量会增加一倍。
当服务器堆积时,对 Apache 服务器的简单且通常快速的本地调用需要 5 秒以上。
我可以直接写入 access_log 文件而不引起问题,或者甚至有没有办法使用类似于 syslog() 或 error_log() 的 php 写入 apache_log 文件?
I need to write statistical data to the live Apache access_log file (I have another process counting specific lines in the access_log file that reports periodically back to another process).
Currently I am simply forcing an entry into the access_log file by doing the following in php:
file("http://127.0.0.1/logme.php?stuff_that_I_can_watch_here");
logme.php does nothing and returns empty with a 200 success.
The problem with the above technique is that for every request to the Apache server, another is spawned to write to the log - hence doubling required apache servers.
When the servers pile up, the simple and usually fast local call to the Apache server takes over 5 seconds.
Can I write to the access_log file directly without causing problems, or maybe even is there a way to write to the apache_log file using php similar to syslog() or error_log()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
apache_note
(http://php.net/apache_note) 来编写您的值到注释,然后将CustomLog
与LogFormat
(%{NOTE_NAME}n
) (http://httpd.apache.org/docs/2.2/mod/mod_log_config.html)来记录新密钥。然后,解析访问日志的程序也可以读取新的日志记录参数。You can use
apache_note
(http://php.net/apache_note) to write your values to a note and then useCustomLog
withLogFormat
(%{NOTE_NAME}n
) (http://httpd.apache.org/docs/2.2/mod/mod_log_config.html) to log the new keys. Your programs which parse the access log can then read the new logging parameters as well.您可以直接写入access_log,但这样做是不行的。
正在运行的 apache 可以生成多个进程,
使用 PHP 等较慢的进程锁定文件写入只会进一步延迟日志记录速度。
不要使用 PHP,添加 附加自定义日志(如果请求完全满足您的要求)。
这是更正确的方法,此自定义日志应包含较少的行,例如不记录静态文件访问。这直接提高了后面日志的解析。
You can write directly to access_log, but is NOT OK to do this.
A running apache can spawn multiple processes,
and lock file for write using slower process like PHP is just further delay logging speed.
Do not use PHP, add an additional custom log if a request full-fill your requirement.
This is more proper way and this custom log should contains lesser line, like static file access is not logged. Which directly improve parsing of the log later.
其他人已经对该设计发表了评论。 Apache access_log 用于 Apache 记录访问,期间。
我在一个应用程序中使用了大约十几个自定义日志文件,用于所有不同的监控、性能分析和取证目的。每个用途一个日志文件使日志分析更加容易。
Others have already commented about the design. The Apache access_log is for Apache to log accesses, period.
I uses about a dozen custom log files in one app for all the different monitoring, performance analysis and forensic purposes. One log file per purpose makes log analysis easier.