Reputation: 11
Laravel: How to change/block Monolog logging to 2 different locations
This code creates a Monolog JSON Formatter
LogServiceProvider->boot()
$filename = __DIR__.'/../json_logger.log';
// // Create the logger
$logger = new \Monolog\Logger('json_logger');
$stream_handler = new \Monolog\Handler\StreamHandler($filename);
$stream_handler->setFormatter( new \Monolog\Formatter\JsonFormatter() );
// Now add some handlers
$logger->pushHandler($stream_handler);
Log::pushHandler($stream_handler);
\Monolog\Registry::addLogger($logger);
Used like so
Log::info('My json-logger is now ready', 'file'=>__FILE__,'line'=>__LINE__]);
This will log to json_logger.log in json format
BUT will also log to storage/logs/laravel.log in standard format
I want to use JSON format exclusively in one location
Upvotes: 1
Views: 727
Reputation: 11
If you have changed the LOG_CHANNEL
channel to your chosen channel using LOG_CHANNEL=json
and you are still logging to other channels you must also edit the default channel ('stack'
) to use only your chosen channel.
FROM
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'daily'],
'ignore_exceptions' => false,
],
...
TO
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['json'],
'ignore_exceptions' => false,
],
...
Upvotes: 0
Reputation: 1652
You can create a new channel inside logging.php
'json' => [
'driver' => 'single',
'path' => storage_path('logs/json.log'),
'level' => 'debug',
'formatter' => JsonFormatter::class,
],
Change your .env if you want to use it as default channel.
LOG_CHANNEL=json
Or you can say
Log::channel('json')->debug('message');
Upvotes: 0