Fix Login class logging per class setting, fix per class getter

Add a new helper function to get current class that called the debug function.
Fix bug were log_per was not set correctly
Change all get_class calls in debug to the new helper function
This commit is contained in:
Clemens Schwaighofer
2021-06-15 18:06:21 +09:00
parent 3035287b5c
commit b628331a9b
4 changed files with 35 additions and 19 deletions

View File

@@ -120,11 +120,11 @@ class Login extends \CoreLibs\DB\IO
*/ */
public function __construct(array $db_config) public function __construct(array $db_config)
{ {
// log login data for this class only
$this->log->setLogPer('class', true);
// create db connection and init base class // create db connection and init base class
parent::__construct($db_config); parent::__construct($db_config);
// log login data for this class only
$this->log->setLogPer('class', true);
// set db special errors
if ($this->db_init_error === true) { if ($this->db_init_error === true) {
echo 'Could not connect to DB<br>'; echo 'Could not connect to DB<br>';
// if I can't connect to the DB to auth exit hard. No access allowed // if I can't connect to the DB to auth exit hard. No access allowed

View File

@@ -229,8 +229,8 @@ class Logging
$rpl_string = !$this->log_per_level ? '' : '_'.$level; // if request to write to one file $rpl_string = !$this->log_per_level ? '' : '_'.$level; // if request to write to one file
$fn = str_replace('##LEVEL##', $rpl_string, $fn); // create output filename $fn = str_replace('##LEVEL##', $rpl_string, $fn); // create output filename
// set per class, but don't use get_class as we will only get self
$rpl_string = !$this->log_per_class ? '' : '_'.str_replace('\\', '-', get_class($this)); // set sub class settings $rpl_string = !$this->log_per_class ? '' : '_'.str_replace('\\', '-', \CoreLibs\Debug\Support::getCallerClass()); // set sub class settings
$fn = str_replace('##CLASS##', $rpl_string, $fn); // create output filename $fn = str_replace('##CLASS##', $rpl_string, $fn); // create output filename
$rpl_string = !$this->log_per_page ? '' : '_'.\CoreLibs\Get\System::getPageName(1); // if request to write to one file $rpl_string = !$this->log_per_page ? '' : '_'.\CoreLibs\Get\System::getPageName(1); // if request to write to one file
@@ -252,6 +252,19 @@ class Logging
return true; return true;
} }
/**
* Superseeded by \CoreLibs\Debug\Support::getCallerClass() calls
*
* @return string Class name where called
* @deprecated Use \CoreLibs\Debug\Support::getCallerClass() insteader
*/
private function getCallerClass(): string
{
// get the last class entry and wrie that
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) ?? [['class' => get_class($this)]];
return end($backtrace)['class'];
}
// *** PUBLIC *** // *** PUBLIC ***
/** /**
@@ -384,7 +397,7 @@ class Logging
if (!in_array($type, ['level', 'class', 'page', 'run'])) { if (!in_array($type, ['level', 'class', 'page', 'run'])) {
return; return;
} }
$this->{'log_per'.$type} = $set; $this->{'log_per_'.$type} = $set;
} }
/** /**
@@ -397,7 +410,7 @@ class Logging
if (!in_array($type, ['level', 'class', 'page', 'run'])) { if (!in_array($type, ['level', 'class', 'page', 'run'])) {
return false; return false;
} }
return $this->{'log_per'.$type}; return $this->{'log_per_'.$type};
} }
/** /**
@@ -418,8 +431,7 @@ class Logging
$this->error_msg[$level] = ''; $this->error_msg[$level] = '';
} }
// get the last class entry and wrie that // get the last class entry and wrie that
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) ?? [['class' => get_class($this)]]; $class = \CoreLibs\Debug\Support::getCallerClass();
$class = end($backtrace)['class'];
// get timestamp // get timestamp
$timestamp = \CoreLibs\Debug\Support::printTime(); $timestamp = \CoreLibs\Debug\Support::printTime();
// HTML string, create only if we have echo // HTML string, create only if we have echo
@@ -502,7 +514,7 @@ class Logging
// create the output wrapper around, so we have a nice formated output per class // create the output wrapper around, so we have a nice formated output per class
if ($string_output) { if ($string_output) {
$string_prefix = '<div style="text-align: left; padding: 5px; font-size: 10px; font-family: sans-serif; border-top: 1px solid black; border-bottom: 1px solid black; margin: 10px 0 10px 0; background-color: white; color: black;">' $string_prefix = '<div style="text-align: left; padding: 5px; font-size: 10px; font-family: sans-serif; border-top: 1px solid black; border-bottom: 1px solid black; margin: 10px 0 10px 0; background-color: white; color: black;">'
.'<div style="font-size: 12px;">{<span style="font-style: italic; color: #928100;">'.get_class($this).'</span>}</div>'; .'<div style="font-size: 12px;">{<span style="font-style: italic; color: #928100;">'.\CoreLibs\Debug\Support::getCallerClass().'</span>}</div>';
$string_output = $string_prefix.$string_output $string_output = $string_prefix.$string_output
.'<div><span style="font-style: italic; color: #108db3;">Script Run Time:</span> '.$script_end.'</div>' .'<div><span style="font-style: italic; color: #108db3;">Script Run Time:</span> '.$script_end.'</div>'
.'</div>'; .'</div>';

View File

@@ -55,6 +55,19 @@ class Support
} }
return null; return null;
} }
/**
* Get the current class where this function is called
* Is mostly used in debug log statements to get the class where the debug was called
* gets top level class
*
* @return string Class name with namespace
*/
public static function getCallerClass(): string
{
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) ?? [['class' => get_called_class()]];
return end($backtrace)['class'];
}
} }
// __END__ // __END__

View File

@@ -16,15 +16,6 @@ if (defined('BASE')) {
DEFINE('CURRENT_WORKING_DIR', str_replace('lib', '', __DIR__)); DEFINE('CURRENT_WORKING_DIR', str_replace('lib', '', __DIR__));
} }
// METHOD: MyErrorHandler
// PARAMS: $type: the error code from PHP
// $message: the error message from php
// $file: in which file the error happend. this is the source file (eg include)
// $line: in which line the error happened
// $context: array with all the variable
// RETURN: true, so cought errors do not get processed by the PHP error engine
// DESC: will catch any error except E_ERROR and try to write them to the log file in log/php_error-<DAY>.llog
// if this fails, it will print the data to the window via echo
/** /**
* will catch any error except E_ERROR and try to write them to the log file * will catch any error except E_ERROR and try to write them to the log file
* in log/php_error-<DAY>.log * in log/php_error-<DAY>.log