Skip to content

Commit

Permalink
#6622 Logger refactoring: moved the database writer to a dedicated class
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Nov 28, 2014
1 parent eff26ce commit f2b4a20
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 56 deletions.
60 changes: 4 additions & 56 deletions core/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -333,7 +334,8 @@ private function getAvailableWriters()

$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;
}

Expand All @@ -347,19 +349,6 @@ public function getLogLevel()
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)) {
Expand Down Expand Up @@ -462,45 +451,4 @@ private function writeErrorToStandardErrorOutput($message)
$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;
}
}
61 changes: 61 additions & 0 deletions core/Log/DatabaseBackend.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit f2b4a20

Please sign in to comment.