diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-11-27 08:45:08 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-11-28 01:58:56 +0300 |
commit | d98e21e2a1934eef1bd9f6c241588e33ddbedcdb (patch) | |
tree | 2ab27fc6320d4831570fb3abcb0c74272f84c9c7 /core | |
parent | e00732e1ea29900860671a5b022d4450377776f2 (diff) |
Logger refactoring, 1st step: separate config from logic
Diffstat (limited to 'core')
-rw-r--r-- | core/Container/StaticContainer.php | 5 | ||||
-rw-r--r-- | core/Log.php | 147 | ||||
-rw-r--r-- | core/Log/LoggerFactory.php | 132 |
3 files changed, 164 insertions, 120 deletions
diff --git a/core/Container/StaticContainer.php b/core/Container/StaticContainer.php index 5948ec1c78..39a6552ef6 100644 --- a/core/Container/StaticContainer.php +++ b/core/Container/StaticContainer.php @@ -39,6 +39,11 @@ class StaticContainer return self::$container; } + public static function reset() + { + self::$container = null; + } + /** * @link http://php-di.org/doc/container-configuration.html */ diff --git a/core/Log.php b/core/Log.php index 1315362c38..4a047965c9 100644 --- a/core/Log.php +++ b/core/Log.php @@ -98,8 +98,6 @@ use Piwik\Db; * $debugInfo = new MyDebugInfo($unexpectedError, $myThirdPartyServiceClient); * Log::debug($debugInfo); * } - * - * @method static \Piwik\Log getInstance() */ class Log extends Singleton { @@ -126,6 +124,13 @@ class Log extends Singleton const GET_AVAILABLE_WRITERS_EVENT = 'Log.getAvailableWriters'; /** + * Singleton instance. + * + * @var Log + */ + private static $instance; + + /** * The current logging level. Everything of equal or greater priority will be logged. * Everything else will be ignored. * @@ -147,7 +152,7 @@ class Log extends Singleton * * @var string */ - private $logMessageFormat = "%level% %tag%[%datetime%] %message%"; + private $logMessageFormat; /** * If we're logging to a file, this is the path to the file to log to. @@ -163,17 +168,26 @@ class Log extends Singleton */ private $loggingToScreen; - /** - * Constructor. - */ - protected function __construct() + public static function getInstance() + { + if (self::$instance === null) { + self::$instance = StaticContainer::getContainer()->get(__CLASS__); + } + return self::$instance; + } + public static function unsetInstance() + { + self::$instance = null; + } + public static function setSingletonInstance($instance) { - $logConfig = Config::getInstance()->log; - $this->setCurrentLogLevelFromConfig($logConfig); - $this->setLogWritersFromConfig($logConfig); - $this->setLogFilePathFromConfig($logConfig); - $this->setStringLogMessageFormat($logConfig); - $this->disableLoggingBasedOnConfig($logConfig); + self::$instance = $instance; + } + + public function __construct($logMessageFormat, $logToFilePath) + { + $this->logMessageFormat = $logMessageFormat; + $this->logToFilePath = $logToFilePath; } /** @@ -262,20 +276,6 @@ class Log extends Singleton ); } - private function setLogWritersFromConfig($logConfig) - { - // set the log writers - $logWriters = @$logConfig[self::LOG_WRITERS_CONFIG_OPTION]; - if (empty($logWriters)) { - return; - } - - $logWriters = array_map('trim', $logWriters); - foreach ($logWriters as $writerName) { - $this->addLogWriter($writerName); - } - } - public function addLogWriter($writerName) { if (array_key_exists($writerName, $this->writers)) { @@ -291,57 +291,6 @@ class Log extends Singleton $this->writers[$writerName] = $availableWritersByName[$writerName]; } - private function setCurrentLogLevelFromConfig($logConfig) - { - if (!empty($logConfig[self::LOG_LEVEL_CONFIG_OPTION])) { - $logLevel = $this->getLogLevelFromStringName($logConfig[self::LOG_LEVEL_CONFIG_OPTION]); - - if ($logLevel >= self::NONE // sanity check - && $logLevel <= self::VERBOSE - ) { - $this->setLogLevel($logLevel); - } - } - } - - private function setStringLogMessageFormat($logConfig) - { - if (isset($logConfig['string_message_format'])) { - $this->logMessageFormat = $logConfig['string_message_format']; - } - } - - private function setLogFilePathFromConfig($logConfig) - { - $logPath = @$logConfig[self::LOGGER_FILE_PATH_CONFIG_OPTION]; - - // Absolute path - if (strpos($logPath, '/') === 0) { - $this->logToFilePath = $logPath; - return; - } - - // Remove 'tmp/' at the beginning - if (strpos($logPath, 'tmp/') === 0) { - $logPath = substr($logPath, strlen('tmp')); - } - - if (empty($logPath)) { - $logPath = $this->getDefaultFileLogPath(); - } - - $logPath = StaticContainer::getContainer()->get('path.tmp') . $logPath; - if (is_dir($logPath)) { - $logPath .= '/piwik.log'; - } - $this->logToFilePath = $logPath; - } - - private function getDefaultFileLogPath() - { - return '/logs/piwik.log'; - } - private function getAvailableWriters() { $writers = array(); @@ -488,48 +437,6 @@ class Log extends Singleton return $level <= $this->currentLogLevel; } - private function disableLoggingBasedOnConfig($logConfig) - { - $disableLogging = false; - - if (!empty($logConfig['log_only_when_cli']) - && !Common::isPhpCliMode() - ) { - $disableLogging = true; - } - - if (!empty($logConfig['log_only_when_debug_parameter']) - && !isset($_REQUEST['debug']) - ) { - $disableLogging = true; - } - - if ($disableLogging) { - $this->currentLogLevel = self::NONE; - } - } - - private function getLogLevelFromStringName($name) - { - $name = strtoupper($name); - switch ($name) { - case 'NONE': - return self::NONE; - case 'ERROR': - return self::ERROR; - case 'WARN': - return self::WARN; - case 'INFO': - return self::INFO; - case 'DEBUG': - return self::DEBUG; - case 'VERBOSE': - return self::VERBOSE; - default: - return -1; - } - } - private function getStringLevel($level) { static $levelToName = array( diff --git a/core/Log/LoggerFactory.php b/core/Log/LoggerFactory.php new file mode 100644 index 0000000000..200b16f34d --- /dev/null +++ b/core/Log/LoggerFactory.php @@ -0,0 +1,132 @@ +<?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\Common; +use Piwik\Config; +use Piwik\Log; + +class LoggerFactory +{ + /** + * @param ContainerInterface $container + * @return Log + */ + public static function createLogger(ContainerInterface $container) + { + $logConfig = Config::getInstance()->log; + + $logFilePath = self::getLogFilePath($logConfig, $container); + + $logger = new Log($container->get('log.format'), $logFilePath); + + self::setCurrentLogLevelFromConfig($logger, $logConfig); + self::setLogWritersFromConfig($logger, $logConfig); + self::disableLoggingBasedOnConfig($logger, $logConfig); + + return $logger; + } + + private static function setCurrentLogLevelFromConfig(Log $logger, $logConfig) + { + if (!empty($logConfig[Log::LOG_LEVEL_CONFIG_OPTION])) { + $logLevel = self::getLogLevelFromStringName($logConfig[Log::LOG_LEVEL_CONFIG_OPTION]); + + if ($logLevel >= Log::NONE // sanity check + && $logLevel <= Log::VERBOSE + ) { + $logger->setLogLevel($logLevel); + } + } + } + + private static function setLogWritersFromConfig(Log $logger, $logConfig) + { + // set the log writers + $logWriters = @$logConfig[Log::LOG_WRITERS_CONFIG_OPTION]; + if (empty($logWriters)) { + return; + } + + $logWriters = array_map('trim', $logWriters); + foreach ($logWriters as $writerName) { + $logger->addLogWriter($writerName); + } + } + + private static function getLogFilePath($logConfig, ContainerInterface $container) + { + $logPath = @$logConfig[Log::LOGGER_FILE_PATH_CONFIG_OPTION]; + + // Absolute path + if (strpos($logPath, '/') === 0) { + return $logPath; + } + + // Remove 'tmp/' at the beginning + if (strpos($logPath, 'tmp/') === 0) { + $logPath = substr($logPath, strlen('tmp')); + } + + if (empty($logPath)) { + return self::getDefaultFileLogPath(); + } + + $logPath = $container->get('path.tmp') . $logPath; + if (is_dir($logPath)) { + $logPath .= '/piwik.log'; + } + + return $logPath; + } + + private static function disableLoggingBasedOnConfig(Log $logger, $logConfig) + { + $disableLogging = false; + + if (!empty($logConfig['log_only_when_cli']) && !Common::isPhpCliMode()) { + $disableLogging = true; + } + + if (!empty($logConfig['log_only_when_debug_parameter']) && !isset($_REQUEST['debug'])) { + $disableLogging = true; + } + + if ($disableLogging) { + $logger->setLogLevel(Log::NONE); + } + } + + private static function getDefaultFileLogPath() + { + return '/logs/piwik.log'; + } + + private static function getLogLevelFromStringName($name) + { + $name = strtoupper($name); + switch ($name) { + case 'NONE': + return Log::NONE; + case 'ERROR': + return Log::ERROR; + case 'WARN': + return Log::WARN; + case 'INFO': + return Log::INFO; + case 'DEBUG': + return Log::DEBUG; + case 'VERBOSE': + return Log::VERBOSE; + default: + return -1; + } + } +} |