fwrite for log

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 [ Mo, 07 Januar 2008 09:36 ] [ ID #1901901 ]

Re: fwrite for log

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
Erwin Moller [ Mo, 07 Januar 2008 10:06 ] [ ID #1901906 ]

Re: fwrite for log

On 7 Jan, 08:36, Hanoi <hin... [at] 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.
colin.mckinnon [ Mo, 07 Januar 2008 13:15 ] [ ID #1901929 ]

Re: fwrite for log

On Jan 7, 10:36 am, Hanoi <hin... [at] 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.
Betikci Boris [ Di, 08 Januar 2008 10:03 ] [ ID #1902856 ]

Re: fwrite for log

On Jan 8, 9:03 am, Betikci Boris <pard... [at] gmail.com> wrote:
> On Jan 7, 10:36 am, Hanoi <hin... [at] 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.
colin.mckinnon [ Di, 08 Januar 2008 17:09 ] [ ID #1902921 ]

Re: fwrite for log

> 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
Hanoi [ Di, 08 Januar 2008 17:13 ] [ ID #1902923 ]

Re: fwrite for log

Betikci Boris wrote:
> On Jan 7, 10:36 am, Hanoi <hin... [at] 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 [at] attglobal.net
==================
Jerry Stuckle [ Di, 08 Januar 2008 18:59 ] [ ID #1902936 ]

Re: fwrite for log

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 [at] attglobal.net
==================
Jerry Stuckle [ Di, 08 Januar 2008 18:59 ] [ ID #1902937 ]

Re: fwrite for log

Ok. it's clear.

Thank you very much for your answers.
Hanoi [ Do, 10 Januar 2008 11:54 ] [ ID #1904748 ]

Re: fwrite for log

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); [at] ob_flush();


--
Sincerely Yours, AnrDaemon <anrdaemon [at] freemail.ru>
AnrDaemon [ Di, 15 Januar 2008 23:43 ] [ ID #1908620 ]
PHP » comp.lang.php » fwrite for log

Vorheriges Thema: ODBC Connection to dbase IV?
Nächstes Thema: php script as a cron job