]> git.mxchange.org Git - friendica-addons.git/commitdiff
Rewrite Monolog factory to implement LoggerFactory
authorArt4 <art4@wlabs.de>
Wed, 5 Feb 2025 15:11:14 +0000 (15:11 +0000)
committerArt4 <art4@wlabs.de>
Wed, 5 Feb 2025 15:11:14 +0000 (15:11 +0000)
monolog/src/Factory/Monolog.php [deleted file]
monolog/src/Factory/MonologFactory.php [new file with mode: 0644]
monolog/static/dependencies.config.php
monolog/static/strategies.config.php [deleted file]

diff --git a/monolog/src/Factory/Monolog.php b/monolog/src/Factory/Monolog.php
deleted file mode 100644 (file)
index c5c6982..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-<?php
-
-namespace Friendica\Addon\monolog\src\Factory;
-
-use Friendica\Addon\monolog\src\Monolog\IntrospectionProcessor;
-use Friendica\Core\Config\Capability\IManageConfigValues;
-use Friendica\Core\Logger\Factory\AbstractLoggerTypeFactory;
-use Monolog\Formatter\LineFormatter;
-use Monolog\Handler\StreamHandler;
-use Monolog\Logger;
-use Monolog\Processor\ProcessIdProcessor;
-use Monolog\Processor\PsrLogMessageProcessor;
-use Monolog\Processor\UidProcessor;
-use Psr\Log\LoggerInterface;
-use Psr\Log\LogLevel;
-
-require_once __DIR__ . '/../../vendor/autoload.php';
-
-class Monolog extends AbstractLoggerTypeFactory
-{
-       public function create(IManageConfigValues $config, string $loglevel = null): LoggerInterface
-       {
-               $loggerTimeZone = new \DateTimeZone('UTC');
-
-               $logger = new Logger($this->channel);
-               $logger->setTimezone($loggerTimeZone);
-               $logger->pushProcessor(new PsrLogMessageProcessor());
-               $logger->pushProcessor(new ProcessIdProcessor());
-               $logger->pushProcessor(new UidProcessor());
-               $logger->pushProcessor(new IntrospectionProcessor($this->introspection, LogLevel::DEBUG));
-
-               $logfile = $config->get('system', 'logfile');
-
-               // just add a stream in case it's either writable or not file
-               if (is_writable($logfile)) {
-                       $loglevel = $loglevel ?? static::mapLegacyConfigDebugLevel($config->get('system', 'loglevel'));
-                       $loglevel = Logger::toMonologLevel($loglevel);
-
-                       // fallback to notice if an invalid loglevel is set
-                       if (!is_int($loglevel)) {
-                               $loglevel = LogLevel::NOTICE;
-                       }
-
-                       $fileHandler = new StreamHandler($logfile, $loglevel);
-
-                       $formatter = new LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
-                       $fileHandler->setFormatter($formatter);
-
-                       $logger->pushHandler($fileHandler);
-               }
-
-               return $logger;
-       }
-}
diff --git a/monolog/src/Factory/MonologFactory.php b/monolog/src/Factory/MonologFactory.php
new file mode 100644 (file)
index 0000000..ede4d5b
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+
+namespace Friendica\Addon\monolog\src\Factory;
+
+use Friendica\Addon\monolog\src\Monolog\IntrospectionProcessor;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Logger\Capability\IHaveCallIntrospections;
+use Friendica\Core\Logger\Factory\LoggerFactory;
+use Monolog\Formatter\LineFormatter;
+use Monolog\Handler\StreamHandler;
+use Monolog\Logger;
+use Monolog\Processor\ProcessIdProcessor;
+use Monolog\Processor\PsrLogMessageProcessor;
+use Monolog\Processor\UidProcessor;
+use Psr\Log\LoggerInterface;
+use Psr\Log\LogLevel;
+
+require_once __DIR__ . '/../../vendor/autoload.php';
+
+final class MonologFactory implements LoggerFactory
+{
+       private IHaveCallIntrospections $introspection;
+
+       private IManageConfigValues $config;
+
+       /**
+        * @param string $channel The channel for the logger
+        */
+       public function __construct(IHaveCallIntrospections $introspection, IManageConfigValues $config)
+       {
+               $this->introspection = $introspection;
+               $this->config        = $config;
+       }
+
+       /**
+        * Creates and returns a PSR-3 Logger instance.
+        *
+        * Calling this method multiple times with the same parameters SHOULD return the same object.
+        *
+        * @param \Psr\Log\LogLevel::* $logLevel The log level
+        * @param \Friendica\Core\Logger\Capability\LogChannel::* $logChannel The log channel
+        */
+       public function createLogger(string $logLevel, string $logChannel): LoggerInterface
+       {
+               $loggerTimeZone = new \DateTimeZone('UTC');
+
+               $logger = new Logger($logChannel);
+               $logger->setTimezone($loggerTimeZone);
+               $logger->pushProcessor(new PsrLogMessageProcessor());
+               $logger->pushProcessor(new ProcessIdProcessor());
+               $logger->pushProcessor(new UidProcessor());
+               $logger->pushProcessor(new IntrospectionProcessor($this->introspection, LogLevel::DEBUG));
+
+               $logfile = $this->config->get('system', 'logfile');
+
+               // just add a stream in case it's either writable or not file
+               if (is_writable($logfile)) {
+                       $logLevel = Logger::toMonologLevel($logLevel);
+
+                       // fallback to notice if an invalid loglevel is set
+                       if (!is_int($logLevel)) {
+                               $logLevel = LogLevel::NOTICE;
+                       }
+
+                       $fileHandler = new StreamHandler($logfile, $logLevel);
+
+                       $formatter = new LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
+                       $fileHandler->setFormatter($formatter);
+
+                       $logger->pushHandler($fileHandler);
+               }
+
+               return $logger;
+       }
+}
index e3066594e75c52709ea4cd9e2a20f58a48045b5c..75ea8df74234becd0476fae7807cfc97b74992e8 100644 (file)
  */
 
 return [
-       \Monolog\Logger::class => [
-               'instanceOf' => \Friendica\Addon\monolog\src\Factory\Monolog::class,
-               'call' => [
-                       ['create', [], \Dice\Dice::CHAIN_CALL],
-               ],
+       \Friendica\Core\Logger\Factory\LoggerFactory::class => [
+               'instanceOf' => \Friendica\Addon\monolog\src\Factory\MonologFactory::class,
        ],
 ];
diff --git a/monolog/static/strategies.config.php b/monolog/static/strategies.config.php
deleted file mode 100644 (file)
index 8715383..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-<?php
-/**
- * @copyright Copyright (C) 2010-2023, 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/>.
- *
- */
-
-return [
-       \Psr\Log\LoggerInterface::class => [
-               \Monolog\Logger::class => ['monolog'],
-       ],
-];