diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-01 02:14:10 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-01 02:14:10 +0300 |
commit | 528d5dcfe266e4b18f87b9fb51cfcb8e7782f8cc (patch) | |
tree | 91ee8ab3bae1daac646e777f3f32093ab2290393 | |
parent | 3563fe0c319d8925af57cbd26695d1bc4b63d2ef (diff) |
#6622 Logger refactoring: moved "log message processing" into "Processor" objects (to prepare the transition to Monolog)
-rw-r--r-- | config/global.php | 3 | ||||
-rw-r--r-- | core/Log.php | 25 | ||||
-rw-r--r-- | core/Log/LoggerFactory.php | 4 | ||||
-rw-r--r-- | core/Log/Processor/SprintfProcessor.php | 37 |
4 files changed, 56 insertions, 13 deletions
diff --git a/config/global.php b/config/global.php index 359519cab9..fc30ae5889 100644 --- a/config/global.php +++ b/config/global.php @@ -29,5 +29,8 @@ return array( } return '%level% %tag%[%datetime%] %message%'; }), + 'log.processors' => array( + DI\link('Piwik\Log\Processor\SprintfProcessor'), + ), ); diff --git a/core/Log.php b/core/Log.php index a59d6857f6..54844f7dbb 100644 --- a/core/Log.php +++ b/core/Log.php @@ -140,10 +140,17 @@ class Log extends Singleton private $currentLogLevel = self::WARN; /** + * Processors process log messages before they are being sent to backends. + * + * @var callable[] + */ + private $processors = array(); + + /** * The array of callbacks executed when logging a message. Each callback writes a log * message to a logging backend. * - * @var array + * @var callable[] */ private $writers = array(); @@ -175,12 +182,14 @@ class Log extends Singleton * @param callable[] $writers * @param string $logMessageFormat * @param int $logLevel + * @param callable[] $processors */ - public function __construct(array $writers, $logMessageFormat, $logLevel) + public function __construct(array $writers, $logMessageFormat, $logLevel, array $processors) { $this->writers = $writers; $this->logMessageFormat = $logMessageFormat; $this->currentLogLevel = $logLevel; + $this->processors = $processors; } /** @@ -286,17 +295,9 @@ class Log extends Singleton } $datetime = date("Y-m-d H:i:s"); - if (is_string($message) - && !empty($sprintfParams) - ) { - // handle array sprintf parameters - foreach ($sprintfParams as &$param) { - if (is_array($param)) { - $param = json_encode($param); - } - } - $message = vsprintf($message, $sprintfParams); + foreach ($this->processors as $processor) { + $message = $processor($message, $sprintfParams, $level); } if (version_compare(phpversion(), '5.3.6', '>=')) { diff --git a/core/Log/LoggerFactory.php b/core/Log/LoggerFactory.php index c66e1bac81..3e48c63fdf 100644 --- a/core/Log/LoggerFactory.php +++ b/core/Log/LoggerFactory.php @@ -15,6 +15,7 @@ use Piwik\Log; use Piwik\Log\Backend\DatabaseBackend; use Piwik\Log\Backend\FileBackend; use Piwik\Log\Backend\ScreenBackend; +use Piwik\Log\Processor\SprintfProcessor; use Piwik\Piwik; class LoggerFactory @@ -33,8 +34,9 @@ class LoggerFactory $logFilePath = self::getLogFilePath($logConfig, $container); $logLevel = self::getLogLevel($logConfig, $container); $writers = self::getLogWriters($logConfig, $messageFormat, $logFilePath); + $processors = $container->get('log.processors'); - return new Log($writers, $messageFormat, $logLevel); + return new Log($writers, $messageFormat, $logLevel, $processors); } private static function getLogLevel($logConfig, ContainerInterface $container) diff --git a/core/Log/Processor/SprintfProcessor.php b/core/Log/Processor/SprintfProcessor.php new file mode 100644 index 0000000000..8ec0d61db2 --- /dev/null +++ b/core/Log/Processor/SprintfProcessor.php @@ -0,0 +1,37 @@ +<?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\Processor; + +/** + * Processes a log message using `sprintf()`. + */ +class SprintfProcessor +{ + public function __invoke($message, array $parameters = array(), $level) + { + if (is_string($message) && !empty($parameters)) { + $parameters = $this->ensureParametersAreStrings($parameters); + + $message = vsprintf($message, $parameters); + } + + return $message; + } + + private function ensureParametersAreStrings(array $parameters) + { + foreach ($parameters as &$param) { + if (is_array($param)) { + $param = json_encode($param); + } + } + + return $parameters; + } +} |