Reputation: 11155
I am logging as the following:
Log::debug("test");
this is the line in the file:
[2016-09-09 00:02:03] local.DEBUG: test
How can I configure the log to write line and file?
Thanks
Upvotes: 2
Views: 2832
Reputation: 4117
You can actually configure the Laravel logger to do many, many things. I've done it before, and in your case it would go something like this:
app('log')->getMonolog()->pushProcessor(function ($record) {
if (array_get($record, 'level_name') === 'DEBUG') {
// ...
}
return $record;
});
The main catch is: you are trying to append info that is virtually impossible to get consistently with PHP, namely the file and line which contain the code that triggered the logging function. In your case you are calling method using a Facade, which will resolve to Laravel's log writer, which in turn calls the Monolog package which will then trigger your processor (which is a Closure). Trying to trace your way back is a fool's errand.
So, the way I see it you have two options to do this easily:
1) Simply state file and line when logging, passing it to the Log writer as context
:
Log::debug('My debug message', ['file' => __FILE__, 'line' => __LINE__]);
2) As you probably know, the reason you usually have file and line information in your Laravel log has nothing to do with the loggers themselves, but rather with a PHP Exception's built-in __toString
method, which outputs along with the stack trace. What if we could take advantage of that to achieve what we want?
<?php
class DebugInfo extends \Exception
{
public function __toString()
{
return 'Debug info "'.$this->getMessage().'" on file '.$this->getFile().':'.$this->getLine();
}
}
That way you could simply write:
Log::debug(new DebugInfo('My debug message'));
And your log would look like this:
[2016-09-09 09:21:29] local.DEBUG: Debug info "My debug message" on file .../app/Http/Controllers/PresentationController.php:15
As a side-note, if you're looking to send messages to yourself for debugging purposes, I'd suggest trying out the Laravel DebugBar package.
Upvotes: 4