]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger/Factory/StreamLogger.php
Use the owner, not the author
[friendica.git] / src / Core / Logger / Factory / StreamLogger.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Logger\Factory;
23
24 use Friendica\Core\Config\Capability\IManageConfigValues;
25 use Friendica\Core\Logger\Capability\LogChannel;
26 use Friendica\Core\Logger\Exception\LoggerArgumentException;
27 use Friendica\Core\Logger\Exception\LoggerException;
28 use Friendica\Core\Logger\Exception\LogLevelException;
29 use Friendica\Core\Logger\Type\StreamLogger as StreamLoggerClass;
30 use Friendica\Core\Logger\Util\FileSystem;
31 use Psr\Log\LoggerInterface;
32 use Psr\Log\NullLogger;
33
34 /**
35  * The logger factory for the StreamLogger instance
36  *
37  * @see StreamLoggerClass
38  */
39 class StreamLogger extends AbstractLoggerTypeFactory
40 {
41         /**
42          * Creates a new PSR-3 compliant stream logger instance
43          *
44          * @param IManageConfigValues $config   The system configuration
45          * @param string|null         $logfile  (optional) A given logfile which should be used as stream (e.g. in case of
46          *                                      developer logging)
47          * @param string|null         $channel  (optional) A given channel in case it is different from the default
48          *
49          * @return LoggerInterface The PSR-3 compliant logger instance
50          *
51          * @throws LoggerException in case the logger cannot get created
52          */
53         public function create(IManageConfigValues $config, string $logfile = null, string $channel = null): LoggerInterface
54         {
55                 $fileSystem = new FileSystem();
56
57                 $logfile = $logfile ?? $config->get('system', 'logfile');
58                 if (!@file_exists($logfile) || !@is_writable($logfile)) {
59                         throw new LoggerArgumentException(sprintf('%s is not a valid logfile', $logfile));
60                 }
61
62                 $loglevel = static::mapLegacyConfigDebugLevel($config->get('system', 'loglevel'));
63
64                 if (array_key_exists($loglevel, StreamLoggerClass::levelToInt)) {
65                         $loglevel = StreamLoggerClass::levelToInt[$loglevel];
66                 } else {
67                         throw new LogLevelException(sprintf('The level "%s" is not valid.', $loglevel));
68                 }
69
70                 $stream = $fileSystem->createStream($logfile);
71
72                 return new StreamLoggerClass($channel ?? $this->channel, $this->introspection, $stream, $loglevel, getmypid());
73         }
74
75         /**
76          * Creates a new PSR-3 compliant develop logger
77          *
78          * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
79          * you'll use this logger instance for the duration of your work.
80          *
81          * It should never get filled during normal usage of Friendica
82          *
83          * @return LoggerInterface The PSR-3 compliant logger instance
84          *
85          * @throws LoggerException
86          */
87         public function createDev(IManageConfigValues $config)
88         {
89                 $debugging   = $config->get('system', 'debugging');
90                 $logfile     = $config->get('system', 'dlogfile');
91                 $developerIp = $config->get('system', 'dlogip');
92
93                 if ((!isset($developerIp) || !$debugging) &&
94                         (!is_file($logfile) || is_writable($logfile))) {
95                         return new NullLogger();
96                 }
97
98                 return $this->create($config, $logfile, LogChannel::DEV);
99         }
100 }