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\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;
35 * The logger factory for the StreamLogger instance
37 * @see StreamLoggerClass
39 class StreamLogger extends AbstractLoggerTypeFactory
42 * Creates a new PSR-3 compliant stream logger instance
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
47 * @param string|null $channel (optional) A given channel in case it is different from the default
49 * @return LoggerInterface The PSR-3 compliant logger instance
51 * @throws LoggerException in case the logger cannot get created
53 public function create(IManageConfigValues $config, string $logfile = null, string $channel = null): LoggerInterface
55 $fileSystem = new FileSystem();
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));
62 $loglevel = static::mapLegacyConfigDebugLevel($config->get('system', 'loglevel'));
64 if (array_key_exists($loglevel, StreamLoggerClass::levelToInt)) {
65 $loglevel = StreamLoggerClass::levelToInt[$loglevel];
67 throw new LogLevelException(sprintf('The level "%s" is not valid.', $loglevel));
70 $stream = $fileSystem->createStream($logfile);
72 return new StreamLoggerClass($channel ?? $this->channel, $this->introspection, $stream, $loglevel, getmypid());
76 * Creates a new PSR-3 compliant develop logger
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.
81 * It should never get filled during normal usage of Friendica
83 * @return LoggerInterface The PSR-3 compliant logger instance
85 * @throws LoggerException
87 public function createDev(IManageConfigValues $config)
89 $debugging = $config->get('system', 'debugging');
90 $logfile = $config->get('system', 'dlogfile');
91 $developerIp = $config->get('system', 'dlogip');
93 if ((!isset($developerIp) || !$debugging) &&
94 (!is_file($logfile) || is_writable($logfile))) {
95 return new NullLogger();
98 return $this->create($config, $logfile, LogChannel::DEV);