note 118678 added to function.debug-backtrace

From: Date: Mon, 18 Jan 2016 19:40:48 +0000
Subject: note 118678 added to function.debug-backtrace
Groups: php.notes 
Request: Send a blank email to php-notes+get-204711@lists.php.net to get a copy of this message
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; } } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 206.169.157.66 ---- Manual Page -- http://php.net/manual/en/function.debug-backtrace.php Edit -- https://master.php.net/note/edit/118678 Del: integrated -- https://master.php.net/note/delete/118678/integrated Del: useless -- https://master.php.net/note/delete/118678/useless Del: bad code -- https://master.php.net/note/delete/118678/bad+code Del: spam -- https://master.php.net/note/delete/118678/spam Del: non-english -- https://master.php.net/note/delete/118678/non-english Del: in docs -- https://master.php.net/note/delete/118678/in+docs Del: other reasons-- https://master.php.net/note/delete/118678 Reject -- https://master.php.net/note/reject/118678 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#204711) next »