From 7a9f70830a0d32fd56f381aac47d5ed64bf4ed7a Mon Sep 17 00:00:00 2001 From: Matthieu Napoli <matthieu@mnapoli.fr> Date: Thu, 4 Dec 2014 18:40:19 +1300 Subject: [PATCH] #6622 Logger refactoring: moved all the log config to DI config --- config/global.php | 33 +++++++++++++++++++- core/Log/LoggerFactory.php | 63 -------------------------------------- 2 files changed, 32 insertions(+), 64 deletions(-) delete mode 100644 core/Log/LoggerFactory.php diff --git a/config/global.php b/config/global.php index a5e74ea79e..122428a07e 100644 --- a/config/global.php +++ b/config/global.php @@ -3,6 +3,7 @@ use Interop\Container\ContainerInterface; use Piwik\Common; use Piwik\Log; +use Piwik\Log\Backend\StdErrBackend; use Piwik\Log\Formatter\AddRequestIdFormatter; use Piwik\Log\Formatter\ErrorHtmlFormatter; use Piwik\Log\Formatter\ErrorTextFormatter; @@ -30,7 +31,8 @@ return array( }), // Log - 'Piwik\Log' => DI\factory(array('Piwik\Log\LoggerFactory', 'createLogger')), + 'Piwik\Log' => DI\object() + ->constructor(DI\link('log.handlers'), DI\link('log.level.piwik'), DI\link('log.processors')), 'log.level.monolog' => DI\factory(function (ContainerInterface $c) { return Log::getMonologLevel($c->get('log.level.piwik')); }), @@ -57,6 +59,35 @@ return array( } return false; }), + 'log.handlers' => DI\factory(function (ContainerInterface $c) { + if ($c->has('old_config.log.log_writers')) { + $writerNames = $c->get('old_config.log.log_writers'); + } + if (empty($writerNames)) { + return array(); + } + + $classes = array( + 'file' => 'Piwik\Log\Backend\FileBackend', + 'screen' => 'Piwik\Log\Backend\StdOutBackend', + 'database' => 'Piwik\Log\Backend\DatabaseBackend', + ); + + $writerNames = array_map('trim', $writerNames); + $writers = array(); + foreach ($writerNames as $writerName) { + if (isset($classes[$writerName])) { + $class = $classes[$writerName]; + $writers[$writerName] = $c->get($class); + } + } + + // Always add the stderr backend + $isLoggingToStdOut = isset($writers['screen']); + $writers['stderr'] = new StdErrBackend($c->get('log.formatter.html'), $isLoggingToStdOut); + + return $writers; + }), 'log.processors' => array( DI\link('Piwik\Log\Processor\ClassNameProcessor'), DI\link('Piwik\Log\Processor\SprintfProcessor'), diff --git a/core/Log/LoggerFactory.php b/core/Log/LoggerFactory.php deleted file mode 100644 index fe783cb42c..0000000000 --- a/core/Log/LoggerFactory.php +++ /dev/null @@ -1,63 +0,0 @@ -<?php -/** - * Piwik - free/libre analytics platform - * - * @link http://piwik.org - * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - */ - -namespace Piwik\Log; - -use Interop\Container\ContainerInterface; -use Piwik\Config; -use Piwik\Log; -use Piwik\Log\Backend\StdErrBackend; - -class LoggerFactory -{ - /** - * @param ContainerInterface $container - * @return Log - */ - public static function createLogger(ContainerInterface $container) - { - $logConfig = Config::getInstance()->log; - - $logLevel = $container->get('log.level.piwik'); - $writers = self::getLogWriters($logConfig, $container); - $processors = $container->get('log.processors'); - - return new Log($writers, $logLevel, $processors); - } - - private static function getLogWriters($logConfig, ContainerInterface $container) - { - $writerNames = @$logConfig[Log::LOG_WRITERS_CONFIG_OPTION]; - - if (empty($writerNames)) { - return array(); - } - - $classes = array( - 'file' => 'Piwik\Log\Backend\FileBackend', - 'screen' => 'Piwik\Log\Backend\StdOutBackend', - 'database' => 'Piwik\Log\Backend\DatabaseBackend', - ); - - $writerNames = array_map('trim', $writerNames); - $writers = array(); - - foreach ($writerNames as $writerName) { - if (isset($classes[$writerName])) { - $class = $classes[$writerName]; - $writers[$writerName] = $container->get($class); - } - } - - // Always add the stderr backend - $isLoggingToStdOut = isset($writers['screen']); - $writers['stderr'] = new StdErrBackend($container->get('log.formatter.html'), $isLoggingToStdOut); - - return $writers; - } -} -- GitLab