Nymethny's avatar

Can you create log channels dynamically?

Is there any way to create log channels dynamically, or have one log channel that logs to a variable file?

Right now I have multiple "services" in my app, which need their own log file. This means that currently they're all defined like this in config/logging.php:

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

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

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

Is there any way to have a single channel with a variable log file, like this:

'service' => [
    'driver' => 'single',
    'path' => storage_path('logs/{service_name}.log'),
    'level' => 'debug',
]

Or alternatively, can log channels be created dynamically without relying on what is configured in the logging.php config file? For example, create a macro for the Log facade, like Log::serviceLog() which would check if the provided channel exists, and create it if it does not.

0 likes
3 replies
gych's avatar

@Nymethny No problem Don't forget to close your thread by selecting the best answer :)

If you have more questions don't hesitate to reach out

Please or to participate in this conversation.