Skip to content

Commit

Permalink
#6622 Logger refactoring: added Log\Processor\ClassNameProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
mnapoli committed Dec 1, 2014
1 parent 8f82754 commit d263af9
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 46 deletions.
1 change: 1 addition & 0 deletions config/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
// Log
'Piwik\Log' => DI\factory(array('Piwik\Log\LoggerFactory', 'createLogger')),
'log.processors' => array(
DI\link('Piwik\Log\Processor\ClassNameProcessor'),
DI\link('Piwik\Log\Processor\SprintfProcessor'),
),
'Piwik\Log\Backend\FileBackend' => DI\object()
Expand Down
48 changes: 2 additions & 46 deletions core/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,65 +266,21 @@ private function doLog($level, $message, $parameters = array())
$record = $processor($record);
}

$record['extra']['class'] = $this->getLoggingClassName();

$this->writeMessage($record, date("Y-m-d H:i:s"));
}

private function writeMessage(array $record)
{
foreach ($this->writers as $writer) {
call_user_func($writer, $record, $this);
}
}

private static function logMessage($level, $message, $sprintfParams)
private static function logMessage($level, $message, $parameters)
{
self::getInstance()->doLog($level, $message, $sprintfParams);
self::getInstance()->doLog($level, $message, $parameters);
}

private function shouldLoggerLog($level)
{
return $level <= $this->currentLogLevel;
}

private function getClassNameThatIsLogging($backtrace)
{
foreach ($backtrace as $tracepoint) {
if (isset($tracepoint['class'])
&& $tracepoint['class'] != 'Piwik\Log'
&& $tracepoint['class'] != 'Piwik\Piwik'
&& $tracepoint['class'] != 'Piwik\CronArchive'
) {
return $tracepoint['class'];
}
}
return false;
}

/**
* Returns the name of the plugin/class that triggered the log.
*
* @return string
*/
private function getLoggingClassName()
{
if (version_compare(phpversion(), '5.3.6', '>=')) {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT);
} else {
$backtrace = debug_backtrace();
}

$tag = Plugin::getPluginNameFromBacktrace($backtrace);

// if we can't determine the plugin, use the name of the calling class
if ($tag == false) {
$tag = $this->getClassNameThatIsLogging($backtrace);
return $tag;
}
return $tag;
}

private function getStringLevel($level)
{
static $levelToName = array(
Expand Down
71 changes: 71 additions & 0 deletions core/Log/Processor/ClassNameProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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();
}
}

0 comments on commit d263af9

Please sign in to comment.