]> git.mxchange.org Git - friendica.git/blob - src/Core/Logger/Factory/Logger.php
suppress E_WARNING at tests with vfs://
[friendica.git] / src / Core / Logger / Factory / Logger.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\Hooks\Capabilities\ICanManageInstances;
26 use Friendica\Core\Logger\Exception\LogLevelException;
27 use Friendica\Core\Logger\Type\ProfilerLogger;
28 use Friendica\Core\Logger\Type\StreamLogger;
29 use Friendica\Core\Logger\Type\SyslogLogger;
30 use Psr\Log\LoggerInterface;
31 use Psr\Log\LogLevel;
32 use Psr\Log\NullLogger;
33
34 /**
35  * A logger factory
36  */
37 class Logger
38 {
39         const DEV_CHANNEL = 'dev';
40
41         /** @var string The log-channel (app, worker, ...) */
42         protected $channel;
43         /** @var ICanManageInstances */
44         protected $instanceManager;
45         /** @var IManageConfigValues */
46         protected $config;
47
48         public function __construct(string $channel, ICanManageInstances $instanceManager, IManageConfigValues $config, string $logfile = null)
49         {
50                 $this->channel         = $channel;
51                 $this->instanceManager = $instanceManager;
52                 $this->config          = $config;
53
54                 $this->instanceManager
55                         ->registerStrategy(LoggerInterface::class, 'syslog', SyslogLogger::class)
56                         ->registerStrategy(LoggerInterface::class, 'stream', StreamLogger::class, isset($logfile) ? [$logfile] : null);
57
58                 if ($this->config->get('system', 'profiling') ?? false) {
59                         $this->instanceManager->registerDecorator(LoggerInterface::class, ProfilerLogger::class);
60                 }
61         }
62
63         /**
64          * Creates a new PSR-3 compliant logger instances
65          *
66          * @param string|null $loglevel (optional) A given loglevel in case the loglevel in the config isn't applicable
67          *
68          * @return LoggerInterface The PSR-3 compliant logger instance
69          */
70         public function create(string $loglevel = null): LoggerInterface
71         {
72                 if (empty($this->config->get('system', 'debugging') ?? false)) {
73                         return new NullLogger();
74                 }
75
76                 $loglevel = $loglevel ?? static::mapLegacyConfigDebugLevel($this->config->get('system', 'loglevel'));
77                 $name     = $this->config->get('system', 'logger_config') ?? 'stream';
78
79                 try {
80                         /** @var LoggerInterface */
81                         return $this->instanceManager->getInstance(LoggerInterface::class, $name, [$this->channel, $loglevel]);
82                 } catch (LogLevelException $exception) {
83                         // If there's a wrong config value for loglevel, try again with standard
84                         $logger = $this->create(LogLevel::NOTICE);
85                         $logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
86                         return $logger;
87                 } catch (\Throwable $e) {
88                         // No logger ...
89                         return new NullLogger();
90                 }
91         }
92
93         /**
94          * Creates a new PSR-3 compliant develop logger
95          *
96          * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
97          * you'll use this logger instance for the duration of your work.
98          *
99          * It should never get filled during normal usage of Friendica
100          *
101          * @return LoggerInterface The PSR-3 compliant logger instance
102          * @throws \Exception
103          */
104         public function createDev()
105         {
106                 $debugging   = $this->config->get('system', 'debugging');
107                 $stream      = $this->config->get('system', 'dlogfile');
108                 $developerIp = $this->config->get('system', 'dlogip');
109
110                 if ((!isset($developerIp) || !$debugging) &&
111                         (!is_file($stream) || is_writable($stream))) {
112                         return new NullLogger();
113                 }
114
115                 $name = $this->config->get('system', 'logger_config') ?? 'stream';
116
117                 /** @var LoggerInterface */
118                 return $this->instanceManager->getInstance(LoggerInterface::class, $name, [self::DEV_CHANNEL, LogLevel::DEBUG, $stream]);
119         }
120
121         /**
122          * Mapping a legacy level to the PSR-3 compliant levels
123          *
124          * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
125          *
126          * @param string $level the level to be mapped
127          *
128          * @return string the PSR-3 compliant level
129          */
130         private static function mapLegacyConfigDebugLevel(string $level): string
131         {
132                 switch ($level) {
133                         // legacy WARNING
134                         case "0":
135                                 return LogLevel::ERROR;
136                         // legacy INFO
137                         case "1":
138                                 return LogLevel::WARNING;
139                         // legacy TRACE
140                         case "2":
141                                 return LogLevel::NOTICE;
142                         // legacy DEBUG
143                         case "3":
144                                 return LogLevel::INFO;
145                         // legacy DATA
146                         case "4":
147                         // legacy ALL
148                         case "5":
149                                 return LogLevel::DEBUG;
150                         // default if nothing set
151                         default:
152                                 return $level;
153                 }
154         }
155 }