]> git.mxchange.org Git - friendica.git/blobdiff - src/Core/Logger/Type/StreamLogger.php
Merge branch '2023.03-rc' into stable
[friendica.git] / src / Core / Logger / Type / StreamLogger.php
index be0283d0ec250f13a5686d73981e303414c4890e..d56f5b20ef6fd2b8fa001f20d9ab811597f1efea 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Core\Logger\Type;
 
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\Hooks\Capabilities\IAmAStrategy;
+use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
 use Friendica\Core\Logger\Exception\LoggerArgumentException;
 use Friendica\Core\Logger\Exception\LoggerException;
+use Friendica\Core\Logger\Exception\LogLevelException;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\FileSystem;
-use Friendica\Util\Introspection;
 use Psr\Log\LogLevel;
 
 /**
  * A Logger instance for logging into a stream (file, stdout, stderr)
  */
-class StreamLogger extends AbstractLogger
+class StreamLogger extends AbstractLogger implements IAmAStrategy
 {
        /**
         * The minimum loglevel at which this logger will be triggered
@@ -79,15 +82,20 @@ class StreamLogger extends AbstractLogger
 
        /**
         * {@inheritdoc}
-        * @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
         * @param string          $level  The minimum loglevel at which this logger will be triggered
         *
         * @throws LoggerArgumentException
+        * @throws LogLevelException
         */
-       public function __construct($channel, $stream, Introspection $introspection, FileSystem $fileSystem, string $level = LogLevel::DEBUG)
+       public function __construct(string $channel, IManageConfigValues $config, IHaveCallIntrospections $introspection, FileSystem $fileSystem, string $level = LogLevel::DEBUG)
        {
                $this->fileSystem = $fileSystem;
 
+               $stream = $this->logfile ?? $config->get('system', 'logfile');
+               if ((@file_exists($stream) && !@is_writable($stream)) && !@is_writable(basename($stream))) {
+                       throw new LoggerArgumentException(sprintf('%s is not a valid logfile', $stream));
+               }
+
                parent::__construct($channel, $introspection);
 
                if (is_resource($stream)) {
@@ -102,7 +110,7 @@ class StreamLogger extends AbstractLogger
                if (array_key_exists($level, $this->levelToInt)) {
                        $this->logLevel = $this->levelToInt[$level];
                } else {
-                       throw new LoggerArgumentException(sprintf('The level "%s" is not valid.', $level));
+                       throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
                }
 
                $this->checkStream();
@@ -127,12 +135,12 @@ class StreamLogger extends AbstractLogger
         * @return void
         *
         * @throws LoggerException
-        * @throws LoggerArgumentException
+        * @throws LogLevelException
         */
        protected function addEntry($level, string $message, array $context = [])
        {
                if (!array_key_exists($level, $this->levelToInt)) {
-                       throw new LoggerArgumentException(sprintf('The level "%s" is not valid.', $level));
+                       throw new LogLevelException(sprintf('The level "%s" is not valid.', $level));
                }
 
                $logLevel = $this->levelToInt[$level];