diff options
author | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-01 06:24:29 +0300 |
---|---|---|
committer | Matthieu Napoli <matthieu@mnapoli.fr> | 2014-12-01 06:24:29 +0300 |
commit | d263af9fc3aa7982f990dc48141dae4887139c0e (patch) | |
tree | d48463072a845e10b3df0667a037f15174cfa2c8 /core | |
parent | 8f8275426fb62c61627b6e77ab0a377eb4864b71 (diff) |
#6622 Logger refactoring: added Log\Processor\ClassNameProcessor
Diffstat (limited to 'core')
-rw-r--r-- | core/Log.php | 48 | ||||
-rw-r--r-- | core/Log/Processor/ClassNameProcessor.php | 71 |
2 files changed, 73 insertions, 46 deletions
diff --git a/core/Log.php b/core/Log.php index 7ca7721f5e..44e64278fb 100644 --- a/core/Log.php +++ b/core/Log.php @@ -266,21 +266,14 @@ class Log extends Singleton $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) @@ -288,43 +281,6 @@ class Log extends Singleton 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( diff --git a/core/Log/Processor/ClassNameProcessor.php b/core/Log/Processor/ClassNameProcessor.php new file mode 100644 index 0000000000..a5b211d7d3 --- /dev/null +++ b/core/Log/Processor/ClassNameProcessor.php @@ -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(); + } +} |