Revision: 41347
Updated Code
at March 14, 2011 23:35 by stz184
Updated Code
<?php class logger { private $file; private $error = false; function __construct($file) { if(file_exists($file) && ((filesize($file) / 1024) > 1024)) { $dirname = dirname($file); $filename = basename($file); $newname = $dirname.'/'.date('d.m.Y').'_'.$filename; if(!rename($file, $newname)) { $this->error = 'Can\'t rename the old log file'; } foreach (glob($dirname.'/*.log') as $logfile) { //ако има Ñтари логове на повече от 1 меÑец if(filemtime($logfile) < (time() - (30 * 24 * 3600))) { unlink($logfile); } } file_put_contents($file, ''); } elseif(!file_exists($file)) { file_put_contents($file, ''); } $this->file = $file; } function log_start() { $msg_start = 'Start time: '.date('d.m.Y h:i:s').PHP_EOL; if(!file_put_contents($this->file, $msg_start, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function log_message($message) { $message = $message.PHP_EOL; if(!file_put_contents($this->file, $message, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function log_end() { $msg_end = 'End time: '.date('d.m.Y h:i:s').PHP_EOL; $msg_end .= '-------------------------------'.PHP_EOL; if(!file_put_contents($this->file, $msg_end, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function is_error() { if($this->error != false) { return true; } return false; } function get_error() { return $this->error; } } /* example usage $log = new logger('/path/to/log.txt'); //if file doesnt exiists, will be created $log->log_start(); //this will insert timestap to mark the beginning if($sallary < $work) { $log->log_message('Not acceptable sallary/work ratio'); } $log->log_end(); //this will insert timestap to mark the end of current log */ ?>
Revision: 41346
Updated Code
at February 16, 2011 20:34 by stz184
Updated Code
<?php class logger { private $file; private $error = false; function __construct($file) { if(file_exists($file) && (filesize($file) / 1024) > 1024) { $dirname = dirname($file); $filename = basename($file); $newname = $dirname.'/'.date('d.m.Y').'_'.$filename; if(!rename($file, $newname)) { $this->error = 'Can\'t rename the old log file'; } foreach (glob($dirname.'/*.log') as $logfile) { if(filemtime($dirname.'/'.$logfile) < (time() - (30 * 24 * 3600))) { unlink($dirname.'/'.$logfile); } } } else { file_put_contents($file, ''); } $this->file = $file; } function log_start() { $msg_start = 'Start time: '.date('d.m.Y h:i:s').PHP_EOL; if(!file_put_contents($this->file, $msg_start, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function log_message($message) { $message = $message.PHP_EOL; if(!file_put_contents($this->file, $message, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function log_end() { $msg_end = 'End time: '.date('d.m.Y h:i:s').PHP_EOL; $msg_end .= '-------------------------------'.PHP_EOL; if(!file_put_contents($this->file, $msg_end, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function is_error() { if($this->error != false) { return true; } return false; } function get_error() { return $this->error; } } /* example usage $log = new logger('/path/to/log.log'); //if file doesnt exiists, will be created $log->log_start(); //this will insert timestap to mark the beginning if($sallary < $work) { $log->log_message('Not acceptable sallary/work ratio'); } $log->log_end(); //this will insert timestap to mark the end of current log */ ?>
Revision: 41345
Updated Code
at February 16, 2011 19:36 by stz184
Updated Code
<?php class logger { private $file; private $error = false; function __construct($file) { if(file_exists($file) && (filesize($file) / 1024) > 1024) { $dirname = dirname($file); $filename = basename($file); $newname = $dirname.'/'.date('d.m.Y').'_'.$filename; if(!rename($file, $newname)) { $this->error = 'Can\'t rename the old log file'; } foreach (glob("*.log") as $logfile) { if(filemtime($dirname.'/'.$logfile) < (time() - (30 * 24 * 3600))) { unlink($dirname.'/'.$logfile); } } } else { file_put_contents($file, ''); } $this->file = $file; } function log_start() { $msg_start = 'Start time: '.date('d.m.Y h:i:s').PHP_EOL; if(!file_put_contents($this->file, $msg_start, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function log_message($message) { $message = $message.PHP_EOL; if(!file_put_contents($this->file, $message, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function log_end() { $msg_end = 'End time: '.date('d.m.Y h:i:s').PHP_EOL; $msg_end .= '-------------------------------'.PHP_EOL; if(!file_put_contents($this->file, $msg_end, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function is_error() { if($this->error != false) { return true; } return false; } function get_error() { return $this->error; } } /* example usage $log = new logger('/path/to/log.log'); //if file doesnt exiists, will be created $log->log_start(); //this will insert timestap to mark the beginning if($sallary < $work) { $log->log_message('Not acceptable sallary/work ratio'); } $log->log_end(); //this will insert timestap to mark the end of current log */ ?>
Revision: 41344
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 16, 2011 19:34 by stz184
Initial Code
<?php class logger { private $file; private $error = false; function __construct($file) { if(file_exists($file) && (filesize($file) / 1024) > 1024) { $dirname = dirname($file); $filename = basename($file); $newname = $dirname.'/'.date('d.m.Y').'_'.$filename; if(!rename($file, $newname)) { $this->error = 'Can\'t rename the old log file'; } foreach (glob("*.log") as $logfile) { //ако има Ñтари логове на повече от 1 меÑец if(filemtime($dirname.'/'.$logfile) < (time() - (30 * 24 * 3600))) { unlink($dirname.'/'.$logfile); } } } else { file_put_contents($file, ''); } $this->file = $file; } function log_start() { $msg_start = 'Start time: '.date('d.m.Y h:i:s').PHP_EOL; if(!file_put_contents($this->file, $msg_start, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function log_message($message) { $message = $message.PHP_EOL; if(!file_put_contents($this->file, $message, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function log_end() { $msg_end = 'End time: '.date('d.m.Y h:i:s').PHP_EOL; $msg_end .= '-------------------------------'.PHP_EOL; if(!file_put_contents($this->file, $msg_end, FILE_APPEND)) { $this->error = 'Can\'t write to log'; } } function is_error() { if($this->error != false) { return true; } return false; } function get_error() { return $this->error; } } /* example usage $log = new logger('/path/to/log.txt'); //if file doesnt exiists, will be created $log->log_start(); //this will insert timestap to mark the beginning if($sallary < $work) { $log->log_message('Not acceptable sallary/work ratio'); } $log->log_end(); //this will insert timestap to mark the end of current log */ ?>
Initial URL
Initial Description
This class can log messages to a file. If the log file becomes too large (over 1MB), the class will archive it and will create new. If there are archives older than one month, they will be deleted automatically. In the future versions I will add the opportunity to set max log size and max time to keep the archives.
Initial Title
Logging class (Log messages to a file)
Initial Tags
class, file, log
Initial Language
PHP