Koobazaur Posted August 4, 2007 Share Posted August 4, 2007 Greetings, I have created an error logger that logs all errors to a text file on my server. Basically, when I include it, the constructor opens the file (with the "a" attribute) and when the deconstructor closes it. Hence I can write errors easily throughout my whole script. Now, something rather obvious occured to me lately. What if two users open a page at the same time and the error logger is executed simultaneously? While the file is open by one logger, wouldn't that block the second one from opening? Meaning, whatever errors the second logger would want to write, it would fail and, thus, I would end up missing some errors? I am not sure how exactly PHP deals with opening already open files, could someone please clarify. If my concerns are correct, I guess the best solution would be to open/close the file on every error log (not in contruct/destruct) and if it fails, then keep retrying for a few seconds in hopes the file gets freed. Or is there a better solution? Quote Link to comment Share on other sites More sharing options...
Eric_Ryk Posted August 5, 2007 Share Posted August 5, 2007 Well I'm pretty sure that you can have it be accessed multiple times at once, but I'm not sure exactly how it is handled. But the way you're doing it is slightly sloppy. I'd recommend storing all of the errors in the object, then when the destructor is called you open the file, write all the errors to the file, then close the file. Quote Link to comment Share on other sites More sharing options...
Koobazaur Posted August 5, 2007 Author Share Posted August 5, 2007 Why exactly is my solution "sloppy," assuming that multiple file accessing is allowed...? Quote Link to comment Share on other sites More sharing options...
Eric_Ryk Posted August 5, 2007 Share Posted August 5, 2007 Why exactly is my solution "sloppy," assuming that multiple file accessing is allowed...? You're wasting ram keeping it open. There's no point in having that connection open if you don't need it all the time. Writing all at once is a better way to go about it. Many people get lazy about these things due to the power that computers have now, but it's still important to pay attention to it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.