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

update ahlogger

parent 425b72ff
No related branches found
No related tags found
1 merge request!49update ahlogger
<?php
/**
* ----------------------------------------------------------------------
*
* Debug logging during a client request.
* So you can measure any action find bottlenecks in your code.
*
* Licence: GNU GPL 3.0
* Source: https://github.com/axelhahn/ahlogger
* Docs: https://www.axel-hahn.de/docs/ahlogger/
*
* USAGE:
* (1) Trigger a message with add() to add a marker
* 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.
* 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
* 2023-05-15 fix _getBar() - division by zero
* ----------------------------------------------------------------------
*/
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
// ----------------------------------------------------------------------
/**
* constuctor
* @param string $sInitMessage init message
......@@ -32,6 +68,10 @@ class logger {
return true;
}
// ----------------------------------------------------------------------
// PUBLIC METHODS
// ----------------------------------------------------------------------
/**
* add a logging message
* @param type $sMessage
......@@ -76,6 +116,16 @@ class logger {
}
}
/**
* 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
*/
protected function _prepareRendering(){
$iMem=memory_get_usage();
$this->add('<hr>');
......@@ -158,8 +208,10 @@ class logger {
* @return {string}
*/
protected function _getBar($iVal, $iMax){
$iWidth=$iVal/$iMax*100;
return '<div class="bar"><div class="progress" style="width: '.$iWidth.'%;">&nbsp;</div></div>';
return $iMax>0
? '<div class="bar"><div class="progress" style="width: '.($iVal/$iMax*100).'%;">&nbsp;</div></div>'
: ''
;
}
/**
......@@ -208,7 +260,7 @@ class logger {
'<td>' . $aLogentry["counter"] . '</td>' .
'<td>' . $aLogentry["level"] . '</td>' .
'<td>' . $aLogentry["timer"] . '</td>' .
'<td>' . $this->_getBar($aLogentry["delta"], $aData["maxtime"]*1000). $aLogentry["delta"] .' ms</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>';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment