From edc8adfc50d0bacee4058301e40ceab2b41c1af4 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 29 Mar 2016 17:28:32 +1300 Subject: [PATCH] CRM-18310 add tests for debug logging, fix caching error --- CRM/Core/DAO.php | 6 ++++-- CRM/Core/Error.php | 32 +++++++++++++++++++--------- tests/phpunit/CRM/Core/ErrorTest.php | 19 +++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index 1b01d7455f..cb02a95c57 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -1331,14 +1331,16 @@ FROM civicrm_domain } /** - * @param $query + * Compose the query by merging the parameters into it. + * + * @param string $query * @param array $params * @param bool $abort * * @return string * @throws Exception */ - public static function composeQuery($query, &$params, $abort = TRUE) { + public static function composeQuery($query, $params, $abort = TRUE) { $tr = array(); foreach ($params as $key => $item) { if (is_numeric($key)) { diff --git a/CRM/Core/Error.php b/CRM/Core/Error.php index 7614a1b355..7d488ee907 100644 --- a/CRM/Core/Error.php +++ b/CRM/Core/Error.php @@ -570,7 +570,10 @@ class CRM_Core_Error extends PEAR_ErrorStack { } /** - * Display the error message on terminal. + * Display the error message on terminal and append it to the log file. + * + * Provided the user has the 'view debug output' the output should be displayed. In all + * cases it should be logged. * * @param string $message * @param bool $out @@ -634,19 +637,29 @@ class CRM_Core_Error extends PEAR_ErrorStack { /** * Obtain a reference to the error log. * - * @param string $comp + * @param string $prefix * * @return Log */ - public static function createDebugLogger($comp = '') { - if (!isset(\Civi::$statics[__CLASS__]['logger_file' . $comp])) { + public static function createDebugLogger($prefix = '') { + self::generateLogFileName($prefix); + return Log::singleton('file', \Civi::$statics[__CLASS__]['logger_file' . $prefix], ''); + } + + /** + * Generate the name of the logfile to use and store it as a static. + * + * This function includes poor man's log file management and a check as to whether the file exists. + * + * @param string $prefix + */ + protected static function generateLogFileName($prefix) { + if (!isset(\Civi::$statics[__CLASS__]['logger_file' . $prefix])) { $config = CRM_Core_Config::singleton(); - if ($comp) { - $comp = $comp . '.'; - } + $prefixString = $prefix ? ($prefix . '.') : ''; - $fileName = "{$config->configAndLogDir}CiviCRM." . $comp . md5($config->dsn) . '.log'; + $fileName = $config->configAndLogDir . 'CiviCRM.' . $prefixString . md5($config->dsn) . '.log'; // Roll log file monthly or if greater than 256M // note that PHP file functions have a limit of 2G and hence @@ -663,9 +676,8 @@ class CRM_Core_Error extends PEAR_ErrorStack { ); } } - \Civi::$statics[__CLASS__]['logger_file' . $comp] = $fileName; + \Civi::$statics[__CLASS__]['logger_file' . $prefix] = $fileName; } - return Log::singleton('file', \Civi::$statics[__CLASS__]['logger_file' . $comp]); } /** diff --git a/tests/phpunit/CRM/Core/ErrorTest.php b/tests/phpunit/CRM/Core/ErrorTest.php index 6b72b87bd0..1db541c657 100644 --- a/tests/phpunit/CRM/Core/ErrorTest.php +++ b/tests/phpunit/CRM/Core/ErrorTest.php @@ -93,4 +93,23 @@ class CRM_Core_ErrorTest extends CiviUnitTestCase { } } + + /** + * Check that a debugger is created and there is no error when passing in a prefix. + * + * Do some basic content checks. + */ + public function testDebugLoggerFormat() { + $log = CRM_Core_Error::createDebugLogger('my-test'); + $log->log('Mary had a little lamb'); + $log->log('Little lamb'); + $config = CRM_Core_Config::singleton(); + $fileContents = file_get_contents($log->_filename); + $this->assertEquals($config->configAndLogDir . 'CiviCRM.' . 'my-test.' . md5($config->dsn) . '.log', $log->_filename); + // The 5 here is a bit arbitrary - on my local the date part is 15 chars (Mar 29 05:29:16) - but we are just checking that + // there are chars for the date at the start. + $this->assertTrue(strpos($fileContents, '[info] Mary had a little lamb') > 10); + $this->assertContains('[info] Little lamb', $fileContents); + } + } -- 2.25.1