Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - pass line number automatically as function argument

Tags:

php

I am writing Logger class in PHP. Is there a way, how to automatically pass line number and script file from call?

Sample:

Logger::Log("message", __LINE__, __FILE__)

I want to pass __LINE__ and __FILE__ automatically, without need of writing it. I have tried to make

public static function Log($msg, $l = __LINE__, $f = __FILE__) { ... }

but this pass line number and file of Log method. Is this even possible in PHP?

like image 650
Martin Perry Avatar asked Aug 08 '26 13:08

Martin Perry


1 Answers

You can use debug_backtrace inside of Logger::Log to retrieve a call stack, which includes the file and line number of the code that called Logger::Log. That's a sensible thing to include in loggers in general.

like image 97
deceze Avatar answered Aug 11 '26 04:08

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!