diff --git a/4dev/tests/DB/CoreLibsDBIOTest.php b/4dev/tests/DB/CoreLibsDBIOTest.php index b60d4549..0ad119a4 100644 --- a/4dev/tests/DB/CoreLibsDBIOTest.php +++ b/4dev/tests/DB/CoreLibsDBIOTest.php @@ -37,6 +37,7 @@ namespace tests; use PHPUnit\Framework\TestCase; use PHPUnit\Framework\MockObject\MockObject; +use CoreLibs\Logging\Logger\Level; /** * Test class for DB\IO + DB\SQL\PgSQL @@ -59,20 +60,6 @@ final class CoreLibsDBIOTest extends TestCase 'db_type' => 'pgsql', 'db_encoding' => '', 'db_ssl' => 'allow', // allow, disable, require, prefer - 'db_debug' => true, - ], - // same as valid, but db debug is off - 'valid_debug_false' => [ - 'db_name' => 'corelibs_db_io_test', - 'db_user' => 'corelibs_db_io_test', - 'db_pass' => 'corelibs_db_io_test', - 'db_host' => 'localhost', - 'db_port' => 5432, - 'db_schema' => 'public', - 'db_type' => 'pgsql', - 'db_encoding' => '', - 'db_ssl' => 'allow', // allow, disable, require, prefer - 'db_debug' => false, ], // same as valid, but encoding is set 'valid_with_encoding_utf8' => [ @@ -85,7 +72,6 @@ final class CoreLibsDBIOTest extends TestCase 'db_type' => 'pgsql', 'db_encoding' => 'UTF-8', 'db_ssl' => 'allow', // allow, disable, require, prefer - 'db_debug' => true, ], // valid with no schema set 'valid_no_schema' => [ @@ -98,7 +84,6 @@ final class CoreLibsDBIOTest extends TestCase 'db_type' => 'pgsql', 'db_encoding' => '', 'db_ssl' => 'allow', // allow, disable, require, prefer - 'db_debug' => true, ], // invalid (missing db name) 'invalid' => [ @@ -111,10 +96,10 @@ final class CoreLibsDBIOTest extends TestCase 'db_type' => 'pgsql', 'db_encoding' => '', 'db_ssl' => 'allow', // allow, disable, require, prefer - 'db_debug' => true, ], ]; private static $log; + private static bool $db_debug = false; /** * Test if pgsql module loaded @@ -137,6 +122,8 @@ final class CoreLibsDBIOTest extends TestCase 'log_folder' => DIRECTORY_SEPARATOR . 'tmp', 'log_file_id' => 'CoreLibs-DB-IO-Test', ]); + // will be true, default logging is true + self::$db_debug = self::$log->getLoggingLevel()->includes(Level::Debug); $db = new \CoreLibs\DB\IO( self::$db_config['valid'], self::$log @@ -541,11 +528,6 @@ final class CoreLibsDBIOTest extends TestCase // actions (set) null, // set exepected - self::$db_config['valid']['db_debug'], - ], - 'set debug to true' => [ - 'valid_debug_false', - true, true, ], 'set debug to false' => [ @@ -570,12 +552,7 @@ final class CoreLibsDBIOTest extends TestCase // actions null, // toggle is inverse - self::$db_config['valid']['db_debug'] ? false : true, - ], - 'toggle debug to true' => [ - 'valid_debug_false', - true, - true, + self::$db_debug ? true : false, ], 'toggle debug to false' => [ 'valid', @@ -604,6 +581,7 @@ final class CoreLibsDBIOTest extends TestCase self::$db_config[$connection], self::$log ); + echo "Expected: " . self::$db_debug . "\n"; $this->assertEquals( $expected, $set === null ? @@ -806,7 +784,6 @@ final class CoreLibsDBIOTest extends TestCase 'host' => 'db_host', 'port' => 'db_port', 'ssl' => 'db_ssl', - 'debug' => 'db_debug', 'password' => '***', ] as $read => $compare ) { diff --git a/www/configs/config.db.php b/www/configs/config.db.php index 23bf44d3..1bc7a6bf 100644 --- a/www/configs/config.db.php +++ b/www/configs/config.db.php @@ -22,7 +22,6 @@ $DB_CONFIG = [ 'db_type' => 'pgsql', 'db_encoding' => '', 'db_ssl' => 'allow', // allow, disable, require, prefer - 'db_debug' => true, // turn on logging or not ], // same as above, but uses pg bouncer 'test_pgbouncer' => [ @@ -35,7 +34,6 @@ $DB_CONFIG = [ 'db_type' => 'pgsql', 'db_encoding' => '', 'db_ssl' => 'allow', // allow, disable, require, prefer - 'db_debug' => true, // turn on logging or not ], ]; diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index 29b0b19b..e890ea1b 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -395,12 +395,10 @@ class IO * main DB concstructor with auto connection to DB and failure set on failed connection * @param array $db_config DB configuration array * @param \CoreLibs\Logging\Logging $log Logging class - * @param bool|null $db_debug_override Overrides debug settings in db_config */ public function __construct( array $db_config, - \CoreLibs\Logging\Logging $log, - ?bool $db_debug_override = null + \CoreLibs\Logging\Logging $log ) { // attach logger $this->log = $log; @@ -417,15 +415,10 @@ class IO $this->db_ssl = !empty($db_config['db_ssl']) ? $db_config['db_ssl'] : 'allow'; // set debug, either via global var, or from config, else set to false $this->dbSetDebug( - // override - $db_debug_override ?? - // from db config setting - $db_config['db_debug'] ?? - // [DEPRECATED] should be handled from outside - $_SESSION['DB_DEBUG'] ?? - // [DEPRECATED] globals should be deprecated - $GLOBALS['DB_DEBUG'] ?? - false + // set if logging level is Debug + $this->log->getLoggingLevel()->includes( + \CoreLibs\Logging\Logger\Level::Debug + ) ); // set loop protection max count @@ -664,6 +657,8 @@ class IO /** * calls the basic class debug with strip command + * for internal calls, will always create a message + * * @param string $debug_id group id for debug * @param string $error_string error message or debug data * @param string $id db debug group @@ -672,7 +667,7 @@ class IO * Will be printed after main error string * @return void */ - protected function __dbDebug( + private function __dbDebugMessage( string $debug_id, string $error_string, string $id = '', @@ -682,24 +677,24 @@ class IO // NOTE prefix allows html for echo output, will be stripped on file print $prefix = ''; if ($id) { - $prefix .= '[' . $id . '] '; + $prefix .= '[' . $id . '] '; } if ($type) { - $prefix .= '{' . $type . '} '; + $prefix .= '{' . $type . '} '; } switch ($id) { case 'DB_ERROR': - $prefix .= 'DB-Error:'; + $prefix .= 'DB-Error:'; break; case 'DB_WARNING': - $prefix .= 'DB-Warning:'; + $prefix .= 'DB-Warning:'; break; } if ($prefix) { $prefix .= '- '; } if ($error_data !== []) { - $error_string .= '
[' + $error_string .= "\n" . '[' . \CoreLibs\Debug\Support::prAr($error_data) . ']'; } @@ -716,6 +711,30 @@ class IO } } + /** + * main call from anywhere in all classes for launching debug messages + * will abort if dbDebug not set + * + * @param string $debug_id group id for debug + * @param string $error_string error message or debug data + * @param string $id db debug group + * @param string $type query identifier (Q, I, etc) + * @param array $error_data Optional error data as array + * @return void + */ + protected function __dbDebug( + string $debug_id, + string $error_string, + string $id = '', + string $type = '', + array $error_data = [] + ): void { + if (!$this->dbGetDebug()) { + return; + } + $this->__dbDebugMessage($debug_id, $error_string, $id, $type, $error_data); + } + /** * Reset warnings and errors before run * Is called on base queries to reset error before each run @@ -778,7 +797,7 @@ class IO $db_error_string = $db_prefix . ' ' . $db_error_string; } if ($db_error_string) { - $this->__dbDebug('db', $db_error_string, 'DB_ERROR', $where_called); + $this->__dbDebugMessage('db', $db_error_string, 'DB_ERROR', $where_called); } return [ $where_called, @@ -835,7 +854,7 @@ class IO $error_id = (string)$error_id; [$where_called, $pg_error_string] = $this->__dbErrorPreprocessor($cursor); // write error msg ... - $this->__dbDebug( + $this->__dbDebugMessage( 'db', $error_id . ': ' . ($this->error_string[$error_id] ?? '[UNKNOWN ERROR]') . ($msg ? ', ' . $msg : ''), @@ -861,7 +880,7 @@ class IO ): void { $warning_id = (string)$warning_id; [$where_called, $pg_error_string] = $this->__dbErrorPreprocessor($cursor); - $this->__dbDebug( + $this->__dbDebugMessage( 'db', $warning_id . ': ' . ($this->error_string[$warning_id] ?? '[UNKNOWN WARNING') . ($msg ? ', ' . $msg : ''), @@ -1166,17 +1185,15 @@ class IO } // $this->debug('DB IO', 'Q: '.$this->query.', RETURN: '.$this->returning_id); // for DEBUG, only on first time ;) - if ($this->db_debug) { - $this->__dbDebug( - 'db', - $this->__dbDebugPrepare( - $this->query, - $this->params - ), - '__dbPrepareExec', - ($this->params === [] ? 'Q' : 'Qp') - ); - } + $this->__dbDebug( + 'db', + $this->__dbDebugPrepare( + $this->query, + $this->params + ), + '__dbPrepareExec', + ($this->params === [] ? 'Q' : 'Qp') + ); // import protection, hash needed $query_hash = $this->dbGetQueryHash($this->query, $this->params); // if the array index does not exists set it 0 @@ -1194,7 +1211,7 @@ class IO $this->query_called[$query_hash] > $this->MAX_QUERY_CALL ) { $this->__dbError(30, false, $this->query); - $this->__dbDebug( + $this->__dbDebugMessage( 'db', $this->__dbDebugPrepare( $this->query, @@ -1222,9 +1239,7 @@ class IO // if either the cursor is false if ($this->cursor === false || $this->db_functions->__dbLastErrorQuery()) { // printout Query if debug is turned on - if ($this->db_debug) { - $this->__dbDebug('db', $this->query, 'dbExec', 'Q[nc]'); - } + $this->__dbDebug('db', $this->query, 'dbExec', 'Q[nc]'); // internal error handling $this->__dbError(13, $this->cursor); return false; @@ -1460,10 +1475,11 @@ class IO $string .= 'at host {b}\'' . $this->db_host . '\'{/b} '; $string .= 'on port {b}\'' . $this->db_port . '\'{/b} '; $string .= 'with ssl mode {b}\'' . $this->db_ssl . '\'{/b}{br}'; - $string .= '{b}-DB-info->{/b} DB IO Class debug output: {b}' . ($this->db_debug ? 'Yes' : 'No') . '{/b}'; + $string .= '{b}-DB-info->{/b} DB IO Class debug output: {b}' + . ($this->dbGetDebug() ? 'Yes' : 'No') . '{/b}'; if ($log === true) { // if debug, remove / change b - $this->__dbDebug('db', str_replace( + $this->__dbDebugMessage('db', str_replace( $html_tags, $replace_text, $string @@ -1605,7 +1621,7 @@ class IO if (is_array($array)) { $this->nbsp = ''; $string .= $this->__printArray($array); - $this->__dbDebug('db', $string, 'dbDumpData'); + $this->__dbDebugMessage('db', $string, 'dbDumpData'); } return $string; } @@ -1985,17 +2001,15 @@ class IO // checks if the params count given matches the expected count if ($this->__dbCheckQueryParams($query, count($params)) === false) { // in case we got an error print out query - if ($this->db_debug) { - $this->__dbDebug( - 'db', - $this->__dbDebugPrepare( - $this->query, - $this->params - ), - 'dbReturn', - ($this->params === [] ? 'Q[e]' : 'Qp[e]') - ); - } + $this->__dbDebug( + 'db', + $this->__dbDebugPrepare( + $this->query, + $this->params + ), + 'dbReturn', + ($this->params === [] ? 'Q[e]' : 'Qp[e]') + ); return false; } // set first call to false @@ -2018,17 +2032,15 @@ class IO if (!$this->cursor_ext[$query_hash]['cursor']) { $this->cursor_ext[$query_hash]['log'][] = 'No cursor'; // for DEBUG, print out each query executed - if ($this->db_debug) { - $this->__dbDebug( - 'db', - $this->__dbDebugPrepare( - $this->cursor_ext[$query_hash]['query'], - $this->cursor_ext[$query_hash]['params'] - ), - 'dbReturn', - ($this->cursor_ext[$query_hash]['params'] === [] ? 'Q' : 'Qp'), - ); - } + $this->__dbDebug( + 'db', + $this->__dbDebugPrepare( + $this->cursor_ext[$query_hash]['query'], + $this->cursor_ext[$query_hash]['params'] + ), + 'dbReturn', + ($this->cursor_ext[$query_hash]['params'] === [] ? 'Q' : 'Qp'), + ); // if no DB Handler try to reconnect if (!$this->dbh) { // if reconnect fails drop out @@ -2055,17 +2067,15 @@ class IO } // if still no cursor ... if (!$this->cursor_ext[$query_hash]['cursor']) { - if ($this->db_debug) { - $this->__dbDebug( - 'db', - $this->__dbDebugPrepare( - $this->cursor_ext[$query_hash]['query'], - $this->cursor_ext[$query_hash]['params'] - ), - 'dbReturn', - ($this->cursor_ext[$query_hash]['params'] === [] ? 'Q[e]' : 'Qp[e]'), - ); - } + $this->__dbDebug( + 'db', + $this->__dbDebugPrepare( + $this->cursor_ext[$query_hash]['query'], + $this->cursor_ext[$query_hash]['params'] + ), + 'dbReturn', + ($this->cursor_ext[$query_hash]['params'] === [] ? 'Q[e]' : 'Qp[e]'), + ); // internal error handling $this->__dbError(13, $this->cursor_ext[$query_hash]['cursor']); return false; @@ -2720,17 +2730,15 @@ class IO ); return false; } - if ($this->db_debug) { - $this->__dbDebug( - 'db', - $this->__dbDebugPrepare( - $this->prepare_cursor[$stm_name]['query'], - $data - ), - 'dbExecPrep', - 'Qpe' - ); - } + $this->__dbDebug( + 'db', + $this->__dbDebugPrepare( + $this->prepare_cursor[$stm_name]['query'], + $data + ), + 'dbExecPrep', + 'Qpe' + ); // if the count does not match if ($this->prepare_cursor[$stm_name]['count'] != count($data)) { $this->__dbError(