Add channels at runtime
A channel is a named Monolog instance with its own level, format, and output. Beyond the channels you pass at construction, you can register more at runtime — for a job worker that needs its own rotating file, say.
Add a channel via the factory
Once LoggerFactory::setManager() has run,
addChannel() registers an extra channel on the current manager:
use Monolog\Level;
use Flytachi\Winter\Logger\LoggerFactory;
LoggerFactory::addChannel('job', [
'level' => Level::Debug,
'format' => 'line',
'output' => 'file',
'file_path' => '/var/log/app/job.log',
'file_max' => 7,
'syslog_ident' => 'winter',
]);
// Now target it explicitly:
LoggerFactory::getLogger('App\\Job\\ReportJob', 'job')->debug('step done');output=file needs file_path
When output is 'file', file_path is required — it’s the absolute path to the log file.
The handler rotates it daily and keeps file_max files. Omitting it throws
InvalidArgumentException. See Channel config.
Add a channel on the manager directly
If you hold a LoggerManager reference, withChannel() returns a new manager with the
extra channel — the original is untouched (the manager is immutable at the config level):
$manager = $manager->withChannel('job', [
'level' => Level::Debug,
'format' => 'line',
'output' => 'file',
'file_path' => '/var/log/app/job.log',
'file_max' => 7,
'syslog_ident' => 'winter',
]);addChannel() on the factory is a thin wrapper: it calls withChannel() and stores the new
manager back, then clears the per-class cache.
Check whether a channel exists
if ($manager->hasChannel('job')) {
// ...
}Unknown-channel fallback
Ask getLogger() for a channel that isn’t registered and it silently falls back to the
current default channel — no exception:
// 'job' not registered → uses the default channel instead:
LoggerFactory::getLogger(self::class, 'job')->info('...');channel() does not fall back
LoggerFactory::channel('job') — the raw
channel accessor — throws InvalidArgumentException for an unregistered channel. Only
getLogger() degrades gracefully to the default.
Drop the channel cache
In a long-running worker that reloads config, flush() drops all built Monolog instances so
they rebuild from the current config on next use:
$manager->flush(); // cached channels dropped; rebuilt lazily on next channel()Related
- Channel config reference — every key and output target
- Framework integration — bootstrap order
- API reference → LoggerManager