diff options
-rw-r--r-- | config/global.php | 1 | ||||
-rw-r--r-- | core/Log.php | 48 | ||||
-rw-r--r-- | core/Log/Processor/ClassNameProcessor.php | 71 |
3 files changed, 74 insertions, 46 deletions
diff --git a/config/global.php b/config/global.php index 6cebcdce9d..e41f6a2bea 100644 --- a/config/global.php +++ b/config/global.php @@ -30,6 +30,7 @@ return array( // 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() 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(); + } +} |