diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-11-28 05:04:19 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-11-28 05:04:19 +0300 |
commit | f2b4a2027a2d66363db7cd49de7cbcc660d121f6 (patch) | |
tree | b3e22d99a300ea039a58070bb8dbee6c3e1d3821 /core | |
parent | eff26ce0ff36d85e83803394525df0ad7a16b204 (diff) |
#6622 Logger refactoring: moved the database writer to a dedicated class
Diffstat (limited to 'core')
-rw-r--r-- | core/Log.php | 60 | ||||
-rw-r--r-- | core/Log/DatabaseBackend.php | 61 |
2 files changed, 65 insertions, 56 deletions
diff --git a/core/Log.php b/core/Log.php index 14bf703d46..5b1d41c20a 100644 --- a/core/Log.php +++ b/core/Log.php @@ -4,12 +4,13 @@ * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later - * */ + namespace Piwik; use Piwik\Container\StaticContainer; use Piwik\Db; +use Piwik\Log\DatabaseBackend; use Piwik\Log\FileBackend; use Piwik\Log\ScreenBackend; @@ -333,7 +334,8 @@ class Log extends Singleton $writers['file'] = new FileBackend($this->logMessageFormat, $this->logToFilePath); $writers['screen'] = new ScreenBackend($this->logMessageFormat); - $writers['database'] = array($this, 'logToDatabase'); + $writers['database'] = new DatabaseBackend($this->logMessageFormat); + return $writers; } @@ -347,19 +349,6 @@ class Log extends Singleton return $this->currentLogLevel; } - private function logToDatabase($level, $tag, $datetime, $message) - { - $message = $this->getMessageFormattedDatabase($level, $tag, $datetime, $message); - if (empty($message)) { - return; - } - - $sql = "INSERT INTO " . Common::prefixTable('logger_message') - . " (tag, timestamp, level, message)" - . " VALUES (?, ?, ?, ?)"; - Db::query($sql, array($tag, $datetime, self::getStringLevel($level), (string)$message)); - } - private function doLog($level, $message, $sprintfParams = array()) { if (!$this->shouldLoggerLog($level)) { @@ -462,45 +451,4 @@ class Log extends Singleton $fe = fopen('php://stderr', 'w'); fwrite($fe, $message); } - - /** - * @param $level - * @param $tag - * @param $datetime - * @param $message - * @return string - */ - private function getMessageFormattedDatabase($level, $tag, $datetime, $message) - { - if (is_string($message)) { - $message = $this->formatMessage($level, $tag, $datetime, $message); - } else { - $logger = $this; - - /** - * Triggered when trying to log an object to a database table. Plugins can use - * this event to convert objects to strings before they are logged. - * - * **Example** - * - * public function formatDatabaseMessage(&$message, $level, $tag, $datetime, $logger) { - * if ($message instanceof MyCustomDebugInfo) { - * $message = $message->formatForDatabase(); - * } - * } - * - * @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_DATABASE_MESSAGE_EVENT, array(&$message, $level, $tag, $datetime, $logger)); - } - $message = trim($message); - return $message; - } } diff --git a/core/Log/DatabaseBackend.php b/core/Log/DatabaseBackend.php new file mode 100644 index 0000000000..210ae088c7 --- /dev/null +++ b/core/Log/DatabaseBackend.php @@ -0,0 +1,61 @@ +<?php + +namespace Piwik\Log; + +use Piwik\Common; +use Piwik\Db; +use Piwik\Log; +use Piwik\Piwik; + +/** + * Writes log to database. + */ +class DatabaseBackend extends Backend +{ + public function __invoke($level, $tag, $datetime, $message, Log $logger) + { + $message = $this->getMessageFormattedDatabase($level, $tag, $datetime, $message, $logger); + if (empty($message)) { + return; + } + + $sql = "INSERT INTO " . Common::prefixTable('logger_message') + . " (tag, timestamp, level, message)" + . " VALUES (?, ?, ?, ?)"; + + Db::query($sql, array($tag, $datetime, self::getStringLevel($level), (string)$message)); + } + + private function getMessageFormattedDatabase($level, $tag, $datetime, $message, $logger) + { + if (is_string($message)) { + $message = $this->formatMessage($level, $tag, $datetime, $message); + } else { + /** + * Triggered when trying to log an object to a database table. Plugins can use + * this event to convert objects to strings before they are logged. + * + * **Example** + * + * public function formatDatabaseMessage(&$message, $level, $tag, $datetime, $logger) { + * if ($message instanceof MyCustomDebugInfo) { + * $message = $message->formatForDatabase(); + * } + * } + * + * @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(Log::FORMAT_DATABASE_MESSAGE_EVENT, array(&$message, $level, $tag, $datetime, $logger)); + } + $message = trim($message); + + return $message; + } +} |