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

7
  • 2
    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? Commented Dec 17, 2010 at 13:14
  • 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. Commented Dec 17, 2010 at 13:18
  • @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) Commented Dec 17, 2010 at 13:19
  • 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"? Commented Dec 17, 2010 at 13:20
  • 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 though Commented Dec 17, 2010 at 13:28

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
there is an apache_note function in php, does this help?
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.

4 Comments

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.
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.
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.
Sometimes you just have to do what you have to do: fwrite() to access_log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.