Writing to apache access_log file with php

I need to write statistics to a live Apache access_log file (I have another process that counts specific lines in the access_log file, which periodically sends a report to another process).

Currently, I'm just forcing a write to 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 success of 200.

The problem with the above technique is that for each request to the Apache server, another one is generated for writing to the log - therefore, to double the required Apache servers.

When servers accumulate, a simple and usually quick local call to the Apache server takes more than 5 seconds.

Can I write directly to the access_log file without causing problems, or maybe there is even a way to write the apache_log file using php similar to syslog () or error_log ()?

+4
source share
3 answers

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 write new keys. Then your programs that analyze the access log can also read the new logging options.

+5
source

is it ok to write directly to access_log

You can write directly to access_log, but SHOULD NOT do this.
Managed apache can spawn multiple processes,
and a write-lock file using a slower process like PHP is just further logging speed.

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

Do not use PHP, add an additional user log if the request completely fills your requirement.
This is a more correct way, and this user log should contain a smaller line, for example, static access to files is not logged. Which will directly improve log analysis later.

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

Others have already commented on the design. Apache access_log is for Apache to access the log, period.

I use about a dozen custom log files in one application for all different analyzes, performance analysis and criminalization. One log file for each purpose facilitates log analysis.

0
source

Source: https://habr.com/ru/post/1332499/


All Articles