note 74548 deleted from function.fwrite by bjori
| From: | bjori@php.net | Date: | Tue, 17 Apr 2007 09:32:07 +0000 |
| Subject: | note 74548 deleted from function.fwrite by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-125504@lists.php.net to get a copy of this message | ||
Note Submitter: Richard Snazell
Reason: bad code
----
Here is a simple class which can be used to write data to a log file. It captures the date/time of
the log entry, the user's remote IP address, and a definable $string. Each entry is appended as
a new line in the log file. A new log file is created daily named YYYYMMDD.log.
<?php
/*
usage:
define('LOG_PATH', '/path/to/save/log/files');
$log = new logger();
$log->write('message string to write in log file');
*/
class logger {
var $ip = null;
var $file = null;
// --- function: constructor --- //
function logger() {
$this->ip = $_SERVER['REMOTE_ADDR'];
$this->file = LOG_PATH.'/'.date('Ymd').'.log';
}
// --- function: write --- //
function write($string) {
if($fp = fopen($this->file, 'a')) {
if(flock($fp, LOCK_EX)) { // do exclusive lock
$log_entry = date('Y-m-d H:i:s')." | ".$this->ip." |
".$string."\r\n";
fwrite($fp, $log_entry);
flock($fp, LOCK_UN); // release the lock
fclose($fp);
}
}
}
} // end class
?>