Writing to Apache access_log file with php
Asked Answered
L

3

9

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

Leeke answered 17/12, 2010 at 13:8 Comment(7)
This smells of bad design. Why do you want to log things in the access log that aren't really access? Wouldn't you be better off using a dedicated log file for these things?Entirety
Originally stats were gathered from the Apache log file in near real-time. As our projects grew more stats were needed and on a conditional level. For example, although a page loading would place a simple entry into the access_log as usual, on occasions things would happen under certain circumstances and only then would this need to be logged. There's a process that "listens" to the access_log file for signals and this info is sent out to external servers. It could get very messy having multiple log files to process.Leeke
@Chris this may be possible somehow - if PHP is running as an Apache module, it seems to be possible to log errors into error_log for example - but is using a separate log file totally out of the question? Sounds like that would be much easier. (Update: Ah, you extended your comment answering my question)Fraught
I suppose I am complicating things by explaining the circumstances. The question really is "is it ok to write to access_log directly - or is there an easy way to achieve the same effect using php"?Leeke
Fair enough. Hmm, maybe virtual() might work faster than an explicit request? php.net/manual/en/function.virtual.php I have no idea whether those calls get logged thoughFraught
For now I have come up with a solution. I am now writing to a second file and have our proprietary "listening" system listen to TWO files instead of the one. It works and may be the final solution - but it was good to put this out there!Leeke
There are also other methods if you only want a single variable - like a username - in the Apache log.Unchancy
A
9

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.

Ambience answered 27/8, 2012 at 16:13 Comment(0)
U
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.

Usually answered 17/12, 2010 at 14:2 Comment(2)
We are not filtering specific page requests, rather inserting additional stats depending upon circumstances. I.e. one page request could result in multiple things we need to see.Leeke
there is an apache_note function in php, does this help?Usually
U
1
<?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.

Urien answered 17/12, 2010 at 13:44 Comment(4)
The problem is not splitting apache logs rather being able to "add" stuff the the access_log file that gets picked up via our proprietary listening software.Leeke
For example, clicking on "index.html" will result in access_log getting another line. But on some ocassions I need to write MORE information to the log file, such as "dummy.php?user_is_chris" - so one request results in two lines appearing in access_log. Our "listening" system counts statistics on the fly end reports to an external server every 60 seconds.Leeke
For example, I could CURL a local image and pass paraemeters this way - but my problem isn't this - my problem is that calling the local server within a request doubles up the open requests. By writing additional information directly to the access_log it prevents the waste of a waiting apache server handler.Leeke
Sometimes you just have to do what you have to do: fwrite() to access_log.Urien

© 2022 - 2024 — McMap. All rights reserved.