Skip to content
Snippets Groups Projects
Commit c2e0b819 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch 'fix-logger' into 'master'

fix logger class

See merge request !47
parents ee66aa70 63b94355
No related branches found
No related tags found
1 merge request!47fix logger class
Pipeline #1472 passed
<?php
/**
* ----------------------------------------------------------------------
*
* Debug logging during a client request.
* So you can measure any action find bottlenecks in your code.
*
<<<<<<< public_html/deployment/classes/logger.class.php
* Source: https://github.com/axelhahn/ahlogger
*
* USAGE:
......@@ -21,55 +18,6 @@ class logger {
protected $aMessages = [];
protected $bShowDebug = false;
protected $_iMemStart = false;
=======
* Licence: GNU GPL 3.0
* Source: https://github.com/axelhahn/ahlogger
* Docs: https://www.axel-hahn.de/docs/ahlogger/
*
* USAGE:<br>
* (1) Trigger a message with add() to add a marker<br>
* (2) The render() method lists all items in a table with time since start
* and the delta to the last message. <br>
*
* @author www.axel-hahn.de
*
* ----------------------------------------------------------------------
* 2016-02-26 init
* 2016-11-19 add memory usage
* (...)
* 2022-09-25 add memory tracking, add cli renderer
* 2022-09-27 css updates
* 2022-10-02 add emoji chars
* 2022-10-16 mark longest action with an icon
* 2022-12-15 make it compatible to PHP 8.2; add doc + comments
* ----------------------------------------------------------------------
*/
class logger {
/**
* @var {array} array of added messages
*/
protected $aMessages = [];
/**
* @var {bool} flag: show debug infos? default: false
*/
protected $bShowDebug = false;
/**
* @var {int} memory usage on start
*/
protected $_iMemStart = false;
/**
* @var {string} dynamic prefix for used css - it is set in the cronstructor
*/
protected $sCssPrefix = '';
// ----------------------------------------------------------------------
// CONSTRUCTOR
// ----------------------------------------------------------------------
>>>>>>> public_html/deployment/classes/logger.class.php
/**
* constuctor
......@@ -84,13 +32,6 @@ class logger {
return true;
}
<<<<<<< public_html/deployment/classes/logger.class.php
=======
// ----------------------------------------------------------------------
// PUBLIC METHODS
// ----------------------------------------------------------------------
>>>>>>> public_html/deployment/classes/logger.class.php
/**
* add a logging message
* @param type $sMessage
......@@ -135,19 +76,6 @@ class logger {
}
}
<<<<<<< public_html/deployment/classes/logger.class.php
=======
/**
* helper function: prepare array of added massages before output
* - detect warnings and errors
* - detect needed time for each action
* - detect longest action
* - detect maximum of memory usage
* - calculate total time
*
* @return array
*/
>>>>>>> public_html/deployment/classes/logger.class.php
protected function _prepareRendering(){
$iMem=memory_get_usage();
$this->add('<hr>');
......@@ -188,18 +116,6 @@ class logger {
if ($iDelta > $iMaxtime) {
$iMaxtime = $iDelta;
$sMaxRowId = $sTrId;
<<<<<<< public_html/deployment/classes/logger.class.php
}
$iMaxmem=max($aLogentry["memory"], $iMaxmem);
if (($iDelta > 1) || $aLogentry["level"] == "warning") {
$aReturn['warnings'].='<a href="#' . $sTrId . '" title="' . sprintf("%01.4f", $iDelta) . ' s">' . $iCounter . '</a>&nbsp;';
}
if ($aLogentry["level"] == "error") {
$aReturn['errors'].='<a href="#' . $sTrId . '" title="' . sprintf("%01.4f", $iDelta) . ' s">' . $iCounter . '</a>&nbsp;';
}
=======
}
$iMaxmem=max($aLogentry["memory"], $iMaxmem);
......@@ -210,13 +126,11 @@ class logger {
if ($aLogentry["level"] == "error") {
$aReturn['errors'].='<a href="#' . $sTrId . '" title="' . sprintf("%01.4f", $iDelta) . ' s">' . $iCounter . '</a>&nbsp;';
}
>>>>>>> public_html/deployment/classes/logger.class.php
$aReturn['entries'][]=[
'time'=>$aLogentry["time"],
'level'=>$aLogentry["level"],
'message'=>$aLogentry["message"],
'memory'=>sprintf("%01.2f", $aLogentry["memory"]/1024/1024), // MB
<<<<<<< public_html/deployment/classes/logger.class.php
'trid'=>$sTrId,
'trclass'=>$aLogentry["level"],
......@@ -299,90 +213,6 @@ class logger {
'<td>' . $aLogentry["message"] . '</td>' .
'</tr>';
}
=======
'trid'=>$sTrId,
'trclass'=>$aLogentry["level"],
'counter'=>$iCounter,
'timer'=>sprintf("%01.3f", $aLogentry["time"] - $sStarttime),
'delta'=>sprintf("%01.0f", $iDelta*1000),
];
$iLasttime = $aLogentry["time"];
}
$aReturn['level']=($bHasWarning
? ($bHasError ? 'error' : 'warning')
: ''
);
$aReturn['maxrowid']=$sMaxRowId;
$aReturn['maxtime']=sprintf("%01.3f", $iMaxtime);
$aReturn['maxmem']=sprintf("%01.2f", $iMaxmem/1024/1024);
$aReturn['totaltime']=sprintf("%01.3f", $aLogentry['time']-$aReturn['entries'][0]['time']);
return $aReturn;
}
/**
* get html code for a progressbar with divs
* @param {int|float} $iVal value between 0..max value
* @param {int|float} $iMax max value
* @return {string}
*/
protected function _getBar($iVal, $iMax){
$iWidth=$iVal/$iMax*100;
return '<div class="bar"><div class="progress" style="width: '.$iWidth.'%;">&nbsp;</div></div>';
}
/**
* render output of all logging messages
*/
public function render() {
if (!$this->bShowDebug){
return false;
}
$aData=$this->_prepareRendering();
/*
Array
(
[totaltime] => 0.006
[errors] =>
[warnings] => 3
[maxrowid] => debugTableRow3
[maxtime] => 0.005
[result] => Array
(
)
[entries] => Array
mit Elementen
Array
(
[time] => 1663959608.2566
[level] => info
[message] => Logger was initialized.
[memory] => 538056
[trid] => debugTableRow1
[trclass] => info
[trstyle] =>
[counter] => 1
[timer] => 0.000
[delta] => 0.000
)
*/
$sOut='';
// echo '<pre>'; print_r($aData); die();
foreach ($aData['entries'] as $aLogentry){
$sOut.='<tr class="'.$this->sCssPrefix.'-level-' . $aLogentry["level"] . ''.($aLogentry["trid"]==$aData["maxrowid"] ? ' '.$this->sCssPrefix.'-maxrow' : '').'" '
.'id="' . $aLogentry["trid"] . '">' .
'<td>' . $aLogentry["counter"] . '</td>' .
'<td>' . $aLogentry["level"] . '</td>' .
'<td>' . $aLogentry["timer"] . '</td>' .
'<td>' . $this->_getBar($aLogentry["delta"], $aData["maxtime"]*1000). $aLogentry["delta"] .' ms'.($aLogentry["delta"]==$aData['maxtime']*1000 ? ' ⏱️' : '').'</td>' .
'<td>' . $this->_getBar($aLogentry["memory"], $aData["maxmem"]) . $aLogentry["memory"] .' MB'. '</td>' .
'<td>' . $aLogentry["message"] . '</td>' .
'</tr>';
}
>>>>>>> public_html/deployment/classes/logger.class.php
if ($sOut){
$sOut = '
<style>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment