Logging class update with dedicated print arrray wrapper

add a prAr that is a new wrapper around print_r, but it does not use
<pre> for layout formatting but {##HTMLPRE##} which will be removed for log
file write or replace with <pre> if printed to the web page
This commit is contained in:
Clemens Schwaighofer
2021-06-16 14:42:52 +09:00
parent d068aaeed7
commit f04487d553
4 changed files with 41 additions and 14 deletions

View File

@@ -86,6 +86,7 @@ class TestR extends TestL
print "** GETCALLERCLASS(INSIDE EXTND CLASS): ".\CoreLibs\Debug\Support::getCallerClass()."<br>";
$this->log->debug('TESTR', 'Logging in class testR (extends testL)');
$this->test('TESTR INSIDE');
$this->log->debug('TESTR', 'Array: '.$this->log->prAr(['a', 'b']).', Other: '.$this->log->prAr(['a', 'b']));
return true;
}
}
@@ -109,7 +110,7 @@ print "S::FDEBUG: ".FileWriter::fdebug('CLASS TEST DEBUG FILE: '.date('Y-m-d H:i
// future DEPRECATED
// $basic->debug('BASIC CLASS', 'Debug test');
$basic->log->debug('BASIC CLASS', 'Debug test');
print "BASIC:<br>".$basic->log->printErrorMsg();
print "BASIC PRINTERRORMSG:<br>".$basic->log->printErrorMsg();
print "</body></html>";

View File

@@ -3,10 +3,10 @@
* @phan-file-suppress PhanTypeSuspiciousStringExpression
*/
$DEBUG_ALL_OVERRIDE = 0; // set to 1 to debug on live/remote server locations
$DEBUG_ALL = 1;
$PRINT_ALL = 1;
$DB_DEBUG = 1;
$DEBUG_ALL_OVERRIDE = false; // set to 1 to debug on live/remote server locations
$DEBUG_ALL = true;
$PRINT_ALL = true;
$DB_DEBUG = true;
if ($DEBUG_ALL) {
error_reporting(E_ALL | E_STRICT | E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR);
@@ -91,6 +91,7 @@ foreach (['debug', 'echo', 'print'] as $type) {
$basic->log->debug('SOME MARK', 'Some error output');
// INTERNAL SET
print "EDIT ACCESS ID: ".$basic->edit_access_id."<br>";
if (is_object($login)) {
// print "ACL: <br>".$basic->print_ar($login->acl)."<br>";

View File

@@ -413,6 +413,20 @@ class Logging
return $this->{'log_per_'.$type};
}
/**
* A replacement for the \CoreLibs\Debug\Support::printAr
* But this does not wrap it in <pre></pre>
* It uses some special code sets so we can convert that to pre flags
* for echo output {##HTMLPRE##} ... {##/HTMLPRE##}
* Do not use this without using it in a string in debug function
* @param array $a Array to format
* @return string print_r formated
*/
public function prAr(array $a): string
{
return '{##HTMLPRE##}'.print_r($a, true).'{##/HTMLPRE##}';
}
/**
* write debug data to error_msg array
* @param string $level id for error message, groups messages together
@@ -441,12 +455,17 @@ class Logging
.'['.$this->running_uid.'] '
.'{'.$class.'} '
.'<'.$level.'> - '
// if stripping all html, etc is requested, only for write error msg
.($strip ?
// find any <br> and replace them with \n
// strip rest of html elements (base only)
preg_replace("/(<\/?)(\w+)([^>]*>)/", '[\\2]', str_replace('<br>', "\n", $string)) :
$string
// strip the htmlpre special tags if exist
.preg_replace(
"/{##HTMLPRE##}((.|\n)*?){##\/HTMLPRE##}/m",
'\\1',
// if stripping all html, etc is requested, only for write error msg
($strip ?
// find any <br> and replace them with \n
// strip rest of html elements (base only)
preg_replace("/(<\/?)(\w+)([^>]*>)/", '', str_replace('<br>', "\n", $string)) :
$string
)
)
."\n"
);
@@ -463,7 +482,9 @@ class Logging
.'[<span style="color: #b000ab;">'.$this->host_name.'</span>] '
.'[<span style="color: #08b369;">'.$this->page_name.'</span>] '
.'[<span style="color: #0062A2;">'.$this->running_uid.'</span>] '
.'{<span style="font-style: italic; color: #928100;">'.$class.'</span>} - '.\CoreLibs\Convert\Html::htmlent($string)
.'{<span style="font-style: italic; color: #928100;">'.$class.'</span>} - '
// we replace special HTMLPRE with <pre> entries
.preg_replace("/{##HTMLPRE##}((.|\n)*?){##\/HTMLPRE##}/m", "<pre>\\1</pre>", \CoreLibs\Convert\Html::htmlent($string))
."</div><!--#BR#-->";
}
return true;

View File

@@ -60,13 +60,17 @@ class Support
* 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
*
* loops over the debug backtrace until if finds the first class (from the end)
* @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'];
$class = null;
while ($class === null) {
$class = array_pop($backtrace)['class'] ?? null;
}
return $class ?? '';
}
}