3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core\Logger\Factory;
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;
32 use Psr\Log\NullLogger;
39 const DEV_CHANNEL = 'dev';
41 /** @var string The log-channel (app, worker, ...) */
43 /** @var ICanManageInstances */
44 protected $instanceManager;
45 /** @var IManageConfigValues */
48 public function __construct(string $channel, ICanManageInstances $instanceManager, IManageConfigValues $config, string $logfile = null)
50 $this->channel = $channel;
51 $this->instanceManager = $instanceManager;
52 $this->config = $config;
54 $this->instanceManager
55 ->registerStrategy(LoggerInterface::class, 'syslog', SyslogLogger::class)
56 ->registerStrategy(LoggerInterface::class, 'stream', StreamLogger::class, isset($logfile) ? [$logfile] : null);
58 if ($this->config->get('system', 'profiling') ?? false) {
59 $this->instanceManager->registerDecorator(LoggerInterface::class, ProfilerLogger::class);
64 * Creates a new PSR-3 compliant logger instances
66 * @param string|null $loglevel (optional) A given loglevel in case the loglevel in the config isn't applicable
68 * @return LoggerInterface The PSR-3 compliant logger instance
70 public function create(string $loglevel = null): LoggerInterface
72 if (empty($this->config->get('system', 'debugging') ?? false)) {
73 return new NullLogger();
76 $loglevel = $loglevel ?? static::mapLegacyConfigDebugLevel($this->config->get('system', 'loglevel'));
77 $name = $this->config->get('system', 'logger_config') ?? 'stream';
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]);
87 } catch (\Throwable $e) {
89 return new NullLogger();
94 * Creates a new PSR-3 compliant develop logger
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.
99 * It should never get filled during normal usage of Friendica
101 * @return LoggerInterface The PSR-3 compliant logger instance
104 public function createDev()
106 $debugging = $this->config->get('system', 'debugging');
107 $stream = $this->config->get('system', 'dlogfile');
108 $developerIp = $this->config->get('system', 'dlogip');
110 if ((!isset($developerIp) || !$debugging) &&
111 (!is_file($stream) || is_writable($stream))) {
112 return new NullLogger();
115 $name = $this->config->get('system', 'logger_config') ?? 'stream';
117 /** @var LoggerInterface */
118 return $this->instanceManager->getInstance(LoggerInterface::class, $name, [self::DEV_CHANNEL, LogLevel::DEBUG, $stream]);
122 * Mapping a legacy level to the PSR-3 compliant levels
124 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
126 * @param string $level the level to be mapped
128 * @return string the PSR-3 compliant level
130 private static function mapLegacyConfigDebugLevel(string $level): string
135 return LogLevel::ERROR;
138 return LogLevel::WARNING;
141 return LogLevel::NOTICE;
144 return LogLevel::INFO;
149 return LogLevel::DEBUG;
150 // default if nothing set