N°4261 - Log in db: change default level.

In order to preserve the existing behavior: not EventIssue creation (unless configured otherwise).
This commit is contained in:
bruno-ds
2021-10-21 11:04:08 +02:00
parent 820f2cbed6
commit 62f7eca0a8
3 changed files with 34 additions and 10 deletions

View File

@@ -562,20 +562,22 @@ abstract class LogAPI
public const LEVEL_OK = 'Ok';
public const LEVEL_DEBUG = 'Debug';
public const LEVEL_TRACE = 'Trace';
/**
* @see GetMinLogLevel
* @used-by GetLevelDefault
* @var string default log level
* @var string default log level.
* @since 2.7.1 N°2977
*/
public const LEVEL_DEFAULT = self::LEVEL_OK;
/**
* @see GetMinLogLevel
* @used-by GetLevelDefault
* @var string default log level when writing to DB
* @var string|bool default log level when writing to DB: false by default in order to disable EventIssue creation, and so on, do not change the behavior.
* @since 3.0.0 N°4261
*/
public const LEVEL_DEFAULT_DB = self::LEVEL_ERROR;
public const LEVEL_DEFAULT_DB = false;
protected static $aLevelsPriority = array(
self::LEVEL_ERROR => 400,
@@ -867,15 +869,15 @@ abstract class LogAPI
*
* @param string $sConfigKey config key used for log
*
* @return string
* @return string|bool if false, then disable log for any level
*
* @uses \LogAPI::LEVEL_DEFAULT
* @uses \LogAPI::LEVEL_DEFAULT_DB
*
* @since 3.0.0 N°3731
* @since 3.0.0 N°3731 Method creation
* @since 3.0.0 N°4261 add specific default level for DB write
*/
protected static function GetLevelDefault(string $sConfigKey): string
protected static function GetLevelDefault(string $sConfigKey)
{
switch ($sConfigKey) {
case static::ENUM_CONFIG_PARAM_DB:
@@ -1113,11 +1115,8 @@ class DeprecatedCallsLog extends LogAPI
*
* In other words, when in dev mode all deprecated calls will be logged to file
*
* @param string $sConfigKey
*
* @return string
*/
protected static function GetLevelDefault(string $sConfigKey): string
protected static function GetLevelDefault(string $sConfigKey)
{
if ($sConfigKey === self::ENUM_CONFIG_PARAM_DB) {
return parent::GetLevelDefault($sConfigKey);
@@ -1296,6 +1295,8 @@ class ExceptionLog extends LogAPI
public const CHANNEL_DEFAULT = 'Exception';
public const CONTEXT_EXCEPTION = '__exception';
public const LEVEL_DEFAULT_DB = self::LEVEL_ERROR;
protected static $m_oFileLog = null;
/**

View File

@@ -267,6 +267,16 @@ class ExceptionLogTest extends ItopDataTestCase
],
];
}
public function testGetLevelDefault()
{
$resultDb = $this->InvokeNonPublicStaticMethod(\ExceptionLog::class, 'GetLevelDefault', [\ExceptionLog::ENUM_CONFIG_PARAM_DB]);
$resultFile = $this->InvokeNonPublicStaticMethod(\ExceptionLog::class, 'GetLevelDefault', [\ExceptionLog::ENUM_CONFIG_PARAM_FILE]);
$resultFilePerDefaultWhenKeyNotFound = $this->InvokeNonPublicStaticMethod(\ExceptionLog::class, 'GetLevelDefault', ['foo']);
$this->assertEquals('Error', $resultDb);
$this->assertEquals('Ok', $resultFile);
$this->assertEquals('Ok', $resultFilePerDefaultWhenKeyNotFound);
}
}

View File

@@ -183,4 +183,17 @@ class LogAPITest extends ItopDataTestCase
$this->assertNotEmpty($oEventIssue->Get($sAttCode), "In the EventIssue instance returned by LogAPI the '$sAttCode' mandatory attr is empty :(");
}
}
public function testGetLevelDefault()
{
$resultDb = $this->InvokeNonPublicStaticMethod(\LogAPI::class, 'GetLevelDefault', [\LogAPI::ENUM_CONFIG_PARAM_DB]);
$resultFile = $this->InvokeNonPublicStaticMethod(\LogAPI::class, 'GetLevelDefault', [\LogAPI::ENUM_CONFIG_PARAM_FILE]);
$resultFilePerDefaultWhenKeyNotFound = $this->InvokeNonPublicStaticMethod(\LogAPI::class, 'GetLevelDefault', ['foo']);
$this->assertEquals(false, $resultDb);
$this->assertEquals('Ok', $resultFile);
$this->assertEquals('Ok', $resultFilePerDefaultWhenKeyNotFound);
}
}