added new log function

This commit is contained in:
Nuno Miguel Reis
2014-01-20 21:55:36 +00:00
parent b4c5bffe68
commit 4dcdd3a0ca
+76 -43
View File
@@ -1,5 +1,5 @@
<?php <?php
/** /**
* Logging class: * Logging class:
* - contains lfile, lwrite and lclose public methods * - contains lfile, lwrite and lclose public methods
* - lfile sets path and name of log file * - lfile sets path and name of log file
@@ -8,46 +8,79 @@
* - first call of lwrite method will open log file implicitly * - first call of lwrite method will open log file implicitly
* - message is written with the following format: [d/M/Y:H:i:s] (script name) message * - message is written with the following format: [d/M/Y:H:i:s] (script name) message
*/ */
class Logging { class Logging
// declare log file and file pointer as private properties {
private $log_file, $fp; // declare log file and file pointer as private properties
// set log file (path and name) private $log_file, $fp;
public function lfile($path) {
$this->log_file = $path; // set log file (path and name)
}
// write message to the log file public function lfile($path)
public function lwrite($message) { {
// if file pointer doesn't exist, then open log file $this->log_file = $path;
if (!$this->fp) { }
$this->lopen();
} // write message to the log file
// define script name
$script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME); public function lwrite($message)
// define current time and suppress E_WARNING if using the system TZ settings {
// (don't forget to set the INI setting date.timezone) // if file pointer doesn't exist, then open log file
$time = @date('[d/M/Y:H:i:s]'); if (!$this->fp) {
// write current time, script name and message to the log file $this->lopen();
fwrite($this->fp, "$time ($script_name) $message" . PHP_EOL); }
} // define script name
// close log file (it's always a good idea to close a file when you're done with it) $script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
public function lclose() { // define current time and suppress E_WARNING if using the system TZ settings
fclose($this->fp); // (don't forget to set the INI setting date.timezone)
} $time = @date('[d/M/Y:H:i:s]');
// open log file (private method) // write current time, script name and message to the log file
private function lopen() { fwrite($this->fp, "$time ($script_name) $message" . PHP_EOL);
// in case of Windows set default log file }
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$log_file_default = 'c:/php/logfile.txt'; // close log file (it's always a good idea to close a file when you're done with it)
}
// set default log file for Linux and other systems private function lopen()
else { {
$log_file_default = '/tmp/logfile.txt'; // in case of Windows set default log file
} if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
// define log file from lfile method or use previously set default $log_file_default = 'c:/php/logfile.txt';
$lfile = $this->log_file ? $this->log_file : $log_file_default; } // set default log file for Linux and other systems
// open log file for writing only and place file pointer at the end of the file else {
// (if the file does not exist, try to create it) $log_file_default = '/tmp/logfile.txt';
$this->fp = fopen($lfile, 'a') or exit("Can't open $lfile!"); }
} // define log file from lfile method or use previously set default
$lfile = $this->log_file ? $this->log_file : $log_file_default;
// open log file for writing only and place file pointer at the end of the file
// (if the file does not exist, try to create it)
$this->fp = fopen($lfile, 'a') or exit("Can't open $lfile!");
}
// open log file (private method)
public function lclose()
{
fclose($this->fp);
}
public function log($level, $msg){
$log_file = $_SESSION['logging']['log']['logfile'];
$log_level = $_SESSION['logging']['log']['loglevel'];
if ($log_file) {
$this->lfile($log_file);
}
else {
$this->lopen();
}
if ($log_level === $level) {
$this->lwrite("[".strtoupper($level)."] ".$msg);
}
//close handle
$this->lclose();
}
} }
?> ?>