3 * @copyright Copyright (C) 2010-2022, 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;
26 use Friendica\Core\Logger\Exception\LogLevelException;
27 use Friendica\Database\Database;
28 use Friendica\Network\HTTPException\InternalServerErrorException;
29 use Friendica\Util\FileSystem;
30 use Friendica\Core\Logger\Util\Introspection;
31 use Friendica\Core\Logger\Type\ProfilerLogger;
32 use Friendica\Core\Logger\Type\StreamLogger;
33 use Friendica\Core\Logger\Type\SyslogLogger;
34 use Friendica\Util\Profiler;
35 use Psr\Log\LoggerInterface;
37 use Psr\Log\NullLogger;
44 const DEV_CHANNEL = 'dev';
47 * A list of classes, which shouldn't get logged
51 private static $ignoreClassList = [
54 'Friendica\\Core\\Logger\\Type',
57 /** @var string The log-channel (app, worker, ...) */
60 public function __construct(string $channel, bool $includeAddon = true)
62 $this->channel = $channel;
64 /// @fixme clean solution = Making Addon & Hook dynamic and load them inside the constructor, so there's no custom load logic necessary anymore
66 Core\Addon::loadAddons();
67 Core\Hook::loadHooks();
72 * Creates a new PSR-3 compliant logger instances
74 * @param Database $database The Friendica Database instance
75 * @param IManageConfigValues $config The config
76 * @param Profiler $profiler The profiler of the app
77 * @param FileSystem $fileSystem FileSystem utils
78 * @param string|null $minLevel (optional) Override minimum Loglevel to log
80 * @return LoggerInterface The PSR-3 compliant logger instance
82 public function create(Database $database, IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, ?string $minLevel = null): LoggerInterface
84 if (empty($config->get('system', 'debugging', false))) {
85 $logger = new NullLogger();
86 $database->setLogger($logger);
90 $introspection = new Introspection(self::$ignoreClassList);
91 $minLevel = $minLevel ?? $config->get('system', 'loglevel');
92 $loglevel = self::mapLegacyConfigDebugLevel((string)$minLevel);
94 $name = $config->get('system', 'logger_config', 'stream');
99 $logger = new SyslogLogger($this->channel, $introspection, $loglevel, $config->get('system', 'syslog_flags', SyslogLogger::DEFAULT_FLAGS), $config->get('system', 'syslog_facility', SyslogLogger::DEFAULT_FACILITY));
100 } catch (LogLevelException $exception) {
101 // If there's a wrong config value for loglevel, try again with standard
102 $logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
103 $logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
104 } catch (\Throwable $e) {
106 $logger = new NullLogger();
114 'channel' => $this->channel,
115 'introspection' => $introspection,
116 'loglevel' => $loglevel,
120 Core\Hook::callAll('logger_instance', $data);
121 } catch (InternalServerErrorException $exception) {
122 $data['logger'] = null;
125 if (($data['logger'] ?? null) instanceof LoggerInterface) {
126 $logger = $data['logger'];
129 if (empty($logger)) {
130 $stream = $config->get('system', 'logfile');
131 // just add a stream in case it's either writable or not file
132 if (!is_file($stream) || is_writable($stream)) {
134 $logger = new StreamLogger($this->channel, $stream, $introspection, $fileSystem, $loglevel);
135 } catch (LogLevelException $exception) {
136 // If there's a wrong config value for loglevel, try again with standard
137 $logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
138 $logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
139 } catch (\Throwable $t) {
141 $logger = new NullLogger();
145 $logger = new SyslogLogger($this->channel, $introspection, $loglevel);
146 } catch (LogLevelException $exception) {
147 // If there's a wrong config value for loglevel, try again with standard
148 $logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
149 $logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
150 } catch (\Throwable $e) {
152 $logger = new NullLogger();
159 $profiling = $config->get('system', 'profiling', false);
161 // In case profiling is enabled, wrap the ProfilerLogger around the current logger
162 if (isset($profiling) && $profiling !== false) {
163 $logger = new ProfilerLogger($logger, $profiler);
166 $database->setLogger($logger);
171 * Creates a new PSR-3 compliant develop logger
173 * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
174 * you'll use this logger instance for the duration of your work.
176 * It should never get filled during normal usage of Friendica
178 * @param IManageConfigValues $config The config
179 * @param Profiler $profiler The profiler of the app
180 * @param FileSystem $fileSystem FileSystem utils
182 * @return LoggerInterface The PSR-3 compliant logger instance
185 public static function createDev(IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem)
187 $debugging = $config->get('system', 'debugging');
188 $stream = $config->get('system', 'dlogfile');
189 $developerIp = $config->get('system', 'dlogip');
191 if ((!isset($developerIp) || !$debugging) &&
192 (!is_file($stream) || is_writable($stream))) {
193 return new NullLogger();
196 $introspection = new Introspection(self::$ignoreClassList);
198 $name = $config->get('system', 'logger_config', 'stream');
203 $logger = new SyslogLogger(self::DEV_CHANNEL, $introspection, LogLevel::DEBUG);
210 'channel' => self::DEV_CHANNEL,
211 'introspection' => $introspection,
212 'loglevel' => LogLevel::DEBUG,
216 Core\Hook::callAll('logger_instance', $data);
217 } catch (InternalServerErrorException $exception) {
218 $data['logger'] = null;
221 if (($data['logger'] ?? null) instanceof LoggerInterface) {
222 return $data['logger'];
225 $logger = new StreamLogger(self::DEV_CHANNEL, $stream, $introspection, $fileSystem, LogLevel::DEBUG);
229 $profiling = $config->get('system', 'profiling', false);
231 // In case profiling is enabled, wrap the ProfilerLogger around the current logger
232 if (isset($profiling) && $profiling !== false) {
233 $logger = new ProfilerLogger($logger, $profiler);
240 * Mapping a legacy level to the PSR-3 compliant levels
242 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
244 * @param string $level the level to be mapped
246 * @return string the PSR-3 compliant level
248 private static function mapLegacyConfigDebugLevel(string $level): string
253 return LogLevel::ERROR;
256 return LogLevel::WARNING;
259 return LogLevel::NOTICE;
262 return LogLevel::INFO;
267 return LogLevel::DEBUG;
268 // default if nothing set