MonologLoggerChannelFactory.php in Monolog 8
File
src/Logger/MonologLoggerChannelFactory.php
View source
<?php
namespace Drupal\monolog\Logger;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class MonologLoggerChannelFactory implements LoggerChannelFactoryInterface, ContainerAwareInterface {
use ContainerAwareTrait;
protected $channels = array();
protected $enabledProcessors;
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];
}
public function addLogger(LoggerInterface $logger, $priority = 0) {
}
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) {
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) {
$h = $this->container
->get('monolog.handler.' . $handler);
if ($formatter && $this->container
->has('monolog.formatter.' . $formatter)) {
$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;
}
}