Jump to content

File-based error logging question


Koobazaur

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.