2018-12-30 21:42:56 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2022-01-02 08:27:47 +01:00
|
|
|
* @copyright Copyright (C) 2010-2022, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-12-30 21:42:56 +01:00
|
|
|
|
2021-10-23 12:22:27 +02:00
|
|
|
namespace Friendica\Core\Logger\Factory;
|
2018-12-30 21:42:56 +01:00
|
|
|
|
2021-10-26 21:44:29 +02:00
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
2021-10-23 12:22:27 +02:00
|
|
|
use Friendica\Core;
|
2022-12-25 16:48:09 +01:00
|
|
|
use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
|
2021-10-29 08:03:59 +02:00
|
|
|
use Friendica\Core\Logger\Exception\LogLevelException;
|
2019-06-07 00:10:45 +02:00
|
|
|
use Friendica\Database\Database;
|
2022-10-17 21:25:04 +02:00
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
2019-10-22 22:47:37 +02:00
|
|
|
use Friendica\Util\FileSystem;
|
2021-10-23 12:22:27 +02:00
|
|
|
use Friendica\Core\Logger\Type\ProfilerLogger;
|
|
|
|
use Friendica\Core\Logger\Type\StreamLogger;
|
|
|
|
use Friendica\Core\Logger\Type\SyslogLogger;
|
2019-02-17 01:56:46 +01:00
|
|
|
use Friendica\Util\Profiler;
|
2018-12-30 21:42:56 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\LogLevel;
|
2021-10-23 12:26:06 +02:00
|
|
|
use Psr\Log\NullLogger;
|
2018-12-30 21:42:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A logger factory
|
|
|
|
*/
|
2021-10-23 12:22:27 +02:00
|
|
|
class Logger
|
2018-12-30 21:42:56 +01:00
|
|
|
{
|
2019-07-21 20:24:16 +02:00
|
|
|
const DEV_CHANNEL = 'dev';
|
|
|
|
|
2021-10-23 12:22:27 +02:00
|
|
|
/** @var string The log-channel (app, worker, ...) */
|
2019-09-17 16:47:00 +02:00
|
|
|
private $channel;
|
|
|
|
|
2022-10-17 21:25:04 +02:00
|
|
|
public function __construct(string $channel, bool $includeAddon = true)
|
2019-07-21 20:24:16 +02:00
|
|
|
{
|
2019-09-17 16:47:00 +02:00
|
|
|
$this->channel = $channel;
|
2022-10-17 21:25:04 +02:00
|
|
|
|
|
|
|
/// @fixme clean solution = Making Addon & Hook dynamic and load them inside the constructor, so there's no custom load logic necessary anymore
|
|
|
|
if ($includeAddon) {
|
|
|
|
Core\Addon::loadAddons();
|
|
|
|
Core\Hook::loadHooks();
|
|
|
|
}
|
2019-07-21 20:24:16 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 21:42:56 +01:00
|
|
|
/**
|
|
|
|
* Creates a new PSR-3 compliant logger instances
|
|
|
|
*
|
2021-10-26 22:09:11 +02:00
|
|
|
* @param Database $database The Friendica Database instance
|
|
|
|
* @param IManageConfigValues $config The config
|
|
|
|
* @param Profiler $profiler The profiler of the app
|
|
|
|
* @param FileSystem $fileSystem FileSystem utils
|
2021-10-29 08:03:59 +02:00
|
|
|
* @param string|null $minLevel (optional) Override minimum Loglevel to log
|
2018-12-30 21:42:56 +01:00
|
|
|
*
|
|
|
|
* @return LoggerInterface The PSR-3 compliant logger instance
|
|
|
|
*/
|
2022-12-25 16:48:09 +01:00
|
|
|
public function create(Database $database, IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, IHaveCallIntrospections $introspection, ?string $minLevel = null): LoggerInterface
|
2018-12-30 21:42:56 +01:00
|
|
|
{
|
2019-02-27 18:55:26 +01:00
|
|
|
if (empty($config->get('system', 'debugging', false))) {
|
2021-10-23 12:26:06 +02:00
|
|
|
$logger = new NullLogger();
|
2019-06-07 00:10:45 +02:00
|
|
|
$database->setLogger($logger);
|
2019-03-02 14:40:59 +01:00
|
|
|
return $logger;
|
2019-02-27 18:55:26 +01:00
|
|
|
}
|
|
|
|
|
2021-10-29 08:03:59 +02:00
|
|
|
$minLevel = $minLevel ?? $config->get('system', 'loglevel');
|
|
|
|
$loglevel = self::mapLegacyConfigDebugLevel((string)$minLevel);
|
2019-02-28 08:56:28 +01:00
|
|
|
|
2022-10-17 21:25:04 +02:00
|
|
|
$name = $config->get('system', 'logger_config', 'stream');
|
2019-03-03 20:32:27 +01:00
|
|
|
|
2022-10-17 21:25:04 +02:00
|
|
|
switch ($name) {
|
2019-03-03 20:32:27 +01:00
|
|
|
case 'syslog':
|
2019-10-21 21:53:55 +02:00
|
|
|
try {
|
2021-12-12 22:45:22 +01:00
|
|
|
$logger = new SyslogLogger($this->channel, $introspection, $loglevel, $config->get('system', 'syslog_flags', SyslogLogger::DEFAULT_FLAGS), $config->get('system', 'syslog_facility', SyslogLogger::DEFAULT_FACILITY));
|
2021-10-29 08:03:59 +02:00
|
|
|
} catch (LogLevelException $exception) {
|
|
|
|
// If there's a wrong config value for loglevel, try again with standard
|
2022-12-25 16:48:09 +01:00
|
|
|
$logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
|
2021-10-29 08:03:59 +02:00
|
|
|
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
|
2019-10-21 21:53:55 +02:00
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// No logger ...
|
2021-10-23 12:26:06 +02:00
|
|
|
$logger = new NullLogger();
|
2019-10-21 21:53:55 +02:00
|
|
|
}
|
2019-03-03 20:32:27 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
|
|
|
default:
|
2022-10-17 21:25:04 +02:00
|
|
|
$data = [
|
|
|
|
'name' => $name,
|
|
|
|
'channel' => $this->channel,
|
|
|
|
'introspection' => $introspection,
|
|
|
|
'loglevel' => $loglevel,
|
|
|
|
'logger' => null,
|
|
|
|
];
|
|
|
|
try {
|
|
|
|
Core\Hook::callAll('logger_instance', $data);
|
|
|
|
} catch (InternalServerErrorException $exception) {
|
|
|
|
$data['logger'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($data['logger'] ?? null) instanceof LoggerInterface) {
|
|
|
|
$logger = $data['logger'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($logger)) {
|
|
|
|
$stream = $config->get('system', 'logfile');
|
|
|
|
// just add a stream in case it's either writable or not file
|
|
|
|
if (!is_file($stream) || is_writable($stream)) {
|
|
|
|
try {
|
|
|
|
$logger = new StreamLogger($this->channel, $stream, $introspection, $fileSystem, $loglevel);
|
|
|
|
} catch (LogLevelException $exception) {
|
|
|
|
// If there's a wrong config value for loglevel, try again with standard
|
2022-12-25 16:48:09 +01:00
|
|
|
$logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
|
2022-10-17 21:25:04 +02:00
|
|
|
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
|
|
|
|
} catch (\Throwable $t) {
|
|
|
|
// No logger ...
|
|
|
|
$logger = new NullLogger();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
$logger = new SyslogLogger($this->channel, $introspection, $loglevel);
|
|
|
|
} catch (LogLevelException $exception) {
|
|
|
|
// If there's a wrong config value for loglevel, try again with standard
|
2022-12-25 16:48:09 +01:00
|
|
|
$logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
|
2022-10-17 21:25:04 +02:00
|
|
|
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
// No logger ...
|
|
|
|
$logger = new NullLogger();
|
|
|
|
}
|
2021-10-29 08:03:59 +02:00
|
|
|
}
|
2019-05-26 14:33:09 +02:00
|
|
|
}
|
2019-03-03 20:32:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$profiling = $config->get('system', 'profiling', false);
|
|
|
|
|
|
|
|
// In case profiling is enabled, wrap the ProfilerLogger around the current logger
|
|
|
|
if (isset($profiling) && $profiling !== false) {
|
|
|
|
$logger = new ProfilerLogger($logger, $profiler);
|
2019-02-03 22:22:04 +01:00
|
|
|
}
|
|
|
|
|
2019-06-07 00:10:45 +02:00
|
|
|
$database->setLogger($logger);
|
2018-12-30 21:42:56 +01:00
|
|
|
return $logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new PSR-3 compliant develop logger
|
|
|
|
*
|
|
|
|
* If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
|
|
|
|
* you'll use this logger instance for the duration of your work.
|
|
|
|
*
|
|
|
|
* It should never get filled during normal usage of Friendica
|
|
|
|
*
|
2021-10-26 22:09:11 +02:00
|
|
|
* @param IManageConfigValues $config The config
|
|
|
|
* @param Profiler $profiler The profiler of the app
|
|
|
|
* @param FileSystem $fileSystem FileSystem utils
|
2018-12-30 21:42:56 +01:00
|
|
|
*
|
|
|
|
* @return LoggerInterface The PSR-3 compliant logger instance
|
2019-03-03 20:32:27 +01:00
|
|
|
* @throws \Exception
|
2018-12-30 21:42:56 +01:00
|
|
|
*/
|
2022-12-25 16:48:09 +01:00
|
|
|
public static function createDev(IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, IHaveCallIntrospections $introspection)
|
2018-12-30 21:42:56 +01:00
|
|
|
{
|
2019-02-11 21:13:53 +01:00
|
|
|
$debugging = $config->get('system', 'debugging');
|
|
|
|
$stream = $config->get('system', 'dlogfile');
|
|
|
|
$developerIp = $config->get('system', 'dlogip');
|
|
|
|
|
2019-05-26 14:33:09 +02:00
|
|
|
if ((!isset($developerIp) || !$debugging) &&
|
2021-10-23 12:22:27 +02:00
|
|
|
(!is_file($stream) || is_writable($stream))) {
|
2021-10-23 12:26:06 +02:00
|
|
|
return new NullLogger();
|
2019-02-11 21:13:53 +01:00
|
|
|
}
|
|
|
|
|
2022-10-17 21:25:04 +02:00
|
|
|
$name = $config->get('system', 'logger_config', 'stream');
|
2018-12-30 21:42:56 +01:00
|
|
|
|
2022-10-17 21:25:04 +02:00
|
|
|
switch ($name) {
|
2018-12-30 21:42:56 +01:00
|
|
|
|
2019-03-03 20:32:27 +01:00
|
|
|
case 'syslog':
|
2019-07-21 20:24:16 +02:00
|
|
|
$logger = new SyslogLogger(self::DEV_CHANNEL, $introspection, LogLevel::DEBUG);
|
2019-03-03 20:32:27 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'stream':
|
|
|
|
default:
|
2022-10-17 21:25:04 +02:00
|
|
|
$data = [
|
|
|
|
'name' => $name,
|
|
|
|
'channel' => self::DEV_CHANNEL,
|
|
|
|
'introspection' => $introspection,
|
|
|
|
'loglevel' => LogLevel::DEBUG,
|
|
|
|
'logger' => null,
|
|
|
|
];
|
|
|
|
try {
|
|
|
|
Core\Hook::callAll('logger_instance', $data);
|
|
|
|
} catch (InternalServerErrorException $exception) {
|
|
|
|
$data['logger'] = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (($data['logger'] ?? null) instanceof LoggerInterface) {
|
|
|
|
return $data['logger'];
|
|
|
|
}
|
|
|
|
|
2019-10-22 22:47:37 +02:00
|
|
|
$logger = new StreamLogger(self::DEV_CHANNEL, $stream, $introspection, $fileSystem, LogLevel::DEBUG);
|
2019-03-03 20:32:27 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$profiling = $config->get('system', 'profiling', false);
|
|
|
|
|
|
|
|
// In case profiling is enabled, wrap the ProfilerLogger around the current logger
|
|
|
|
if (isset($profiling) && $profiling !== false) {
|
|
|
|
$logger = new ProfilerLogger($logger, $profiler);
|
|
|
|
}
|
2019-02-11 21:13:53 +01:00
|
|
|
|
2018-12-30 21:42:56 +01:00
|
|
|
return $logger;
|
|
|
|
}
|
|
|
|
|
2019-02-11 21:13:53 +01:00
|
|
|
/**
|
|
|
|
* Mapping a legacy level to the PSR-3 compliant levels
|
2019-05-26 14:33:09 +02:00
|
|
|
*
|
2019-02-11 21:13:53 +01:00
|
|
|
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md#5-psrlogloglevel
|
|
|
|
*
|
|
|
|
* @param string $level the level to be mapped
|
|
|
|
*
|
|
|
|
* @return string the PSR-3 compliant level
|
|
|
|
*/
|
2021-10-23 12:22:27 +02:00
|
|
|
private static function mapLegacyConfigDebugLevel(string $level): string
|
2019-02-11 21:13:53 +01:00
|
|
|
{
|
|
|
|
switch ($level) {
|
|
|
|
// legacy WARNING
|
|
|
|
case "0":
|
|
|
|
return LogLevel::ERROR;
|
|
|
|
// legacy INFO
|
|
|
|
case "1":
|
|
|
|
return LogLevel::WARNING;
|
|
|
|
// legacy TRACE
|
|
|
|
case "2":
|
|
|
|
return LogLevel::NOTICE;
|
|
|
|
// legacy DEBUG
|
|
|
|
case "3":
|
|
|
|
return LogLevel::INFO;
|
|
|
|
// legacy DATA
|
2019-10-21 21:53:55 +02:00
|
|
|
case "4":
|
2019-10-22 22:48:54 +02:00
|
|
|
// legacy ALL
|
|
|
|
case "5":
|
2019-10-22 22:47:37 +02:00
|
|
|
return LogLevel::DEBUG;
|
2019-02-11 21:13:53 +01:00
|
|
|
// default if nothing set
|
|
|
|
default:
|
|
|
|
return $level;
|
|
|
|
}
|
|
|
|
}
|
2018-12-30 21:42:56 +01:00
|
|
|
}
|