Skip to content
Snippets Groups Projects
Commit c0591ee4 authored by Steffen Müller's avatar Steffen Müller Committed by Christian Kuhn
Browse files

[TASK] Convert log levels from string to integer

In PSR-3, severity levels for log() can be given as a string.
If a string is given, it gets converted to the corresponding
constant of LogLevel.

Resolves: #48881
Releases: 6.2
Change-Id: I8d10d10b947a20bac483f7d3775a2d2fd7be52f6
Reviewed-on: https://review.typo3.org/21815
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
parent c45b2bf8
No related merge requests found
......@@ -147,6 +147,20 @@ class LogLevel {
}
}
/**
* Normalizes level by converting it from string to integer
*
* @param string $level
* @return integer|string
*/
static public function normalizeLevel($level) {
if (is_string($level) && defined(__CLASS__ . '::' . strtoupper($level))) {
$level = constant(__CLASS__ . '::' . strtoupper($level));
}
return $level;
}
}
......
......@@ -172,12 +172,13 @@ class Logger implements \Psr\Log\LoggerInterface {
/**
* Adds a log record.
*
* @param integer $level Log level.
* @param integer|string $level Log level. Value according to \TYPO3\CMS\Core\Log\LogLevel. Alternatively accepts a string.
* @param string $message Log message.
* @param array $data Additional data to log
* @return mixed
*/
public function log($level, $message, array $data = array()) {
$level = LogLevel::normalizeLevel($level);
\TYPO3\CMS\Core\Log\LogLevel::validateLevel($level);
if ($level > $this->minimumLogLevel) {
return $this;
......
......@@ -76,6 +76,20 @@ class LevelTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
\TYPO3\CMS\Core\Log\LogLevel::validateLevel($inputValue);
}
/**
* @test
*/
public function normalizeLevelConvertsValidLevelFromStringToInteger() {
$this->assertEquals(7, \TYPO3\CMS\Core\Log\LogLevel::normalizeLevel('debug'));
}
/**
* @test
*/
public function normalizeLevelDoesNotConvertInvalidLevel() {
$levelString = 'invalid';
$this->assertEquals($levelString, \TYPO3\CMS\Core\Log\LogLevel::normalizeLevel($levelString));
}
}
?>
\ No newline at end of file
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment