]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Logger.php
Avoid memory issue in exception
[friendica.git] / src / Core / Logger.php
index c6b81e0cc993eea1ef1060f75a3f229a291f7212..6b8112796f557f00dea156a9c22fef3993097584 100644 (file)
@@ -5,8 +5,8 @@
 namespace Friendica\Core;
 
 use Friendica\BaseObject;
+use Friendica\Factory\LoggerFactory;
 use Friendica\Network\HTTPException\InternalServerErrorException;
-use Friendica\Util\LoggerFactory;
 use Psr\Log\LoggerInterface;
 use Psr\Log\LogLevel;
 
@@ -77,14 +77,15 @@ class Logger extends BaseObject
        {
                $debugging = Config::get('system', 'debugging');
                $logfile = Config::get('system', 'logfile');
-               $loglevel = intval(Config::get('system', 'loglevel'));
+               $loglevel = Config::get('system', 'loglevel');
 
                if (!$debugging || !$logfile) {
                        return;
                }
 
-               $level = self::mapPSR3Level($loglevel);
-               LoggerFactory::addStreamHandler($logger, $logfile, $level);
+               $loglevel = self::mapLegacyConfigDebugLevel((string)$loglevel);
+
+               LoggerFactory::addStreamHandler($logger, $logfile, $loglevel);
 
                self::$logger = $logger;
 
@@ -100,15 +101,51 @@ class Logger extends BaseObject
                LoggerFactory::addStreamHandler(self::$devLogger, $logfile, LogLevel::DEBUG);
        }
 
+       /**
+        * Mapping a legacy level to the PSR-3 compliant levels
+        * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
+        *
+        * @param string $level the level to be mapped
+        *
+        * @return string the PSR-3 compliant level
+        */
+       private static function mapLegacyConfigDebugLevel($level)
+       {
+               switch ($level) {
+                       // legacy WARNING
+                       case "0":
+                               return LogLevel::ERROR;
+                       // legacy INFO
+                       case "1":
+                               return LogLevel::WARNING;
+                       // legacy TRACE
+                       case "2":
+                               return LogLevel::NOTICE;
+                       // legacy DEBUG
+                       case "3":
+                               return LogLevel::INFO;
+                       // legacy DATA
+                       case "4":
+                               return LogLevel::DEBUG;
+                       // legacy ALL
+                       case "5":
+                               return LogLevel::DEBUG;
+                       // default if nothing set
+                       default:
+                               return $level;
+               }
+       }
+
        /**
         * System is unusable.
+        *
         * @see LoggerInterface::emergency()
         *
         * @param string $message
         * @param array  $context
         *
         * @return void
-        *
+        * @throws \Exception
         */
        public static function emergency($message, $context = [])
        {
@@ -132,7 +169,7 @@ class Logger extends BaseObject
         * @param array  $context
         *
         * @return void
-        *
+        * @throws \Exception
         */
        public static function alert($message, $context = [])
        {
@@ -155,7 +192,7 @@ class Logger extends BaseObject
         * @param array  $context
         *
         * @return void
-        *
+        * @throws \Exception
         */
        public static function critical($message, $context = [])
        {
@@ -177,7 +214,7 @@ class Logger extends BaseObject
         * @param array  $context
         *
         * @return void
-        *
+        * @throws \Exception
         */
        public static function error($message, $context = [])
        {
@@ -202,7 +239,7 @@ class Logger extends BaseObject
         * @param array  $context
         *
         * @return void
-        *
+        * @throws \Exception
         */
        public static function warning($message, $context = [])
        {
@@ -223,7 +260,7 @@ class Logger extends BaseObject
         * @param array  $context
         *
         * @return void
-        *
+        * @throws \Exception
         */
        public static function notice($message, $context = [])
        {
@@ -246,7 +283,7 @@ class Logger extends BaseObject
         * @param array  $context
         *
         * @return void
-        *
+        * @throws \Exception
         */
        public static function info($message, $context = [])
        {
@@ -267,6 +304,7 @@ class Logger extends BaseObject
         * @param array  $context
         *
         * @return void
+        * @throws \Exception
         */
        public static function debug($message, $context = [])
        {
@@ -283,11 +321,12 @@ class Logger extends BaseObject
      * @brief Logs the given message at the given log level
      *
      * @param string $msg
-     * @param int $level
+     * @param string $level
         *
+        * @throws \Exception
         * @deprecated since 2019.03 Use Logger::debug() Logger::info() , ... instead
      */
-    public static function log($msg, $level = LogLevel::NOTICE)
+    public static function log($msg, $level = LogLevel::INFO)
     {
                if (!isset(self::$logger)) {
                        return;
@@ -298,15 +337,16 @@ class Logger extends BaseObject
         self::getApp()->saveTimestamp($stamp1, "file");
     }
 
-    /**
-     * @brief An alternative logger for development.
-     * Works largely as log() but allows developers
-     * to isolate particular elements they are targetting
-     * personally without background noise
-     *
-     * @param string $msg
+       /**
+        * @brief An alternative logger for development.
+        * Works largely as log() but allows developers
+        * to isolate particular elements they are targetting
+        * personally without background noise
+        *
+        * @param string $msg
         * @param string $level
-     */
+        * @throws \Exception
+        */
     public static function devLog($msg, $level = LogLevel::DEBUG)
     {
                if (!isset(self::$logger)) {