fwrite for log

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 ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-07-2008
Hanoi
 
Posts: n/a
Default 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
Reply With Quote
  #2 (permalink)  
Old 01-07-2008
Erwin Moller
 
Posts: n/a
Default 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
Reply With Quote
  #3 (permalink)  
Old 01-07-2008
C. (http://symcbean.blogspot.com/)
 
Posts: n/a
Default Re: fwrite for log

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.
Reply With Quote
  #4 (permalink)  
Old 01-08-2008
Betikci Boris
 
Posts: n/a
Default Re: fwrite for log

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.
Reply With Quote
  #5 (permalink)  
Old 01-08-2008
C. (http://symcbean.blogspot.com/)
 
Posts: n/a
Default Re: fwrite for log

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.
Reply With Quote
  #6 (permalink)  
Old 01-08-2008
Hanoi
 
Posts: n/a
Default 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




Reply With Quote
  #7 (permalink)  
Old 01-08-2008
Jerry Stuckle
 
Posts: n/a
Default Re: fwrite for log

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
==================

Reply With Quote
  #8 (permalink)  
Old 01-08-2008
Jerry Stuckle
 
Posts: n/a
Default 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@attglobal.net
==================

Reply With Quote
  #9 (permalink)  
Old 01-10-2008
Hanoi
 
Posts: n/a
Default Re: fwrite for log


Ok. it's clear.

Thank you very much for your answers.
Reply With Quote
  #10 (permalink)  
Old 01-15-2008
AnrDaemon
 
Posts: n/a
Default 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); @ob_flush();


--
Sincerely Yours, AnrDaemon <anrdaemon@freemail.ru>

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:58 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0