Can PHP get write access to Apache's log?

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
ptgo
Forum Newbie
Posts: 2
Joined: Thu Sep 22, 2005 10:47 am

Can PHP get write access to Apache's log?

Post by ptgo »

Hi there,

I am a novice PHP programmer, and have a question on how the following task may be done with PHP/Apache.

When we fulfill a web request in PHP we need to log the response into a file for later processing. The response can be anywhere from 2k to 6k. We clocked our PHP app and it services about 60 requests per sec. I tried just creating a file for containing each response, but due to high request volume, we were generating 2,400 files in 400 seconds. This created too much load on the system, plus the resulting number of individual log files (one for each web
request) was unmanageable (not to mention using up all available iNodes or file handles).

We figured since Apache is ultra-efficient at logging (like how their access log files get sooo big yet this logging task does not noticeably drag down the server), can we in PHP somehow use Apache's logging service? It would be great if all PHP threads can just somehow pass Apache our log data and have it stash them in one big file (that rolls on some schedule). This way we avoid creating tons of little files, and our PHP threads can effectively share a log file handle without having to serialize (lock/unlock) access to the log file.

Has anyone done this or have ideas or leads?

Thanks in advance for your help!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you may be able to use the trigger_error() or error_log() systems to send messages, however sending those can often slow php down a bit..

Instead, maybe you should send the logs to a database using a persistent connection? I'd also recommend pairing down the size of entries as several kilobytes is a lot of data....
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

apache_note() + LogFormat (%{your_note_name}n)
works exactly as you described
ptgo
Forum Newbie
Posts: 2
Joined: Thu Sep 22, 2005 10:47 am

Can PHP get write access to Apache's log?

Post by ptgo »

Thanks everyone for your thoughtful responses! We did not pursue writing to the apache log but we did look at using a db (sql lite) or sleepycat, the latter being the most lightweight embedded db we can find.

But then we started wondering, maybe instead of generating one cache file for each request we can generate one file every X minutes (that contain responses for the last X minutes). Less files = easier to manage = decrease overhead of creating new files = decrease expense of lock/unlock. And so far our prototype yields acceptable performance!

Thanks again for the help!
Post Reply