Search code examples
laravellogging

create a new laravel logging channel through ServiceProvider


I am in the process of creating a Laravel package. Is it possible to create new logging channels through a ServiceProvider class?

I have manually created a new logging channel in my config/logging.php, however I would prefer if the package could do this automatically for me in the future.

Update: I have a Laravel package in which will use it's own logging stack and file. Here is the code I have in my config/logging.php right now:

'deployment' => [
    'driver' => 'single',
    'path' => storage_path('logs/deployment.log'),
    'level' => 'debug',
],

Thanks.


Solution

  • You can use app('log') to get the LogManager singleton from the service container and the public function stack(array $channels, $channel=null) method to pass in an array of channels you want to add.

    app('log')->stack([
        'channels' => [
            'test' => [
                'driver' => 'daily',
                'path' => storage_path('logs/test.log'),
                'level' => 'debug',
                'days' => 30,
            ]
        ]
    ]);