HTDutchy
HTDutchy

Reputation: 1130

Laravel: Adding log channels outside logging.php configuration (plugin development)

I'm working on a laravel plugin/package.
We want to define our own loggers outside of the main project configuration (config/logger.php)

I have tried the following in the ServiceProvider register() function.

Based on the MonoLogger test code.

$partLogger = new Logger('vendor_part-log');
$partLogHandler = new StreamHandler(storage_path('logs/vendor/part-log.log', Logger::DEBUG));
$partLogger->pushHandler($partLogHandler);
// MonoLog Registry
Registry::addLogger($partLogger, 'vendor_part-log');

Sadly this doesn't work inside Laravel.
I also can't get the other existing loggers from Registry::

So the problem is that the new channel won't register.

Is there a different Registry in use inside Laravel or do I need an entirely different solution to achieve this?

Upvotes: 1

Views: 577

Answers (1)

HTDutchy
HTDutchy

Reputation: 1130

While we'd still like a more automated solution, we've compromised and are using a static function as source of the configuration.

In config/logging.php:

At the top replace return [... with $config = [....

And at the bottom add the following lines:

$config['channels'] = array_merge(
    $config['channels'],
    \FooVendor\Bar\Classes\Logs::getLogs(),
);

return $config;

And create the mentioned class with the following function:

    /**
     * Configs to be merged in config/logging.php
     * @return array
     */
    public static function getLogs()
    {
        return [
            'foo_partlogger' => [
                'driver' => 'single',
                'path' => storage_path('logs/foo/partlogger.log'),
                'level' => 'debug',
            ],
            'foo_second_partlogger' => [
                'driver' => 'single',
                'path' => storage_path('logs/foo/second_partlogger.log'),
                'level' => 'debug',
            ],
        ];
    }

Upvotes: 1

Related Questions