]> git.mxchange.org Git - friendica.git/blobdiff - src/Factory/LoggerFactory.php
Fix notices
[friendica.git] / src / Factory / LoggerFactory.php
index 0feb3b2f7842b787e04084e176075c63d0b818c4..ad6658b563b6e8040f80d0ad36bc0922cd8e48f3 100644 (file)
@@ -1,11 +1,31 @@
 <?php
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Factory;
 
-use Friendica\Core\Config\Configuration;
+use Friendica\Core\Config\IConfig;
 use Friendica\Core\Logger;
 use Friendica\Database\Database;
 use Friendica\Network\HTTPException\InternalServerErrorException;
+use Friendica\Util\FileSystem;
 use Friendica\Util\Introspection;
 use Friendica\Util\Logger\Monolog\DevelopHandler;
 use Friendica\Util\Logger\Monolog\IntrospectionProcessor;
@@ -38,28 +58,24 @@ class LoggerFactory
                'Friendica\\Util\\Logger',
        ];
 
-       /**
-        * Retrieve the channel based on the __FILE__
-        *
-        * @return string
-        */
-       private function findChannel()
+       private $channel;
+
+       public function __construct(string $channel)
        {
-               return basename($_SERVER['PHP_SELF'], '.php');
+               $this->channel = $channel;
        }
 
        /**
         * Creates a new PSR-3 compliant logger instances
         *
-        * @param Configuration $config   The config
-        * @param Profiler      $profiler The profiler of the app
+        * @param Database   $database   The Friendica Database instance
+        * @param IConfig    $config     The config
+        * @param Profiler   $profiler   The profiler of the app
+        * @param FileSystem $fileSystem FileSystem utils
         *
         * @return LoggerInterface The PSR-3 compliant logger instance
-        *
-        * @throws \Exception
-        * @throws InternalServerErrorException
         */
-       public function create(Database $database, Configuration $config, Profiler $profiler)
+       public function create(Database $database, IConfig $config, Profiler $profiler, FileSystem $fileSystem)
        {
                if (empty($config->get('system', 'debugging', false))) {
                        $logger = new VoidLogger();
@@ -76,7 +92,7 @@ class LoggerFactory
                                $loggerTimeZone = new \DateTimeZone('UTC');
                                Monolog\Logger::setTimezone($loggerTimeZone);
 
-                               $logger = new Monolog\Logger($this->findChannel());
+                               $logger = new Monolog\Logger($this->channel);
                                $logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
                                $logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
                                $logger->pushProcessor(new Monolog\Processor\UidProcessor());
@@ -86,12 +102,22 @@ class LoggerFactory
 
                                // just add a stream in case it's either writable or not file
                                if (!is_file($stream) || is_writable($stream)) {
-                                       static::addStreamHandler($logger, $stream, $loglevel);
+                                       try {
+                                               static::addStreamHandler($logger, $stream, $loglevel);
+                                       } catch (\Throwable $e) {
+                                               // No Logger ..
+                                               $logger = new VoidLogger();
+                                       }
                                }
                                break;
 
                        case 'syslog':
-                               $logger = new SyslogLogger($this->findChannel(), $introspection, $loglevel);
+                               try {
+                                       $logger = new SyslogLogger($this->channel, $introspection, $loglevel);
+                               } catch (\Throwable $e) {
+                                       // No logger ...
+                                       $logger = new VoidLogger();
+                               }
                                break;
 
                        case 'stream':
@@ -99,7 +125,12 @@ class LoggerFactory
                                $stream = $config->get('system', 'logfile');
                                // just add a stream in case it's either writable or not file
                                if (!is_file($stream) || is_writable($stream)) {
-                                       $logger = new StreamLogger($this->findChannel(), $stream, $introspection, $loglevel);
+                                       try {
+                                               $logger = new StreamLogger($this->channel, $stream, $introspection, $fileSystem, $loglevel);
+                                       } catch (\Throwable $t) {
+                                               // No logger ...
+                                               $logger = new VoidLogger();
+                                       }
                                } else {
                                        $logger = new VoidLogger();
                                }
@@ -125,15 +156,16 @@ class LoggerFactory
         *
         * It should never get filled during normal usage of Friendica
         *
-        * @param Configuration $config   The config
-        * @param Profiler      $profiler The profiler of the app
+        * @param IConfig    $config     The config
+        * @param Profiler   $profiler   The profiler of the app
+        * @param FileSystem $fileSystem FileSystem utils
         *
         * @return LoggerInterface The PSR-3 compliant logger instance
         *
         * @throws InternalServerErrorException
         * @throws \Exception
         */
-       public static function createDev(Configuration $config, Profiler $profiler)
+       public static function createDev(IConfig $config, Profiler $profiler, FileSystem $fileSystem)
        {
                $debugging   = $config->get('system', 'debugging');
                $stream      = $config->get('system', 'dlogfile');
@@ -173,7 +205,7 @@ class LoggerFactory
 
                        case 'stream':
                        default:
-                               $logger = new StreamLogger(self::DEV_CHANNEL, $stream, $introspection, LogLevel::DEBUG);
+                               $logger = new StreamLogger(self::DEV_CHANNEL, $stream, $introspection, $fileSystem, LogLevel::DEBUG);
                                break;
                }
 
@@ -213,7 +245,6 @@ class LoggerFactory
                                return LogLevel::INFO;
                        // legacy DATA
                        case "4":
-                               return LogLevel::DEBUG;
                        // legacy ALL
                        case "5":
                                return LogLevel::DEBUG;
@@ -232,7 +263,6 @@ class LoggerFactory
         *
         * @return void
         *
-        * @throws InternalServerErrorException if the logger is incompatible to the logger factory
         * @throws \Exception in case of general failures
         */
        public static function addStreamHandler($logger, $stream, $level = LogLevel::NOTICE)
@@ -251,8 +281,6 @@ class LoggerFactory
                        $fileHandler->setFormatter($formatter);
 
                        $logger->pushHandler($fileHandler);
-               } else {
-                       throw new InternalServerErrorException('Logger instance incompatible for MonologFactory');
                }
        }