@@ -467,37 +467,52 @@ protected function interpolate($message, array $context = [])
467467 return strtr ($ message , $ replace );
468468 }
469469
470- //--------------------------------------------------------------------
471470
472471 /**
473- * Determines the current file/line that the log method was called from.
474- * by analyzing the backtrace.
472+ * Determines the file and line that the logging call
473+ * was made from by analyzing the backtrace.
474+ * Find the earliest stack frame that is part of our logging system.
475475 *
476476 * @return array
477477 */
478478 public function determineFile (): array
479479 {
480- // Determine the file and line by finding the first
481- // backtrace that is not part of our logging system.
482- $ trace = debug_backtrace ();
483- $ file = null ;
484- $ line = null ;
480+ $ logFunctions = [
481+ 'log_message ' ,
482+ 'log ' ,
483+ 'error ' ,
484+ 'debug ' ,
485+ 'info ' ,
486+ 'warning ' ,
487+ 'critical ' ,
488+ 'emergency ' ,
489+ 'alert ' ,
490+ 'notice ' ,
491+ ];
492+
493+ // Generate Backtrace info
494+ $ trace = \debug_backtrace (false );
485495
486- foreach ($ trace as $ row )
496+ // So we search from the bottom (earliest) of the stack frames
497+ $ stackFrames = \array_reverse ($ trace );
498+
499+ // Find the first reference to a Logger class method
500+ foreach ($ stackFrames as $ frame )
487501 {
488- if (in_array ($ row ['function ' ], [ ' interpolate ' , ' determineFile ' , ' log ' , ' log_message ' ] ))
502+ if (\ in_array ($ frame ['function ' ], $ logFunctions ))
489503 {
490- continue ;
504+ $ file = isset ($ frame ['file ' ]) ? $ this ->cleanFileNames ($ frame ['file ' ]) : 'unknown ' ;
505+ $ line = $ frame ['line ' ] ?? 'unknown ' ;
506+ return [
507+ $ file ,
508+ $ line ,
509+ ];
491510 }
492-
493- $ file = $ row ['file ' ] ?? isset ($ row ['object ' ]) ? get_class ($ row ['object ' ]) : 'unknown ' ;
494- $ line = $ row ['line ' ] ?? $ row ['function ' ] ?? 'unknown ' ;
495- break ;
496511 }
497512
498513 return [
499- $ file ,
500- $ line ,
514+ ' unknown ' ,
515+ ' unknown ' ,
501516 ];
502517 }
503518
0 commit comments