fwrite for log
am 07.01.2008 09:36:10 von Hanoi
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
Re: fwrite for log
am 07.01.2008 10:06:00 von Erwin Moller
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
Re: fwrite for log
am 07.01.2008 13:15:32 von colin.mckinnon
On 7 Jan, 08:36, 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
Use an asynchronous logging mechanism (see the docs page for syslog())
C.
Re: fwrite for log
am 08.01.2008 10:03:09 von Betikci Boris
On Jan 7, 10:36 am, 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
If you use OS as gnu/linux, cpu will handle each process sequently.
Re: fwrite for log
am 08.01.2008 17:09:58 von colin.mckinnon
On Jan 8, 9:03 am, Betikci Boris wrote:
> On Jan 7, 10:36 am, 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
>
> 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.
Re: fwrite for log
am 08.01.2008 17:13:31 von Hanoi
> 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
Re: fwrite for log
am 08.01.2008 18:59:13 von Jerry Stuckle
Betikci Boris wrote:
> On Jan 7, 10:36 am, 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
>
> 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
==================
Re: fwrite for log
am 08.01.2008 18:59:59 von Jerry Stuckle
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
==================
Re: fwrite for log
am 10.01.2008 11:54:05 von Hanoi
Ok. it's clear.
Thank you very much for your answers.
Re: fwrite for log
am 15.01.2008 23:43:04 von AnrDaemon
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