Update logging class (#6829)

* logging class improvements

* remove alias

* adjust line print out for field order

* refactor class name to be inline with framework standard

* fix writing direct to write method
This commit is contained in:
frytimo 2023-11-13 21:55:52 -04:00 committed by GitHub
parent 2bbce23ff2
commit 12fb45c99f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 152 additions and 78 deletions

View File

@ -1,91 +1,165 @@
<?php <?php
/** /**
* Logging class: * Logging class:
* - contains lfile, lwrite and lclose public methods * - constructor requires the full file name and path for the log file. if it
* - lfile sets path and name of log file * does not exist php will automatically try and create it. The log file will
* - lwrite writes message to the log file (and implicitly opens log file) * remain open for the life cycle of the object to improve performance.
* - lclose closes log file * - message is written with the following format: [d/M/Y:H:i:s] (script name) message.
* - first call of lwrite method will open log file implicitly * - log file is closed when the object is destroyed.
* - 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 // declare log file and file pointer as private properties
private $log_file, $fp; private $fp;
private $debug_func;
private $debug_line;
private $debug_file;
private $debug_class;
// set log file (path and name) public function __construct(string $filename_and_path) {
//init values
$this->clear_debug();
public function lfile($path) try {
{ //open file in append mode
$this->log_file = $path; $this->fp = fopen($filename_and_path, 'a');
} catch (Exception $ex) {
//send the error to the caller
throw $ex;
}
}
public function __destruct() {
try {
$this->flush();
} catch (Exception $ex) {
//do nothing
} finally {
//close the file
if (is_resource($this->fp)) {
fclose($this->fp);
}
}
}
/**
* Ensure all data arrives on disk
* @throws Exception
*/
public function flush() {
try {
//ensure everything arrives on disk
if (is_resource($this->fp)) {
fflush($this->fp);
}
} catch (Exception $ex) {
throw $ex;
}
} }
// write message to the log file // write message to the log file
private function _write($msg) {
public function lwrite($message)
{
// if file pointer doesn't exist, then open log file
if (!$this->fp) {
$this->lopen();
}
// define script name
$script_name = pathinfo($_SERVER['PHP_SELF'], PATHINFO_FILENAME);
// define current time and suppress E_WARNING if using the system TZ settings // define current time and suppress E_WARNING if using the system TZ settings
// (don't forget to set the INI setting date.timezone) }
$time = @date('[d/M/Y:H:i:s]');
private function clear_debug() {
$this->debug_line = null;
$this->debug_file = null;
$this->debug_func = null;
$this->debug_class = null;
}
/**
* Write raw data to the
* @param string $level
* @param string $message
*/
public function write(string $level, string $message) {
$this->get_backtrace_details();
// write current time, script name and message to the log file // write current time, script name and message to the log file
fwrite($this->fp, "$time ($script_name) $message" . PHP_EOL); // (don't forget to set the INI setting date.timezone)
$time = @date('Y-m-d H:i:s');
$file = $this->debug_file ?? 'file not set';
$line = $this->debug_line ?? '0000';
fwrite($this->fp, "[$time] [$level] [{$file}:{$line}] $message");
$this->clear_debug();
} }
// close log file (it's always a good idea to close a file when you're done with it) public function debug_class(?string $debug_class = null) {
if (func_num_args() > 0) {
private function lopen() $this->debug_class = $debug_class;
{ return $this;
// in case of Windows set default log file
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$log_file_default = 'c:/php/logfile.txt';
} // set default log file for Linux and other systems
else {
$log_file_default = '/tmp/logfile.txt';
} }
// define log file from lfile method or use previously set default return $this->debug_class;
$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 debug_line(?string $debug_line = null) {
if (func_num_args() > 0) {
public function lclose() $this->debug_line = $debug_line;
{ return $this;
fclose($this->fp); }
return $this->debug_line;
} }
public function log($level, $msg){ public function debug_func(?string $debug_func = null) {
if (func_num_args() > 0) {
$log_file = $_SESSION['logging']['log']['logfile']; $this->debug_func = $debug_func;
$log_level = $_SESSION['logging']['log']['loglevel']; return $this;
}
if ($log_level !== $level) { return $this->debug_func;
return;
} }
if ($log_file) { public function debug_file(?string $debug_file = null) {
$this->lfile($log_file); if (func_num_args() > 0) {
$this->debug_file = $debug_file;
return $this;
}
return $this->debug_file;
} }
$this->lopen(); public function writeln($level, $message) {
$this->get_backtrace_details();
$this->write($level, $message . "\n");
}
$this->lwrite("[".strtoupper($level)."] ".$msg); public function debug($message) {
$this->get_backtrace_details();
$this->writeln("DEBUG", $message);
}
//close handle public function info($message) {
$this->lclose(); $this->get_backtrace_details();
$this->writeln("INFO", $message);
}
public function warning($message) {
$this->get_backtrace_details();
$this->writeln("WARNING", $message);
}
public function error($message) {
$this->get_backtrace_details();
$this->writeln("ERROR", $message);
}
private function get_backtrace_details() {
if ($this->debug_file === null) {
$debug = debug_backtrace();
$ndx = count($debug) - 1;
$this->debug_file = $debug[$ndx]['file'];
$this->debug_line = $debug[$ndx]['line'];
$this->debug_func = $debug[$ndx]['function'];
$this->debug_class = $debug[$ndx]['class'] ?? '';
}
} }
} }
//$log = new Logging(); /*
//$log->log("debug", "passed validation, ".__line__); * Example:
//$log->log("debug", check_str($_POST["ivr_menu_uuid"])); $log = new logging(sys_get_temp_dir() . '/logging.log');
$log->writeln("debug", "passed validation");
?> $log->debug("pass");
$log->warning("variable should not used");
$log->debug_file(__FILE__)->debug_line(__LINE__)->write("DEBUG", "Raw message\n");
*/