diff options
-rw-r--r-- | core/Common.php | 34 | ||||
-rw-r--r-- | core/Tracker.php | 36 | ||||
-rw-r--r-- | core/Tracker/Action.php | 31 | ||||
-rw-r--r-- | core/Tracker/Cache.php | 10 | ||||
-rw-r--r-- | core/Tracker/Handler.php | 21 | ||||
-rw-r--r-- | core/Tracker/Model.php | 6 | ||||
-rw-r--r-- | plugins/BulkTracking/Tracker/Handler.php | 2 | ||||
-rw-r--r-- | plugins/Monolog/config/config.php | 6 | ||||
-rw-r--r-- | plugins/Monolog/config/tracker.php | 16 | ||||
-rw-r--r-- | plugins/Monolog/tests/System/TrackerLoggingTest.php | 15 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/TrackerTest.php | 2 | ||||
-rw-r--r-- | tests/PHPUnit/Unit/TrackerTest.php | 1 |
12 files changed, 116 insertions, 64 deletions
diff --git a/core/Common.php b/core/Common.php index 6a6fad5df4..15f709ce7a 100644 --- a/core/Common.php +++ b/core/Common.php @@ -1259,34 +1259,20 @@ class Common } /** - * @todo This method is weird, it's debugging statements but seem to only work for the tracker, maybe it - * should be moved elsewhere + * @deprecated Use the logger directly instead. */ public static function printDebug($info = '') { - if (isset($GLOBALS['PIWIK_TRACKER_DEBUG']) && $GLOBALS['PIWIK_TRACKER_DEBUG']) { - if (!headers_sent()) { - // prevent XSS in tracker debug output - Common::sendHeader('Content-type: text/plain'); - } - - if (is_object($info)) { - $info = var_export($info, true); - } - - $logger = StaticContainer::get('Psr\Log\LoggerInterface'); + if (is_object($info)) { + $info = var_export($info, true); + } - if (is_array($info) || is_object($info)) { - $info = Common::sanitizeInputValues($info); - $out = var_export($info, true); - foreach (explode("\n", $out) as $line) { - $logger->debug($line); - } - } else { - foreach (explode("\n", $info) as $line) { - $logger->debug($line); - } - } + $logger = StaticContainer::get('Psr\Log\LoggerInterface'); + if (is_array($info) || is_object($info)) { + $out = var_export($info, true); + $logger->debug($out); + } else { + $logger->debug($info); } } diff --git a/core/Tracker.php b/core/Tracker.php index cca32fae60..3e0cefc4c2 100644 --- a/core/Tracker.php +++ b/core/Tracker.php @@ -9,6 +9,7 @@ namespace Piwik; use Exception; +use Piwik\Container\StaticContainer; use Piwik\Plugins\BulkTracking\Tracker\Requests; use Piwik\Plugins\PrivacyManager\Config as PrivacyManagerConfig; use Piwik\Tracker\Db as TrackerDb; @@ -19,6 +20,7 @@ use Piwik\Tracker\RequestSet; use Piwik\Tracker\TrackerConfig; use Piwik\Tracker\Visit; use Piwik\Plugin\Manager as PluginManager; +use Psr\Log\LoggerInterface; /** * Class used by the logging script piwik.php called by the javascript tag. @@ -43,6 +45,16 @@ class Tracker private $countOfLoggedRequests = 0; protected $isInstalled = null; + /** + * @var LoggerInterface + */ + private $logger; + + public function __construct() + { + $this->logger = StaticContainer::get(LoggerInterface::class); + } + public function isDebugModeEnabled() { return array_key_exists('PIWIK_TRACKER_DEBUG', $GLOBALS) && $GLOBALS['PIWIK_TRACKER_DEBUG'] === true; @@ -53,7 +65,7 @@ class Tracker $record = TrackerConfig::getConfigValue('record_statistics') != 0; if (!$record) { - Common::printDebug('Tracking is disabled in the config.ini.php via record_statistics=0'); + $this->logger->debug('Tracking is disabled in the config.ini.php via record_statistics=0'); } return $record && $this->isInstalled(); @@ -74,8 +86,9 @@ class Tracker ErrorHandler::registerErrorHandler(); ExceptionHandler::setUp(); - Common::printDebug("Debug enabled - Input parameters: "); - Common::printDebug(var_export($_GET + $_POST, true)); + $this->logger->debug("Debug enabled - Input parameters: {params}", [ + 'params' => var_export($_GET + $_POST, true), + ]); } } @@ -129,9 +142,11 @@ class Tracker public function trackRequest(Request $request) { if ($request->isEmptyRequest()) { - Common::printDebug("The request is empty"); + $this->logger->debug('The request is empty'); } else { - Common::printDebug("Current datetime: " . date("Y-m-d H:i:s", $request->getCurrentTimestamp())); + $this->logger->debug('Current datetime: {date}', [ + 'date' => date("Y-m-d H:i:s", $request->getCurrentTimestamp()), + ]); $visit = Visit\Factory::make(); $visit->setRequest($request); @@ -306,15 +321,20 @@ class Tracker try { $pluginManager = PluginManager::getInstance(); $pluginsTracker = $pluginManager->loadTrackerPlugins(); - Common::printDebug("Loading plugins: { " . implode(", ", $pluginsTracker) . " }"); + + $this->logger->debug("Loading plugins: { {plugins} }", [ + 'plugins' => implode(", ", $pluginsTracker), + ]); } catch (Exception $e) { - Common::printDebug("ERROR: " . $e->getMessage()); + $this->logger->error('Error loading tracker plugins: {exception}', [ + 'exception' => $e, + ]); } } private function handleFatalErrors() { - register_shutdown_function(function () { + register_shutdown_function(function () { // TODO: add a log here $lastError = error_get_last(); if (!empty($lastError) && $lastError['type'] == E_ERROR) { Common::sendResponseCode(500); diff --git a/core/Tracker/Action.php b/core/Tracker/Action.php index 5a7a3d9b0b..d0caddcdc3 100644 --- a/core/Tracker/Action.php +++ b/core/Tracker/Action.php @@ -11,10 +11,12 @@ namespace Piwik\Tracker; use Exception; use Piwik\Common; +use Piwik\Container\StaticContainer; use Piwik\Piwik; use Piwik\Plugin\Dimension\ActionDimension; use Piwik\Plugin\Manager; use Piwik\Tracker; +use Psr\Log\LoggerInterface; /** * An action @@ -77,6 +79,11 @@ abstract class Action private $rawActionUrl; /** + * @var mixed|LoggerInterface + */ + private $logger; + + /** * Makes the correct Action object based on the request. * * @param Request $request @@ -148,6 +155,7 @@ abstract class Action { $this->actionType = $type; $this->request = $request; + $this->logger = StaticContainer::get(LoggerInterface::class); } /** @@ -202,8 +210,12 @@ abstract class Action $this->actionUrl = PageUrl::getUrlIfLookValid($url2); if ($url != $this->rawActionUrl) { - Common::printDebug(' Before was "' . $this->rawActionUrl . '"'); - Common::printDebug(' After is "' . $url2 . '"'); + $this->logger->debug(' Before was "{rawActionUrl}"', [ + 'rawActionUrl' => $this->rawActionUrl, + ]); + $this->logger->debug(' After is "{url2}"', [ + 'url2' => $url2, + ]); } } @@ -332,7 +344,7 @@ abstract class Action $actionId = $dimension->getActionId(); $actions[$field] = array($value, $actionId); - Common::printDebug("$field = $value"); + $this->logger->debug("$field = $value"); } } @@ -403,10 +415,11 @@ abstract class Action $visitAction['idlink_va'] = $this->idLinkVisitAction; - Common::printDebug("Inserted new action:"); $visitActionDebug = $visitAction; $visitActionDebug['idvisitor'] = bin2hex($visitActionDebug['idvisitor']); - Common::printDebug($visitActionDebug); + $this->logger->debug("Inserted new action: {action}", [ + 'action' => var_export($visitActionDebug, true), + ]); } public function writeDebugInfo() @@ -415,9 +428,11 @@ abstract class Action $name = $this->getActionName(); $url = $this->getActionUrl(); - Common::printDebug("Action is a $type, - Action name = " . $name . ", - Action URL = " . $url); + $this->logger->debug('Action is a {type}, Action name = {name}, Action URL = {url}', [ + 'type' => $type, + 'name' => $name, + 'url' => $url, + ]); return true; } diff --git a/core/Tracker/Cache.php b/core/Tracker/Cache.php index cd60614461..f20223d234 100644 --- a/core/Tracker/Cache.php +++ b/core/Tracker/Cache.php @@ -13,9 +13,11 @@ use Piwik\ArchiveProcessor\Rules; use Piwik\Cache as PiwikCache; use Piwik\Common; use Piwik\Config; +use Piwik\Container\StaticContainer; use Piwik\Option; use Piwik\Piwik; use Piwik\Tracker; +use Psr\Log\LoggerInterface; /** * Simple cache mechanism used in Tracker to avoid requesting settings from mysql on every request @@ -116,7 +118,9 @@ class Cache * @param int $idSite The site ID to get attributes for. */ Piwik::postEvent('Tracker.Cache.getSiteAttributes', array(&$content, $idSite)); - Common::printDebug("Website $idSite tracker cache was re-created."); + + $logger = StaticContainer::get(LoggerInterface::class); + $logger->debug("Website $idSite tracker cache was re-created."); }); // if nothing is returned from the plugins, we don't save the content @@ -191,7 +195,9 @@ class Cache */ Piwik::postEvent('Tracker.setTrackerCacheGeneral', array(&$cacheContent)); self::setCacheGeneral($cacheContent); - Common::printDebug("General tracker cache was re-created."); + + $logger = StaticContainer::get(LoggerInterface::class); + $logger->debug("General tracker cache was re-created."); Tracker::restoreTrackerPlugins(); diff --git a/core/Tracker/Handler.php b/core/Tracker/Handler.php index cb4707a01b..f4c9fa9339 100644 --- a/core/Tracker/Handler.php +++ b/core/Tracker/Handler.php @@ -10,11 +10,13 @@ namespace Piwik\Tracker; use Piwik\Common; +use Piwik\Container\StaticContainer; use Piwik\Exception\InvalidRequestParameterException; use Piwik\Exception\UnexpectedWebsiteFoundException; use Piwik\Tracker; use Exception; use Piwik\Url; +use Psr\Log\LoggerInterface; class Handler { @@ -28,9 +30,15 @@ class Handler */ private $tasksRunner; + /** + * @var LoggerInterface + */ + private $logger; + public function __construct() { $this->setResponse(new Response()); + $this->logger = StaticContainer::get(LoggerInterface::class); } public function setResponse($response) @@ -81,8 +89,6 @@ class Handler public function onException(Tracker $tracker, RequestSet $requestSet, Exception $e) { - Common::printDebug("Exception: " . $e->getMessage()); - $statusCode = 500; if ($e instanceof UnexpectedWebsiteFoundException) { $statusCode = 400; @@ -90,6 +96,17 @@ class Handler $statusCode = 400; } + // if an internal server error, log as a real error, otherwise it's just malformed input + if ($statusCode == 500) { + $this->logger->error('Exception: {exception}', [ + 'exception' => $e, + ]); + } else { + $this->logger->debug('Exception: {exception}', [ + 'exception' => $e, + ]); + } + $this->response->outputException($tracker, $e, $statusCode); $this->redirectIfNeeded($requestSet); } diff --git a/core/Tracker/Model.php b/core/Tracker/Model.php index 6e77c4c6f5..f7a8406a1c 100644 --- a/core/Tracker/Model.php +++ b/core/Tracker/Model.php @@ -10,7 +10,9 @@ namespace Piwik\Tracker; use Exception; use Piwik\Common; +use Piwik\Container\StaticContainer; use Piwik\Tracker; +use Psr\Log\LoggerInterface; class Model { @@ -76,7 +78,9 @@ class Model try { $this->getDb()->query($sql, $sqlBind); } catch (Exception $e) { - Common::printDebug("There was an error while updating the Conversion: " . $e->getMessage()); + StaticContainer::get(LoggerInterface::class)->error("There was an error while updating the Conversion: {exception}", [ + 'exception' => $e, + ]); return false; } diff --git a/plugins/BulkTracking/Tracker/Handler.php b/plugins/BulkTracking/Tracker/Handler.php index a2ca69f3f3..2cf0b507bd 100644 --- a/plugins/BulkTracking/Tracker/Handler.php +++ b/plugins/BulkTracking/Tracker/Handler.php @@ -25,6 +25,8 @@ class Handler extends Tracker\Handler public function __construct() { + parent::__construct(); + $this->setResponse(new Response()); } diff --git a/plugins/Monolog/config/config.php b/plugins/Monolog/config/config.php index ff60f7c5bd..18e07c5c33 100644 --- a/plugins/Monolog/config/config.php +++ b/plugins/Monolog/config/config.php @@ -40,7 +40,10 @@ return array( $writers = []; foreach ($writerNames as $writerName) { - if ($writerName === 'screen' && \Piwik\Common::isPhpCliMode()) { + if ($writerName === 'screen' + && \Piwik\Common::isPhpCliMode() + && !defined('PIWIK_TEST_MODE') + ) { continue; // screen writer is only valid for web requests } @@ -112,6 +115,7 @@ return array( return Log::getMonologLevel(constant('Piwik\Log::'.strtoupper($level))); } } + return Logger::WARNING; }), diff --git a/plugins/Monolog/config/tracker.php b/plugins/Monolog/config/tracker.php index 620101ff7a..bb7820f9f1 100644 --- a/plugins/Monolog/config/tracker.php +++ b/plugins/Monolog/config/tracker.php @@ -10,17 +10,13 @@ function isTrackerDebugEnabled(ContainerInterface $c) return array( - 'Psr\Log\LoggerInterface' => \DI\decorate(function ($previous, ContainerInterface $c) { - if (isTrackerDebugEnabled($c)) { - return $previous; - } else { - return new \Psr\Log\NullLogger(); - } - }), - - 'log.handler.classes' => DI\decorate(function ($previous) { - if (isset($previous['screen'])) { + 'log.handler.classes' => DI\decorate(function ($previous, ContainerInterface $c) { + if (isset($previous['screen']) + && isTrackerDebugEnabled($c) + ) { $previous['screen'] = 'Piwik\Plugins\Monolog\Handler\EchoHandler'; + } else { + unset($previous['screen']); } return $previous; diff --git a/plugins/Monolog/tests/System/TrackerLoggingTest.php b/plugins/Monolog/tests/System/TrackerLoggingTest.php index a7b2fe2025..44a7755e9f 100644 --- a/plugins/Monolog/tests/System/TrackerLoggingTest.php +++ b/plugins/Monolog/tests/System/TrackerLoggingTest.php @@ -15,6 +15,7 @@ use Piwik\Tests\Framework\Fixture; use Piwik\Tests\Framework\TestCase\SystemTestCase; use Piwik\Tests\Framework\TestingEnvironmentVariables; use PiwikTracker; +use Psr\Container\ContainerInterface; /** * @group Monolog @@ -75,11 +76,10 @@ class TrackerLoggingTest extends SystemTestCase { $response = $t->doTrackPageView('incredible title!'); - $this->assertStringStartsWith("DEBUG: Debug enabled - Input parameters: -DEBUG: array ( -DEBUG: 'idsite' => '1', -DEBUG: 'rec' => '1', -DEBUG: 'apiv' => '1',", $response); + $this->assertStringStartsWith("DEBUG: Debug enabled - Input parameters: array ( + 'idsite' => '1', + 'rec' => '1', + 'apiv' => '1',", $response); } private function setTrackerConfig($trackerConfig) @@ -96,11 +96,10 @@ DEBUG: 'apiv' => '1',", $response); 'Psr\Log\LoggerInterface' => \DI\get('Monolog\Logger'), Config::class => \DI\decorate(function (Config $config) { $config->tests['enable_logging'] = 1; + $config->log['log_writers'] = ['screen']; return $config; }), - 'log.handlers' => [ - \DI\get(EchoHandler::class), - ], + 'Tests.log.allowAllHandlers' => true, ); } diff --git a/tests/PHPUnit/Integration/TrackerTest.php b/tests/PHPUnit/Integration/TrackerTest.php index 450a9a64c4..cd0778a253 100644 --- a/tests/PHPUnit/Integration/TrackerTest.php +++ b/tests/PHPUnit/Integration/TrackerTest.php @@ -490,6 +490,8 @@ class TestTracker extends Tracker { public function __construct() { + parent::__construct(); + $this->isInstalled = true; $this->record = true; } diff --git a/tests/PHPUnit/Unit/TrackerTest.php b/tests/PHPUnit/Unit/TrackerTest.php index 8d0dc89908..7a0e11d112 100644 --- a/tests/PHPUnit/Unit/TrackerTest.php +++ b/tests/PHPUnit/Unit/TrackerTest.php @@ -208,6 +208,7 @@ class TestTracker extends Tracker { public function __construct() { + parent::__construct(); $this->record = true; } |