Writing to Apache access_log file with php

后端 未结 3 1636
失恋的感觉
失恋的感觉 2020-12-20 16:57

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

相关标签:
3条回答
  • 2020-12-20 17:26

    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.

    0 讨论(0)
  • 2020-12-20 17:45
    <?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.

    0 讨论(0)
  • 2020-12-20 17:46

    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.

    0 讨论(0)
提交回复
热议问题