}
/**
- * @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)) {
}
/**
- * 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
/**
* 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
);
}
}
- \Civi::$statics[__CLASS__]['logger_file' . $comp] = $fileName;
+ \Civi::$statics[__CLASS__]['logger_file' . $prefix] = $fileName;
}
- return Log::singleton('file', \Civi::$statics[__CLASS__]['logger_file' . $comp]);
}
/**
}
}
+
+ /**
+ * 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);
+ }
+
}