PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hi,
I want to use some logging to log e.g. user errors and other problems occuring during my php script execution. There are numerus code examples already available. But I wonder: If I have some level of user traffic and a few events happen at the same time, what happens to my log file / events? Will I lose some events? Will I get errors? Do I have to take care of this case, by using some kind of queue?
of course, but: wouldn't this mean the log file writing is 'atomic'? I mean one instance would open the log file and start to write. What about another instance? They will continue to work as soon as they get cpu time. Or are the write accesses somehow buffered by PHP?
In my experience, when you get a few concurrent users pounding on a logfile, you start getting messages mixed together. You are likely to lose/garble messages.
If it's important that you not lose messages in this way, write each message to a unique file, then merge those files at the end of the day.
Or better still, keep your log in the database. Then each message has its own record.
When you flock() a file, what happens with all the other simultaneous file access requests? Are they all paused until the file is freed? And then, are they handled in order of access request?