Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Writing to log file function acting strange
Old 01-15-2009, 12:50 PM Writing to log file function acting strange
jason_alan's Avatar
Super Talker

Posts: 100
Name: Jason
Location: Seattle, WA
Trades: 0
I'm trying to write to my log file using this code in my class:

PHP Code:
function writeLog($str) {
        
$h fopen($this->logFile'a');
        
fwrite($hdate("D M j G:i:s T Y") . ' ' $str PHP_EOL);
        
fclose($h);

It works all well and good, but calling this function writes the string to the log file at least 3 times, every time! I have verified this function is getting called exactly 1 time.

Ex:
$Flickr->writeLog('Generating Image');
Gives me this in my log file:

Thu Jan 15 8:48:59 PST 2009 Generating Image
Thu Jan 15 8:49:01 PST 2009 Generating Image
Thu Jan 15 8:49:01 PST 2009 Generating Image

Anyone have any ideas why this is happening?
jason_alan is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-15-2009, 03:49 PM Re: Writing to log file function acting strange
rogem002's Avatar
PHP Chap

Posts: 843
Name: Mike
Location: United Kingdom
Trades: 0
PHP Code:
function writeLog($str) {
        
file_put_contents(file_get_contents($this->logFile).date("D M j G:i:s T Y") . ' ' $str PHP_EOL$this->logFile);

Is a bit better coding wise.

However, i think something else is writing to the log as the timestamp is changing.
8:48:59
8:49:01
__________________
My Blog/Site:
Please login or register to view this content. Registration is FREE

Last edited by rogem002; 01-15-2009 at 03:52 PM..
rogem002 is offline
Reply With Quote
View Public Profile Visit rogem002's homepage!
 
Old 01-15-2009, 04:10 PM Re: Writing to log file function acting strange
jason_alan's Avatar
Super Talker

Posts: 100
Name: Jason
Location: Seattle, WA
Trades: 0
Yeah that should probably work, but both file_get_contents and file_put_contents open and close the file, leading to some unnecessary processing time. I'll implement it for now till I can figure out what's going on here
jason_alan is offline
Reply With Quote
View Public Profile
 
Old 01-15-2009, 04:57 PM Re: Writing to log file function acting strange
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
Quote:
Originally Posted by rogem002 View Post
PHP Code:
function writeLog($str) {
        
file_put_contents(file_get_contents($this->logFile).date("D M j G:i:s T Y") . ' ' $str PHP_EOL$this->logFile);

Is a bit better coding wise.
Performances wise, it's not recommended.
What this code does is to read the complete file, append a line and overwrite the original file with the full content.

On a small log, ok.
But when your log reaches a certain size, it's overkill.

The fopen() in append mode is much more logical and put less stress over the server.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to Writing to log file function acting strange
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.13788 seconds with 12 queries