diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-11-28 05:00:56 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-11-28 05:00:56 +0300 |
commit | eff26ce0ff36d85e83803394525df0ad7a16b204 (patch) | |
tree | f7d8321126b9f987a370f623948f51f27c42c219 /core/Log.php | |
parent | 42fd548d2f911276ef2ddb05d630637461e1c4a2 (diff) |
#6622 Logger refactoring: moved the screen writer to a dedicated class
Diffstat (limited to 'core/Log.php')
-rw-r--r-- | core/Log.php | 75 |
1 files changed, 6 insertions, 69 deletions
diff --git a/core/Log.php b/core/Log.php index 0f7d4e533d..14bf703d46 100644 --- a/core/Log.php +++ b/core/Log.php @@ -11,6 +11,7 @@ namespace Piwik; use Piwik\Container\StaticContainer; use Piwik\Db; use Piwik\Log\FileBackend; +use Piwik\Log\ScreenBackend; /** * Logging utility class. @@ -331,7 +332,7 @@ class Log extends Singleton Piwik::postEvent(self::GET_AVAILABLE_WRITERS_EVENT, array(&$writers)); $writers['file'] = new FileBackend($this->logMessageFormat, $this->logToFilePath); - $writers['screen'] = array($this, 'logToScreen'); + $writers['screen'] = new ScreenBackend($this->logMessageFormat); $writers['database'] = array($this, 'logToDatabase'); return $writers; } @@ -346,16 +347,6 @@ class Log extends Singleton return $this->currentLogLevel; } - private function logToScreen($level, $tag, $datetime, $message) - { - $message = $this->getMessageFormattedScreen($level, $tag, $datetime, $message); - if (empty($message)) { - return; - } - - echo $message; - } - private function logToDatabase($level, $tag, $datetime, $message) { $message = $this->getMessageFormattedDatabase($level, $tag, $datetime, $message); @@ -411,7 +402,10 @@ class Log extends Singleton } if ($level == self::ERROR) { - $message = $this->getMessageFormattedScreen($level, $tag, $datetime, $message); + $writers = $this->getAvailableWriters(); + /** @var ScreenBackend $screenBackend */ + $screenBackend = $writers['screen']; + $message = $screenBackend->getMessageFormattedScreen($level, $tag, $datetime, $message, $this); $this->writeErrorToStandardErrorOutput($message); if (!isset($this->writers['screen'])) { echo $message; @@ -457,63 +451,6 @@ class Log extends Singleton } /** - * @param $level - * @param $tag - * @param $datetime - * @param $message - * @return string - */ - private function getMessageFormattedScreen($level, $tag, $datetime, $message) - { - static $currentRequestKey; - if (empty($currentRequestKey)) { - $currentRequestKey = substr(Common::generateUniqId(), 0, 5); - } - - if (is_string($message)) { - if (!defined('PIWIK_TEST_MODE')) { - $message = '[' . $currentRequestKey . '] ' . $message; - } - $message = $this->formatMessage($level, $tag, $datetime, $message); - - if (!Common::isPhpCliMode()) { - $message = Common::sanitizeInputValue($message); - $message = '<pre>' . $message . '</pre>'; - } - } else { - $logger = $this; - - /** - * Triggered when trying to log an object to the screen. Plugins can use - * this event to convert objects to strings before they are logged. - * - * The result of this callback can be HTML so no sanitization is done on the result. - * This means **YOU MUST SANITIZE THE MESSAGE YOURSELF** if you use this event. - * - * **Example** - * - * public function formatScreenMessage(&$message, $level, $tag, $datetime, $logger) { - * if ($message instanceof MyCustomDebugInfo) { - * $message = Common::sanitizeInputValue($message->formatForScreen()); - * } - * } - * - * @param mixed &$message The object that is being logged. Event handlers should - * check if the object is of a certain type and if it is, - * set `$message` to the string that should be logged. - * @param int $level The log level used with this log entry. - * @param string $tag The current plugin that started logging (or if no plugin, - * the current class). - * @param string $datetime Datetime of the logging call. - * @param Log $logger The Log singleton. - */ - Piwik::postEvent(self::FORMAT_SCREEN_MESSAGE_EVENT, array(&$message, $level, $tag, $datetime, $logger)); - } - $message = trim($message); - return $message . "\n"; - } - - /** * @param $message */ private function writeErrorToStandardErrorOutput($message) |