This is a discussion on fwrite for log within the PHP Language forums, part of the PHP Programming Forums category; Hi, I would like to have a php file that log informations sent by an image request. For logging, I ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi, I would like to have a php file that log informations sent by an image request. For logging, I use fwrite to write on a file. If more requests are done on the same time, there could be a lost of data because the file is open by one process and not available for the other processes? Thanks |
|
|||
|
Hanoi wrote:
> Hi, > > I would like to have a php file that log informations sent by an image > request. > > For logging, I use fwrite to write on a file. > > If more requests are done on the same time, there could be a lost of > data because the file is open by one process and not available for the > other processes? > > Thanks Hi, Yes, you should prepare yourself for that indeed. Use filelocking: http://nl2.php.net/manual/en/function.flock.php Filelocking is reliable, even on Windows. You'll find a basic (but working) example on that page. Regards, Erwin Moller |
|
|||
|
On 7 Jan, 08:36, Hanoi <hin...@inwind.it> wrote:
> Hi, > > I would like to have a php file that log informations sent by an image > request. > > For logging, I use fwrite to write on a file. > > If more requests are done on the same time, there could be a lost of > data because the file is open by one process and not available for the > other processes? > > Thanks Use an asynchronous logging mechanism (see the docs page for syslog()) C. |
|
|||
|
On Jan 7, 10:36 am, Hanoi <hin...@inwind.it> wrote:
> Hi, > > I would like to have a php file that log informations sent by an image > request. > > For logging, I use fwrite to write on a file. > > If more requests are done on the same time, there could be a lost of > data because the file is open by one process and not available for the > other processes? > > Thanks If you use OS as gnu/linux, cpu will handle each process sequently. |
|
|||
|
On Jan 8, 9:03 am, Betikci Boris <pard...@gmail.com> wrote:
> On Jan 7, 10:36 am, Hanoi <hin...@inwind.it> wrote: > > > Hi, > > > I would like to have a php file that log informations sent by an image > > request. > > > For logging, I use fwrite to write on a file. > > > If more requests are done on the same time, there could be a lost of > > data because the file is open by one process and not available for the > > other processes? > > > Thanks > > If you use OS as gnu/linux, cpu will handle each process sequently. No it won't. At the level of PHP code they will effectively be running concurrently, not sequentially. You can have multiple processes writing to a file, but in the absence of a mutex there is no guarantee that one write will not occurr in the middle of a seperate write - large writes are not atomic. C. |
|
|||
|
Betikci Boris wrote:
> On Jan 7, 10:36 am, Hanoi <hin...@inwind.it> wrote: >> Hi, >> >> I would like to have a php file that log informations sent by an image >> request. >> >> For logging, I use fwrite to write on a file. >> >> If more requests are done on the same time, there could be a lost of >> data because the file is open by one process and not available for the >> other processes? >> >> Thanks > > If you use OS as gnu/linux, cpu will handle each process sequently. > Incorrect. Processes are run concurrently, not sequentially. Any interrupt (i.e. disk i/o, timer, network traffic) will cause the OS to switch back to the system to handle the interrupt. When processing resumes, it will be the next process in the list. That is not necessarily the same process which was executing before the interrupt. That's why things like mutex semaphores and file locks exist. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|||
|
Hanoi wrote:
>> If you use OS as gnu/linux, cpu will handle each process sequently. > > > Yes, I use a debian OS ... > > So I can simply use fwrite without flock or any other asynchronous > logging mechanism? > > Thanks > > > > > Not safely. If it were safe, there would be no need for flock(). -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|||
|
Greetings, Hanoi.
In reply to Your message dated Monday, January 7, 2008, 11:36:10, > I would like to have a php file that log informations sent by an image > request. > For logging, I use fwrite to write on a file. For the logging purposes (considering there is not much of data sent to log) I'm using this filter function: function ob_debugfilter($str) { $str = date('Y-m-d H:i:s') . " {$str}\n"; if(defined('toolDebugFilterTarget') && toolDebugFilterTarget) { error_log($str, 3, toolDebugFilterTarget); $str = NULL; } return $str; } To initialize it, You should define a constant toolDebugFilterTarget with the path to Your logfile and start output buffering in Your code. Don't forget to use ob_flush(); at each point of log output, itherwise all Your log output from single run will be written in a single line. I.e. var_export($rc); @ob_flush(); -- Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru> |