diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2015-02-02 01:45:32 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2015-02-12 00:12:19 +0300 |
commit | e122a4e37b3eef21fc6fc32ed7d31c56c38d673f (patch) | |
tree | 89aa9600a70200f1e94cf0454d62d037f0e361e2 /core | |
parent | 47c0eae3f8202430ea576e09cf0ded90f783eea1 (diff) |
Moved the monolog configuration and code into a Monolog plugin
Diffstat (limited to 'core')
-rw-r--r-- | core/Log/Formatter/LineMessageFormatter.php | 75 | ||||
-rw-r--r-- | core/Log/Handler/DatabaseHandler.php | 39 | ||||
-rw-r--r-- | core/Log/Handler/FileHandler.php | 31 | ||||
-rw-r--r-- | core/Log/Handler/WebNotificationHandler.php | 51 | ||||
-rw-r--r-- | core/Log/Processor/ClassNameProcessor.php | 71 | ||||
-rw-r--r-- | core/Log/Processor/ExceptionToTextProcessor.php | 58 | ||||
-rw-r--r-- | core/Log/Processor/RequestIdProcessor.php | 34 | ||||
-rw-r--r-- | core/Log/Processor/SprintfProcessor.php | 40 | ||||
-rw-r--r-- | core/Updates/2.11.0-b5.php | 23 | ||||
-rw-r--r-- | core/Version.php | 2 |
10 files changed, 24 insertions, 400 deletions
diff --git a/core/Log/Formatter/LineMessageFormatter.php b/core/Log/Formatter/LineMessageFormatter.php deleted file mode 100644 index 7c38fc32df..0000000000 --- a/core/Log/Formatter/LineMessageFormatter.php +++ /dev/null @@ -1,75 +0,0 @@ -<?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\Formatter; - -use Monolog\Formatter\FormatterInterface; - -/** - * Formats a log message into a line of text using our custom Piwik log format. - */ -class LineMessageFormatter implements FormatterInterface -{ - /** - * The log message format string that turns a tag name, date-time and message into - * one string to log. - * - * @var string - */ - private $logMessageFormat; - - /** - * @param string $logMessageFormat - */ - public function __construct($logMessageFormat) - { - $this->logMessageFormat = $logMessageFormat; - } - - public function format(array $record) - { - $class = isset($record['extra']['class']) ? $record['extra']['class'] : ''; - $date = $record['datetime']->format('Y-m-d H:i:s'); - - $message = $this->prefixMessageWithRequestId($record); - - $message = str_replace( - array('%tag%', '%message%', '%datetime%', '%level%'), - array($class, $message, $date, $record['level_name']), - $this->logMessageFormat - ); - - $message = str_replace("\n", "\n ", $message); - - $message .= "\n"; - - return $message; - } - - public function formatBatch(array $records) - { - foreach ($records as $key => $record) { - $records[$key] = $this->format($record); - } - - return $records; - } - - private function prefixMessageWithRequestId(array $record) - { - $requestId = isset($record['extra']['request_id']) ? $record['extra']['request_id'] : ''; - - $message = trim($record['message']); - - if ($requestId) { - $message = '[' . $requestId . '] ' . $message; - } - - return $message; - } -} diff --git a/core/Log/Handler/DatabaseHandler.php b/core/Log/Handler/DatabaseHandler.php deleted file mode 100644 index 1cdcd0f8b9..0000000000 --- a/core/Log/Handler/DatabaseHandler.php +++ /dev/null @@ -1,39 +0,0 @@ -<?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\Handler; - -use Monolog\Handler\AbstractProcessingHandler; -use Piwik\Common; -use Piwik\Db; - -/** - * Writes log to database. - */ -class DatabaseHandler extends AbstractProcessingHandler -{ - protected function write(array $record) - { - $sql = sprintf( - 'INSERT INTO %s (tag, timestamp, level, message) VALUES (?, ?, ?, ?)', - Common::prefixTable('logger_message') - ); - - $queryLog = Db::isQueryLogEnabled(); - Db::enableQueryLog(false); - - Db::query($sql, array( - $record['extra']['class'], - $record['datetime']->format('Y-m-d H:i:s'), - $record['level_name'], - trim($record['formatted']) - )); - - Db::enableQueryLog($queryLog); - } -} diff --git a/core/Log/Handler/FileHandler.php b/core/Log/Handler/FileHandler.php deleted file mode 100644 index 57f31610c1..0000000000 --- a/core/Log/Handler/FileHandler.php +++ /dev/null @@ -1,31 +0,0 @@ -<?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\Handler; - -use Monolog\Handler\StreamHandler; -use Piwik\Filechecks; - -/** - * Writes log to file. - * - * Extends StreamHandler to be able to have a custom exception message. - */ -class FileHandler extends StreamHandler -{ - protected function write(array $record) - { - try { - parent::write($record); - } catch (\UnexpectedValueException $e) { - throw new \Exception( - Filechecks::getErrorMessageMissingPermissions($this->url) - ); - } - } -} diff --git a/core/Log/Handler/WebNotificationHandler.php b/core/Log/Handler/WebNotificationHandler.php deleted file mode 100644 index 37004d6521..0000000000 --- a/core/Log/Handler/WebNotificationHandler.php +++ /dev/null @@ -1,51 +0,0 @@ -<?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\Handler; - -use Monolog\Handler\AbstractProcessingHandler; -use Monolog\Logger; -use Piwik\Common; -use Piwik\Notification; -use Piwik\Notification\Manager; -use Zend_Session_Exception; - -/** - * Writes log messages into HTML notification box. - */ -class WebNotificationHandler extends AbstractProcessingHandler -{ - protected function write(array $record) - { - switch ($record['level']) { - case Logger::EMERGENCY: - case Logger::ALERT: - case Logger::CRITICAL: - case Logger::ERROR: - $context = Notification::CONTEXT_ERROR; - break; - case Logger::WARNING: - $context = Notification::CONTEXT_WARNING; - break; - default: - $context = Notification::CONTEXT_INFO; - break; - } - - $message = htmlentities($record['formatted']); - - $notification = new Notification($message); - $notification->context = $context; - try { - Manager::notify(Common::getRandomString(), $notification); - } catch (Zend_Session_Exception $e) { - // Can happen if this handler is enabled in CLI - // Silently ignore the error. - } - } -} diff --git a/core/Log/Processor/ClassNameProcessor.php b/core/Log/Processor/ClassNameProcessor.php deleted file mode 100644 index a5b211d7d3..0000000000 --- a/core/Log/Processor/ClassNameProcessor.php +++ /dev/null @@ -1,71 +0,0 @@ -<?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; - -use Piwik\Plugin; - -/** - * Records the name of the class that logged. - */ -class ClassNameProcessor -{ - private $skippedClasses = array( - __CLASS__, - 'Piwik\Log', - 'Piwik\Piwik', - 'Piwik\CronArchive', - 'Monolog\Logger', - ); - - public function __invoke(array $record) - { - $record['extra']['class'] = $this->getLoggingClassName(); - - return $record; - } - - /** - * Returns the name of the plugin/class that triggered the log. - * - * @return string - */ - private function getLoggingClassName() - { - $backtrace = $this->getBacktrace(); - - $name = Plugin::getPluginNameFromBacktrace($backtrace); - - // if we can't determine the plugin, use the name of the calling class - if ($name == false) { - $name = $this->getClassNameThatIsLogging($backtrace); - } - - return $name; - } - - private function getClassNameThatIsLogging($backtrace) - { - foreach ($backtrace as $line) { - if (isset($line['class']) && !in_array($line['class'], $this->skippedClasses)) { - return $line['class']; - } - } - - return ''; - } - - private function getBacktrace() - { - if (version_compare(phpversion(), '5.3.6', '>=')) { - return debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT); - } - - return debug_backtrace(); - } -} diff --git a/core/Log/Processor/ExceptionToTextProcessor.php b/core/Log/Processor/ExceptionToTextProcessor.php deleted file mode 100644 index c3d51ae9e5..0000000000 --- a/core/Log/Processor/ExceptionToTextProcessor.php +++ /dev/null @@ -1,58 +0,0 @@ -<?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; - -use Piwik\ErrorHandler; -use Piwik\Log; - -/** - * Process a log record containing an exception to generate a textual message. - */ -class ExceptionToTextProcessor -{ - public function __invoke(array $record) - { - if (! $this->contextContainsException($record)) { - return $record; - } - - /** @var \Exception $exception */ - $exception = $record['context']['exception']; - - $record['message'] = sprintf( - "%s(%d): %s\n%s", - $exception->getFile(), - $exception->getLine(), - $this->getMessage($exception), - $this->getStackTrace($exception) - ); - - return $record; - } - - private function contextContainsException($record) - { - return isset($record['context']['exception']) - && $record['context']['exception'] instanceof \Exception; - } - - private function getMessage(\Exception $exception) - { - if ($exception instanceof \ErrorException) { - return ErrorHandler::getErrNoString($exception->getSeverity()) . ' - ' . $exception->getMessage(); - } - - return $exception->getMessage(); - } - - private function getStackTrace(\Exception $exception) - { - return Log::$debugBacktraceForTests ?: $exception->getTraceAsString(); - } -} diff --git a/core/Log/Processor/RequestIdProcessor.php b/core/Log/Processor/RequestIdProcessor.php deleted file mode 100644 index 46ae17372e..0000000000 --- a/core/Log/Processor/RequestIdProcessor.php +++ /dev/null @@ -1,34 +0,0 @@ -<?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; - -use Piwik\Common; - -/** - * Adds a unique "request id" to the log message to follow log entries for each HTTP request. - */ -class RequestIdProcessor -{ - private $currentRequestKey; - - public function __invoke(array $record) - { - if (Common::isPhpCliMode()) { - return $record; - } - - if (empty($this->currentRequestKey)) { - $this->currentRequestKey = substr(Common::generateUniqId(), 0, 5); - } - - $record['extra']['request_id'] = $this->currentRequestKey; - - return $record; - } -} diff --git a/core/Log/Processor/SprintfProcessor.php b/core/Log/Processor/SprintfProcessor.php deleted file mode 100644 index 9032f5ba7a..0000000000 --- a/core/Log/Processor/SprintfProcessor.php +++ /dev/null @@ -1,40 +0,0 @@ -<?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(array $record) - { - $message = $record['message']; - $parameters = $record['context']; - - if (is_string($message) && !empty($parameters)) { - $parameters = $this->ensureParametersAreStrings($parameters); - - $record['message'] = vsprintf($message, $parameters); - } - - return $record; - } - - private function ensureParametersAreStrings(array $parameters) - { - foreach ($parameters as &$param) { - if (is_array($param)) { - $param = json_encode($param); - } - } - - return $parameters; - } -} diff --git a/core/Updates/2.11.0-b5.php b/core/Updates/2.11.0-b5.php new file mode 100644 index 0000000000..1a75fd9f88 --- /dev/null +++ b/core/Updates/2.11.0-b5.php @@ -0,0 +1,23 @@ +<?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\Updates; + +use Piwik\Plugin\Manager; +use Piwik\Updates; + +class Updates_2_11_0_b5 extends Updates +{ + static function update() + { + try { + Manager::getInstance()->activatePlugin('Monolog'); + } catch (\Exception $e) { + } + } +} diff --git a/core/Version.php b/core/Version.php index f67f64b677..644f25ec21 100644 --- a/core/Version.php +++ b/core/Version.php @@ -20,7 +20,7 @@ final class Version * The current Piwik version. * @var string */ - const VERSION = '2.11.0-b4'; + const VERSION = '2.11.0-b5'; public function isStableVersion($version) { |