[TASK] Convert log levels from string to integer 15/21815/4
authorSteffen Müller <typo3@t3node.com>
Tue, 2 Jul 2013 16:37:49 +0000 (18:37 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Thu, 4 Jul 2013 14:03:18 +0000 (16:03 +0200)
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
typo3/sysext/core/Classes/Log/LogLevel.php
typo3/sysext/core/Classes/Log/Logger.php
typo3/sysext/core/Tests/Unit/Log/LogLevelTest.php

index ac6e08a..35c9147 100644 (file)
@@ -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;
+       }
+
 }
 
 
index 1c87bbb..6ebaa72 100644 (file)
@@ -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;
index 993cf92..08308e8 100644 (file)
@@ -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