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\Capabilities\IHaveCallIntrospections;
27 use Friendica\Core\Logger\Exception\LogLevelException;
28 use Friendica\Database\Database;
29 use Friendica\Network\HTTPException\InternalServerErrorException;
30 use Friendica\Util\FileSystem;
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';
46 /** @var string The log-channel (app, worker, ...) */
49 public function __construct(string $channel, bool $includeAddon = true)
51 $this->channel = $channel;
53 /// @fixme clean solution = Making Addon & Hook dynamic and load them inside the constructor, so there's no custom load logic necessary anymore
55 Core\Addon::loadAddons();
56 Core\Hook::loadHooks();
61 * Creates a new PSR-3 compliant logger instances
63 * @param Database $database The Friendica Database instance
64 * @param IManageConfigValues $config The config
65 * @param Profiler $profiler The profiler of the app
66 * @param FileSystem $fileSystem FileSystem utils
67 * @param string|null $minLevel (optional) Override minimum Loglevel to log
69 * @return LoggerInterface The PSR-3 compliant logger instance
71 public function create(Database $database, IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, IHaveCallIntrospections $introspection, ?string $minLevel = null): LoggerInterface
73 if (empty($config->get('system', 'debugging', false))) {
74 $logger = new NullLogger();
75 $database->setLogger($logger);
79 $minLevel = $minLevel ?? $config->get('system', 'loglevel');
80 $loglevel = self::mapLegacyConfigDebugLevel((string)$minLevel);
82 $name = $config->get('system', 'logger_config', 'stream');
87 $logger = new SyslogLogger($this->channel, $introspection, $loglevel, $config->get('system', 'syslog_flags', SyslogLogger::DEFAULT_FLAGS), $config->get('system', 'syslog_facility', SyslogLogger::DEFAULT_FACILITY));
88 } catch (LogLevelException $exception) {
89 // If there's a wrong config value for loglevel, try again with standard
90 $logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
91 $logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
92 } catch (\Throwable $e) {
94 $logger = new NullLogger();
102 'channel' => $this->channel,
103 'introspection' => $introspection,
104 'loglevel' => $loglevel,
108 Core\Hook::callAll('logger_instance', $data);
109 } catch (InternalServerErrorException $exception) {
110 $data['logger'] = null;
113 if (($data['logger'] ?? null) instanceof LoggerInterface) {
114 $logger = $data['logger'];
117 if (empty($logger)) {
118 $stream = $config->get('system', 'logfile');
119 // just add a stream in case it's either writable or not file
120 if (!is_file($stream) || is_writable($stream)) {
122 $logger = new StreamLogger($this->channel, $stream, $introspection, $fileSystem, $loglevel);
123 } catch (LogLevelException $exception) {
124 // If there's a wrong config value for loglevel, try again with standard
125 $logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
126 $logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
127 } catch (\Throwable $t) {
129 $logger = new NullLogger();
133 $logger = new SyslogLogger($this->channel, $introspection, $loglevel);
134 } catch (LogLevelException $exception) {
135 // If there's a wrong config value for loglevel, try again with standard
136 $logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
137 $logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
138 } catch (\Throwable $e) {
140 $logger = new NullLogger();
147 $profiling = $config->get('system', 'profiling', false);
149 // In case profiling is enabled, wrap the ProfilerLogger around the current logger
150 if (isset($profiling) && $profiling !== false) {
151 $logger = new ProfilerLogger($logger, $profiler);
154 $database->setLogger($logger);
159 * Creates a new PSR-3 compliant develop logger
161 * If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
162 * you'll use this logger instance for the duration of your work.
164 * It should never get filled during normal usage of Friendica
166 * @param IManageConfigValues $config The config
167 * @param Profiler $profiler The profiler of the app
168 * @param FileSystem $fileSystem FileSystem utils
170 * @return LoggerInterface The PSR-3 compliant logger instance
173 public static function createDev(IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, IHaveCallIntrospections $introspection)
175 $debugging = $config->get('system', 'debugging');
176 $stream = $config->get('system', 'dlogfile');
177 $developerIp = $config->get('system', 'dlogip');
179 if ((!isset($developerIp) || !$debugging) &&
180 (!is_file($stream) || is_writable($stream))) {
181 return new NullLogger();
184 $name = $config->get('system', 'logger_config', 'stream');
189 $logger = new SyslogLogger(self::DEV_CHANNEL, $introspection, LogLevel::DEBUG);
196 'channel' => self::DEV_CHANNEL,
197 'introspection' => $introspection,
198 'loglevel' => LogLevel::DEBUG,
202 Core\Hook::callAll('logger_instance', $data);
203 } catch (InternalServerErrorException $exception) {
204 $data['logger'] = null;
207 if (($data['logger'] ?? null) instanceof LoggerInterface) {
208 return $data['logger'];
211 $logger = new StreamLogger(self::DEV_CHANNEL, $stream, $introspection, $fileSystem, LogLevel::DEBUG);
215 $profiling = $config->get('system', 'profiling', false);
217 // In case profiling is enabled, wrap the ProfilerLogger around the current logger
218 if (isset($profiling) && $profiling !== false) {
219 $logger = new ProfilerLogger($logger, $profiler);
226 * Mapping a legacy level to the PSR-3 compliant levels
228 * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
230 * @param string $level the level to be mapped
232 * @return string the PSR-3 compliant level
234 private static function mapLegacyConfigDebugLevel(string $level): string
239 return LogLevel::ERROR;
242 return LogLevel::WARNING;
245 return LogLevel::NOTICE;
248 return LogLevel::INFO;
253 return LogLevel::DEBUG;
254 // default if nothing set