简体   繁体   English

用php写入Apache access_log文件

[英]Writing to Apache access_log file with php

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). 我需要将统计数据写入实时Apache access_log文件(我有另一个进程计算access_log文件中的特定行,它定期报告回另一个进程)。

Currently I am simply forcing an entry into the access_log file by doing the following in php: 目前我只是通过在php中执行以下操作来强制进入access_log文件:

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. logme.php什么都不做,返回空200成功。

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. 上述技术的问题在于,对于Apache服务器的每个请求,都会产生另一个请求写入日志 - 因此需要加倍所需的Apache服务器。

When the servers pile up, the simple and usually fast local call to the Apache server takes over 5 seconds. 当服务器堆积起来时,对Apache服务器的简单且通常快速的本地调用需要5秒钟。

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()? 我可以直接写入access_log文件而不会导致问题,或者甚至可以使用类似于syslog()或error_log()的php写入apache_log文件吗?

You can use apache_note ( http://php.net/apache_note ) to write your values to a note and then use CustomLog with LogFormat ( %{NOTE_NAME}n ) ( http://httpd.apache.org/docs/2.2/mod/mod_log_config.html ) to log the new keys. 您可以使用apache_notehttp://php.net/apache_note )将值写入注释,然后将CustomLogLogFormat一起使用( %{NOTE_NAME}n )( http://httpd.apache.org/docs/2.2/ mod / mod_log_config.html )记录新密钥。 Your programs which parse the access log can then read the new logging parameters as well. 然后,解析访问日志的程序也可以读取新的日志记录参数。

is it ok to write to access_log directly 是否可以直接写入access_log

You can write directly to access_log, but is NOT OK to do this. 您可以直接写入access_log,但不能这样做。
A running apache can spawn multiple processes, 运行的apache可以生成多个进程,
and lock file for write using slower process like PHP is just further delay logging speed. 和像PHP一样使用较慢进程写入的锁文件只是进一步延迟了记录速度。

or is there an easy way to achieve the same effect using php 或者有一种简单的方法来使用PHP实现相同的效果

Do not use PHP, add an additional custom log if a request full-fill your requirement. 如果请求完全满足您的要求,请不要使用PHP,添加其他自定义日志
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. 这样可以直接改进日志的解析。

<?php
  $h = fopen('/path/to/access_log', 'a');
  fwrite($h, 'Message');
  fclose($h);
?>

Others have already commented about the design. 其他人已经评论过这个设计。 The Apache access_log is for Apache to log accesses, period. Apache access_log用于Apache记录访问时段。

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. 每个目的一个日志文件使日志分析更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM