This is something I had been trying to do for a while so your question intrigued me and I think I have found a solution.
If you create a new file app\Logging\CustomizeFormatter with the following content
<?php
namespace App\Logging;
use Monolog\Logger;
use Monolog\Processor\IntrospectionProcessor;
class CustomizeFormatter
{
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
$handler->pushProcessor(new IntrospectionProcessor(Logger::DEBUG, ['Illuminate\']));
}
}
}
If you then change your active logging configuration (I'm using daily channel) to add a tap key with your custom formatter
'tap' => [App\Logging\CustomizeFormatter::class],
Then when you use the logger you will get something like the following added to your log entries
{
"file": "/.../app/Http/Controllers/MyController.php",
"line": 25,
"class": "App\Http\Controllers\MyController",
"function": "index"
}
Think that covers what you asked for, feel free to change the names/file locations to suit and the array being passed to the IntrospectionProcessor as the 2nd argument is classes to ignore (If you omit this you just get the logger class in the logs which isn't useful).