diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-05 01:40:32 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-05 01:40:32 +0300 |
commit | 2ffa749ea2057bbaeeef59f0e08cc2c288cbbfb9 (patch) | |
tree | 1a73b5831ee1d9543b23e7f3d02f06f9c946a584 /core | |
parent | 28a07a8d13a05f923b3d231283261e484078de72 (diff) |
#6622 Logger refactoring: using Monolog!
`Piwik\Log` is now a wrapper on top of Monolog. The real logger can now be injected using `Psr\Log\LoggerInterface`.
Diffstat (limited to 'core')
-rw-r--r-- | core/Log.php | 86 | ||||
-rw-r--r-- | core/Log/Backend/DatabaseBackend.php | 3 | ||||
-rw-r--r-- | core/Log/Backend/FileBackend.php | 1 | ||||
-rw-r--r-- | core/Log/Backend/StdErrBackend.php | 1 | ||||
-rw-r--r-- | core/Log/Backend/StdOutBackend.php | 1 | ||||
-rw-r--r-- | core/Log/Formatter/AddRequestIdFormatter.php | 1 | ||||
-rw-r--r-- | core/Log/Formatter/Formatter.php | 1 | ||||
-rw-r--r-- | core/Log/Formatter/HtmlPreFormatter.php | 1 | ||||
-rw-r--r-- | core/Log/Formatter/LineMessageFormatter.php | 8 |
9 files changed, 19 insertions, 84 deletions
diff --git a/core/Log.php b/core/Log.php index 846949e0f3..9ce5d5f8a2 100644 --- a/core/Log.php +++ b/core/Log.php @@ -8,11 +8,9 @@ namespace Piwik; -use Monolog\Handler\AbstractHandler; -use Monolog\Handler\HandlerInterface; use Monolog\Logger; use Piwik\Container\StaticContainer; -use Piwik\Db; +use Psr\Log\LoggerInterface; /** * Logging utility class. @@ -129,27 +127,9 @@ class Log extends Singleton private static $instance; /** - * The current logging level. Everything of equal or greater priority will be logged. - * Everything else will be ignored. - * - * @var int - */ - private $currentLogLevel = self::WARN; - - /** - * Processors process log messages before they are being sent to backends. - * - * @var callable[] + * @var LoggerInterface */ - private $processors = array(); - - /** - * The array of handlers called when logging a message. Each handler writes a log - * message to a logging backend. - * - * @var HandlerInterface[] - */ - private $handlers = array(); + private $logger; public static function getInstance() { @@ -168,15 +148,11 @@ class Log extends Singleton } /** - * @param HandlerInterface[] $handlers - * @param int $logLevel - * @param callable[] $processors + * @param LoggerInterface $logger */ - public function __construct(array $handlers, $logLevel, array $processors) + public function __construct(LoggerInterface $logger) { - $this->handlers = $handlers; - $this->currentLogLevel = $logLevel; - $this->processors = $processors; + $this->logger = $logger; } /** @@ -191,7 +167,7 @@ class Log extends Singleton */ public static function error($message /* ... */) { - self::logMessage(self::ERROR, $message, array_slice(func_get_args(), 1)); + self::logMessage(Logger::ERROR, $message, array_slice(func_get_args(), 1)); } /** @@ -203,7 +179,7 @@ class Log extends Singleton */ public static function warning($message /* ... */) { - self::logMessage(self::WARN, $message, array_slice(func_get_args(), 1)); + self::logMessage(Logger::WARNING, $message, array_slice(func_get_args(), 1)); } /** @@ -215,7 +191,7 @@ class Log extends Singleton */ public static function info($message /* ... */) { - self::logMessage(self::INFO, $message, array_slice(func_get_args(), 1)); + self::logMessage(Logger::INFO, $message, array_slice(func_get_args(), 1)); } /** @@ -227,7 +203,7 @@ class Log extends Singleton */ public static function debug($message /* ... */) { - self::logMessage(self::DEBUG, $message, array_slice(func_get_args(), 1)); + self::logMessage(Logger::DEBUG, $message, array_slice(func_get_args(), 1)); } /** @@ -239,7 +215,7 @@ class Log extends Singleton */ public static function verbose($message /* ... */) { - self::logMessage(self::VERBOSE, $message, array_slice(func_get_args(), 1)); + self::logMessage(Logger::DEBUG, $message, array_slice(func_get_args(), 1)); } /** @@ -250,9 +226,11 @@ class Log extends Singleton { } + /** + * @deprecated Will be removed + */ public function getLogLevel() { - return $this->currentLogLevel; } private function doLog($level, $message, $parameters = array()) @@ -262,28 +240,11 @@ class Log extends Singleton $parameters['exception'] = $message; $message = $message->getMessage(); } - - // Create a record similar to Monolog to ease future transition - $record = array( - 'message' => $message, - 'context' => $parameters, - 'channel' => 'piwik', - 'level' => $this->getMonologLevel($level), - 'level_name' => self::getStringLevel($level), - 'time' => new \DateTime(), - 'datetime' => new \DateTime(), - 'extra' => array(), - ); - - foreach ($this->processors as $processor) { - $record = $processor($record); + if (! is_string($message)) { + throw new \InvalidArgumentException('Trying to log a message that is not a string'); } - foreach ($this->handlers as $handler) { - if ($handler->isHandling($record)) { - $handler->handle($record); - } - } + $this->logger->log($level, $message, $parameters); } private static function logMessage($level, $message, $parameters) @@ -291,19 +252,6 @@ class Log extends Singleton self::getInstance()->doLog($level, $message, $parameters); } - private function getStringLevel($level) - { - static $levelToName = array( - Log::NONE => 'NONE', - Log::ERROR => 'ERROR', - Log::WARN => 'WARN', - Log::INFO => 'INFO', - Log::DEBUG => 'DEBUG', - Log::VERBOSE => 'VERBOSE' - ); - return $levelToName[$level]; - } - public static function getMonologLevel($level) { switch ($level) { diff --git a/core/Log/Backend/DatabaseBackend.php b/core/Log/Backend/DatabaseBackend.php index 8b25c3afa0..66811aac2f 100644 --- a/core/Log/Backend/DatabaseBackend.php +++ b/core/Log/Backend/DatabaseBackend.php @@ -11,7 +11,6 @@ namespace Piwik\Log\Backend; use Monolog\Handler\AbstractProcessingHandler; use Piwik\Common; use Piwik\Db; -use Piwik\Log; /** * Writes log to database. @@ -26,6 +25,6 @@ class DatabaseBackend extends AbstractProcessingHandler . " (tag, timestamp, level, message)" . " VALUES (?, ?, ?, ?)"; - Db::query($sql, array($record['extra']['class'], $record['time']->format('Y-m-d H:i:s'), $record['level_name'], (string) $record['message'])); + Db::query($sql, array($record['extra']['class'], $record['datetime']->format('Y-m-d H:i:s'), $record['level_name'], (string) $record['message'])); } } diff --git a/core/Log/Backend/FileBackend.php b/core/Log/Backend/FileBackend.php index 2a8a117ebe..21f810480a 100644 --- a/core/Log/Backend/FileBackend.php +++ b/core/Log/Backend/FileBackend.php @@ -11,7 +11,6 @@ namespace Piwik\Log\Backend; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; use Piwik\Filechecks; -use Piwik\Log; /** * Writes log to file. diff --git a/core/Log/Backend/StdErrBackend.php b/core/Log/Backend/StdErrBackend.php index a173823dba..ed2a88a380 100644 --- a/core/Log/Backend/StdErrBackend.php +++ b/core/Log/Backend/StdErrBackend.php @@ -11,7 +11,6 @@ namespace Piwik\Log\Backend; use Monolog\Formatter\FormatterInterface; use Monolog\Handler\AbstractProcessingHandler; use Monolog\Logger; -use Piwik\Log; /** * Writes log to stderr. diff --git a/core/Log/Backend/StdOutBackend.php b/core/Log/Backend/StdOutBackend.php index 0b704a12c8..ba306b16a5 100644 --- a/core/Log/Backend/StdOutBackend.php +++ b/core/Log/Backend/StdOutBackend.php @@ -9,7 +9,6 @@ namespace Piwik\Log\Backend; use Monolog\Handler\AbstractProcessingHandler; -use Piwik\Log; /** * Writes log to stdout. diff --git a/core/Log/Formatter/AddRequestIdFormatter.php b/core/Log/Formatter/AddRequestIdFormatter.php index 9d027f5886..d9f0b5dfac 100644 --- a/core/Log/Formatter/AddRequestIdFormatter.php +++ b/core/Log/Formatter/AddRequestIdFormatter.php @@ -9,7 +9,6 @@ namespace Piwik\Log\Formatter; use Piwik\Common; -use Piwik\Log; /** * Adds a unique "request id" to the log message to follow log entries for each HTTP request. diff --git a/core/Log/Formatter/Formatter.php b/core/Log/Formatter/Formatter.php index 03c3d5a150..694d087907 100644 --- a/core/Log/Formatter/Formatter.php +++ b/core/Log/Formatter/Formatter.php @@ -9,7 +9,6 @@ namespace Piwik\Log\Formatter; use Monolog\Formatter\FormatterInterface; -use Piwik\Log; /** * Formats a log message. diff --git a/core/Log/Formatter/HtmlPreFormatter.php b/core/Log/Formatter/HtmlPreFormatter.php index 878304e8fa..fd559ce72b 100644 --- a/core/Log/Formatter/HtmlPreFormatter.php +++ b/core/Log/Formatter/HtmlPreFormatter.php @@ -9,7 +9,6 @@ namespace Piwik\Log\Formatter; use Piwik\Common; -use Piwik\Log; /** * Formats the message into `<pre></pre>` HTML tags. diff --git a/core/Log/Formatter/LineMessageFormatter.php b/core/Log/Formatter/LineMessageFormatter.php index 14096ce7ac..94b9dabf24 100644 --- a/core/Log/Formatter/LineMessageFormatter.php +++ b/core/Log/Formatter/LineMessageFormatter.php @@ -8,8 +8,6 @@ namespace Piwik\Log\Formatter; -use Piwik\Log; - /** * Formats a log message into a single line of text. */ @@ -30,13 +28,9 @@ class LineMessageFormatter extends Formatter public function format(array $record) { - if (! is_string($record['message'])) { - throw new \InvalidArgumentException('Trying to log a message that is not a string'); - } - $record['message'] = str_replace( array("%tag%", "%message%", "%datetime%", "%level%"), - array($record['extra']['class'], trim($record['message']), $record['time']->format('Y-m-d H:i:s'), $record['level_name']), + array($record['extra']['class'], trim($record['message']), $record['datetime']->format('Y-m-d H:i:s'), $record['level_name']), $this->logMessageFormat ); |