note 118678 deleted from function.debug-backtrace by cmb
| From: | cmb@php.net | Date: | Sun, 17 Nov 2019 08:30:10 +0000 |
| Subject: | note 118678 deleted from function.debug-backtrace by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-213062@lists.php.net to get a copy of this message | ||
Note Submitter: eggmatters at gmail dot com
----
Here is a simple trace to incorporate into your Logging class. This is if you've written an MVC
or any OOP type of application including a Logging class.
The problem is, calling the first occurence of debug_backtrace()[]['class'] will echo the
logger class not the caller.
Below, I've employed some semantics which dovetail with my architecture:
Everything is either a class, or a script.
The caller, if a class, should be the last trace before the Logger class traces:
<?php
class Logger {
. . .
private static function formatMesssage($level, $message) {
$millis = explode(".", microtime(true));
$date = new DateTime();
return $date->format('Y-m-d h:i:s:') . $millis[1] . " [$level][" .
self::caller() . "] $message\n";
}
//Debug backtrace called. Find next occurence of class after Logger, or return calling script:
private static function caller() {
$dbg = debug_backtrace();
$i = 0;
$class = filter_input(INPUT_SERVER, 'SCRIPT_NAME');
while ($i < count($dbg) - 1 ) {
if (!empty($dbg[$i]['class']) && $dbg[$i]['class'] !=
'Logger' ) {
$class = $dbg[$i]['class'] . "->" . $dbg[$i]['function']
."()";
break;
}
$i++;
}
return $class;
}
}
?>