tags:

views:

24

answers:

2

Hi All,

I have a question. I have a script that's fired by a GET trigger. I'm trying to log the executions of the script using fwrite and append, but what I have happening is that if an instance is called while another instane is still writing the file, it will interrupt the fwrite and insert its own data. Is there any way I can queue the script to wait in case the file is being written to? But I don't want to lose any of the log data.

Thanks in advance!

+1  A: 

You should look into flock() probably. It might also be a good idea to have a create a separate file for each execution and then merge them at the end by locking the bigger log file or you might even consider sending everything to the database if it is feasible.

Sabeen Malik
+1  A: 

Using flock is a really bad idea - you then get into the realms of working out how long a file should locked for, articially suspending execution of your script - all kinds of nastiness. PHP does not have the kind of file locking semantics needed for concurrent file access.

Better solutions are:

  1. write the log entries to stderr - these should then appear in the webserver error_log
  2. use syslog
  3. use trigger_error($msg, E_USER_NOTICE) and make sure your error reporting is setup to log to a file
  4. write the log entries to a DBMS
symcbean