diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-01 04:15:21 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-01 04:15:21 +0300 |
commit | a726a1f907bd60945934cbf68f031c8507a3bf95 (patch) | |
tree | c783e86a71eac40fd4bb2b811feaa46bc5e0779a | |
parent | e9a73aa910650f518cb8b8229995578df5063d53 (diff) |
#6622 Logger refactoring: simplified log formatters
-rw-r--r-- | core/Log.php | 50 | ||||
-rw-r--r-- | core/Log/Formatter/ErrorTextFormatter.php | 10 | ||||
-rw-r--r-- | core/Log/Formatter/ExceptionTextFormatter.php | 10 | ||||
-rw-r--r-- | core/Log/LoggerFactory.php | 2 |
4 files changed, 10 insertions, 62 deletions
diff --git a/core/Log.php b/core/Log.php index 2892c4406d..904cdb0f83 100644 --- a/core/Log.php +++ b/core/Log.php @@ -10,8 +10,6 @@ namespace Piwik; use Piwik\Container\StaticContainer; use Piwik\Db; -use Piwik\Log\Backend\StdOutBackend; -use Piwik\Log\LoggerFactory; /** * Logging utility class. @@ -155,14 +153,6 @@ class Log extends Singleton */ private $writers = array(); - /** - * The log message format string that turns a tag name, date-time and message into - * one string to log. - * - * @var string - */ - private $logMessageFormat; - public static function getInstance() { if (self::$instance === null) { @@ -181,14 +171,12 @@ class Log extends Singleton /** * @param callable[] $writers - * @param string $logMessageFormat * @param int $logLevel * @param callable[] $processors */ - public function __construct(array $writers, $logMessageFormat, $logLevel, array $processors) + public function __construct(array $writers, $logLevel, array $processors) { $this->writers = $writers; - $this->logMessageFormat = $logMessageFormat; $this->currentLogLevel = $logLevel; $this->processors = $processors; } @@ -256,29 +244,6 @@ class Log extends Singleton self::logMessage(self::VERBOSE, $message, array_slice(func_get_args(), 1)); } - /** - * Creates log message combining logging info including a log level, tag name, - * date time, and caller-provided log message. The log message can be set through - * the `[log] string_message_format` INI config option. By default it will - * create log messages like: - * - * **LEVEL [tag:datetime] log message** - * - * @param int $level - * @param string $tag - * @param string $datetime - * @param string $message - * @return string - */ - public function formatMessage($level, $tag, $datetime, $message) - { - return str_replace( - array("%tag%", "%message%", "%datetime%", "%level%"), - array($tag, trim($message), $datetime, $this->getStringLevel($level)), - $this->logMessageFormat - ); - } - public function setLogLevel($logLevel) { $this->currentLogLevel = $logLevel; @@ -321,19 +286,6 @@ class Log extends Singleton return $level <= $this->currentLogLevel; } - private function getStringLevel($level) - { - static $levelToName = array( - self::NONE => 'NONE', - self::ERROR => 'ERROR', - self::WARN => 'WARN', - self::INFO => 'INFO', - self::DEBUG => 'DEBUG', - self::VERBOSE => 'VERBOSE' - ); - return $levelToName[$level]; - } - private function getClassNameThatIsLogging($backtrace) { foreach ($backtrace as $tracepoint) { diff --git a/core/Log/Formatter/ErrorTextFormatter.php b/core/Log/Formatter/ErrorTextFormatter.php index ba9b18d51a..6cfb5739b7 100644 --- a/core/Log/Formatter/ErrorTextFormatter.php +++ b/core/Log/Formatter/ErrorTextFormatter.php @@ -18,13 +18,11 @@ class ErrorTextFormatter extends Formatter { public function format($message, $level, $tag, $datetime, Log $logger) { - if (! $message instanceof Error) { - return $this->next($message, $level, $tag, $datetime, $logger); + if ($message instanceof Error) { + $message = $message->errfile . '(' . $message->errline . '): ' . Error::getErrNoString($message->errno) + . ' - ' . $message->errstr . "\n" . $message->backtrace; } - $message = $message->errfile . '(' . $message->errline . '): ' . Error::getErrNoString($message->errno) - . ' - ' . $message->errstr . "\n" . $message->backtrace; - - return $logger->formatMessage($level, $tag, $datetime, $message); + return $this->next($message, $level, $tag, $datetime, $logger); } } diff --git a/core/Log/Formatter/ExceptionTextFormatter.php b/core/Log/Formatter/ExceptionTextFormatter.php index 24ab88c270..0fcf5f6ea2 100644 --- a/core/Log/Formatter/ExceptionTextFormatter.php +++ b/core/Log/Formatter/ExceptionTextFormatter.php @@ -18,13 +18,11 @@ class ExceptionTextFormatter extends Formatter { public function format($message, $level, $tag, $datetime, Log $logger) { - if (! $message instanceof \Exception) { - return $this->next($message, $level, $tag, $datetime, $logger); + if ($message instanceof \Exception) { + $message = sprintf("%s(%d): %s\n%s", $message->getFile(), $message->getLine(), $message->getMessage(), + ExceptionHandler::$debugBacktraceForTests ?: $message->getTraceAsString()); } - $message = sprintf("%s(%d): %s\n%s", $message->getFile(), $message->getLine(), $message->getMessage(), - ExceptionHandler::$debugBacktraceForTests ?: $message->getTraceAsString()); - - return $logger->formatMessage($level, $tag, $datetime, $message); + return $this->next($message, $level, $tag, $datetime, $logger); } } diff --git a/core/Log/LoggerFactory.php b/core/Log/LoggerFactory.php index 22ff4a0b99..7222453f20 100644 --- a/core/Log/LoggerFactory.php +++ b/core/Log/LoggerFactory.php @@ -43,7 +43,7 @@ class LoggerFactory $writers = self::getLogWriters($logConfig, $messageFormat, $logFilePath); $processors = $container->get('log.processors'); - return new Log($writers, $messageFormat, $logLevel, $processors); + return new Log($writers, $logLevel, $processors); } private static function getLogLevel($logConfig, ContainerInterface $container) |