Writing to Apache access_log file with php

孤街浪徒 提交于 2019-11-28 07:41:54

问题


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()?


回答1:


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. Your programs which parse the access log can then read the new logging parameters as well.




回答2:


is it ok to write to access_log directly

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.

or is there an easy way to achieve the same effect using php

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.




回答3:


<?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.

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.



来源:https://stackoverflow.com/questions/4470716/writing-to-apache-access-log-file-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!