Reputation: 1435
You can define your log channels in config/logging.php
as I have already done.
Then you can send a log to your new channel like this:
Log::channel('my-channel')->error('message');
Now, when I want to catch an exception without throwing it, I usually use the report()
helper function like this:
try{
throw new Exception('message');
}catch(Exception $e){
report($e);
}
That is very useful for continuing a script after an exception is thrown, but it always logs the exception to my default logging channel.
I want to use the report()
function and specify that it should be reported to my-channel
.
I played around with app/Exceptions/Handler.php
without any luck.
Upvotes: 0
Views: 1469
Reputation: 1435
So far I decided to just make a simple class to emulate report()
instead of trying to use it directly:
<?php
namespace App\Exceptions;
use Log;
use Throwable;
class Reporter
{
/**
* Reports an exception to a specific channel.
*
* @param Throwable $e
* @param string $channel
* @param array $context
*/
public static function report(Throwable $e, $channel = 'daily', $context = [])
{
Log::channel($channel)->error(
$e->getMessage(),
array_merge(
Context::getContext(),
$context,
['exception' => $e],
)
);
}
}
And you would use it like this:
try{
throw new Exception('message');
}catch(Exception $e){
Reporter::report($e, 'my-channel');
}
You can also pass extra context to the log message as an array.
Upvotes: 4