Newb
Newb

Reputation: 678

php Logging Issues

Code works fine, except for the fact that there is a problem here:

 //Log Events
function logEvent($newinput) {
    if ($newinput !== NULL) {
        // Add a timestamp to the start of the $message
        $newinput = date("Y/m/d H:i:s").': '.$newinput;
        $fp = fopen('log.txt', 'w');
        fwrite($fp, $newinput."\n");
        fclose($fp);
    }
}
//Problem writing these two lines to log.txt?
 //The bad, the two lines below are not on the log.txt
logEvent('Selection'.$selections[$selection]);
logEvent('Change' . $change. 'cents.');

//This line is written to a text file (log.txt), okay that's good.
logEvent('Input' .  $newinput);

Upvotes: 0

Views: 117

Answers (2)

philjohn
philjohn

Reputation: 546

You need to use the append modifier when opening the file, you've gone

fopen('log.txt', 'w');

this means that every time you call that function, the log file is getting blown out and recreated, if you instead used

fopen('log.txt', 'a');

then your new log entries will append to the file instead.

You could also look into keeping the file open for later inserts too, but there may be issues with multiple updates in other requests.

Upvotes: 0

ufk
ufk

Reputation: 32134

i think you're not appending to the file, you're rewriting it. try fopen with 'a' instead of 'w'.

Upvotes: 1

Related Questions