class MonologLoggerChannelFactory in Monolog 8
Same name and namespace in other branches
- 2.x src/Logger/MonologLoggerChannelFactory.php \Drupal\monolog\Logger\MonologLoggerChannelFactory
Defines a factory for logging channels.
Hierarchy
- class \Drupal\monolog\Logger\MonologLoggerChannelFactory implements \Symfony\Component\DependencyInjection\ContainerAwareInterface, LoggerChannelFactoryInterface uses \Symfony\Component\DependencyInjection\ContainerAwareTrait
Expanded class hierarchy of MonologLoggerChannelFactory
File
- src/
Logger/ MonologLoggerChannelFactory.php, line 14
Namespace
Drupal\monolog\LoggerView source
class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, ContainerAwareInterface {
use ContainerAwareTrait;
/**
* Array of all instantiated logger channels keyed by channel name.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface[]
*/
protected $channels = array();
/**
* Array of enabled processors.
*
* @var array
*/
protected $enabledProcessors;
/**
* {@inheritdoc}
*/
public function get($channel) {
if (!isset($this->channels[$channel])) {
try {
$this->channels[$channel] = $this
->getChannelInstance($channel);
} catch (\InvalidArgumentException $e) {
$this->channels[$channel] = new NullLogger();
if ($this->container
->get('current_user')
->hasPermission('administer site configuration')) {
\Drupal::messenger()
->addError($e
->getMessage());
}
}
}
return $this->channels[$channel];
}
/**
* {@inheritdoc}
*/
public function addLogger(LoggerInterface $logger, $priority = 0) {
// No-op, we have handlers which are services and configured in the services.yml file.
// @see https://www.drupal.org/node/2411683
}
/**
* Factory function for Monolog loggers.
*
* @param string $channel_name
* The name the logging channel.
*
* @return \Psr\Log\LoggerInterface
*
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
protected function getChannelInstance($channel_name) {
if (!class_exists('Monolog\\Logger')) {
throw new \RuntimeException('The Monolog\\Logger class was not found. Make sure the Monolog package is installed via Composer.');
}
if (!$this->container) {
// We need the container to read parameters etc.
return new NullLogger();
}
$logger = new Logger($channel_name);
$parameters = $this->container
->getParameter('monolog.channel_handlers');
$config = array_key_exists($channel_name, $parameters) ? $parameters[$channel_name] : $parameters['default'];
$formatter = NULL;
$handlers = $config;
if (array_key_exists('handlers', $config)) {
$formatter = $config['formatter'];
$handlers = $config['handlers'];
}
foreach ($handlers as $handler) {
/** @var \Monolog\Handler\HandlerInterface $h */
$h = $this->container
->get('monolog.handler.' . $handler);
if ($formatter && $this->container
->has('monolog.formatter.' . $formatter)) {
/** @var \Monolog\Formatter\FormatterInterface $f */
$f = $this->container
->get('monolog.formatter.' . $formatter);
$h
->setFormatter($f);
}
$logger
->pushHandler($h);
}
foreach ($this->container
->getParameter('monolog.processors') as $processor) {
$logger
->pushProcessor($this->container
->get('monolog.processor.' . $processor));
}
return $logger;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MonologLoggerChannelFactory:: |
protected | property | Array of all instantiated logger channels keyed by channel name. | |
MonologLoggerChannelFactory:: |
protected | property | Array of enabled processors. | |
MonologLoggerChannelFactory:: |
public | function |
Adds a logger to all the channels. Overrides LoggerChannelFactoryInterface:: |
|
MonologLoggerChannelFactory:: |
public | function |
Retrieves the registered logger for the requested channel. Overrides LoggerChannelFactoryInterface:: |
|
MonologLoggerChannelFactory:: |
protected | function | Factory function for Monolog loggers. |