Introduce dynamic hook loading

- Dynamically load addon files
- Dynamically load hooks
- Rewrite Logger-logic to use new hook logic (Monolog is working again)
This commit is contained in:
Philipp 2023-07-02 23:56:56 +02:00
parent ff092833ae
commit 14b76e48f0
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
39 changed files with 1163 additions and 469 deletions

View file

@ -0,0 +1,80 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Core\Logger\Factory;
use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
use Psr\Log\LogLevel;
/**
* Abstract class for creating logger types, which includes common necessary logic/content
*/
abstract class AbstractLoggerTypeFactory
{
/** @var string */
protected $channel;
/** @var IHaveCallIntrospections */
protected $introspection;
/**
* @param string $channel The channel for the logger
*/
public function __construct(IHaveCallIntrospections $introspection, string $channel)
{
$this->channel = $channel;
$this->introspection = $introspection;
}
/**
* Mapping a legacy level to the PSR-3 compliant levels
*
* @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
*/
protected static function mapLegacyConfigDebugLevel(string $level): string
{
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
case "4":
// legacy ALL
case "5":
return LogLevel::DEBUG;
// default if nothing set
default:
return $level;
}
}
}

View file

@ -22,134 +22,38 @@
namespace Friendica\Core\Logger\Factory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hooks\Capabilities\ICanManageInstances;
use Friendica\Core\Logger\Exception\LogLevelException;
use Friendica\Core\Logger\Type\ProfilerLogger;
use Friendica\Core\Logger\Type\StreamLogger;
use Friendica\Core\Logger\Type\SyslogLogger;
use Friendica\Core\Hooks\Capabilities\ICanCreateInstances;
use Friendica\Core\Logger\Capabilities\LogChannel;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
use Throwable;
/**
* A logger factory
* The logger factory for the core logging instances
*/
class Logger
{
const DEV_CHANNEL = 'dev';
/** @var string The log-channel (app, worker, ...) */
/** @var string The channel */
protected $channel;
/** @var ICanManageInstances */
protected $instanceManager;
/** @var IManageConfigValues */
protected $config;
public function __construct(string $channel, ICanManageInstances $instanceManager, IManageConfigValues $config, string $logfile = null)
public function __construct(string $channel = LogChannel::DEFAULT)
{
$this->channel = $channel;
$this->instanceManager = $instanceManager;
$this->config = $config;
$this->instanceManager
->registerStrategy(LoggerInterface::class, 'syslog', SyslogLogger::class)
->registerStrategy(LoggerInterface::class, 'stream', StreamLogger::class, isset($logfile) ? [$logfile] : null);
if ($this->config->get('system', 'profiling') ?? false) {
$this->instanceManager->registerDecorator(LoggerInterface::class, ProfilerLogger::class);
}
$this->channel = $channel;
}
/**
* Creates a new PSR-3 compliant logger instances
*
* @param string|null $loglevel (optional) A given loglevel in case the loglevel in the config isn't applicable
*
* @return LoggerInterface The PSR-3 compliant logger instance
*/
public function create(string $loglevel = null): LoggerInterface
public function create(ICanCreateInstances $createInstances, IManageConfigValues $config): LoggerInterface
{
if (empty($this->config->get('system', 'debugging') ?? false)) {
if (empty($config->get('system', 'debugging') ?? false)) {
return new NullLogger();
}
$loglevel = $loglevel ?? static::mapLegacyConfigDebugLevel($this->config->get('system', 'loglevel'));
$name = $this->config->get('system', 'logger_config') ?? 'stream';
$name = $config->get('system', 'logger_config') ?? '';
try {
/** @var LoggerInterface */
return $this->instanceManager->getInstance(LoggerInterface::class, $name, [$this->channel, $loglevel]);
} catch (LogLevelException $exception) {
// If there's a wrong config value for loglevel, try again with standard
$logger = $this->create(LogLevel::NOTICE);
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
return $logger;
} catch (\Throwable $e) {
return $createInstances->createWithName(LoggerInterface::class, $name, [$this->channel]);
} catch (Throwable $e) {
// No logger ...
return new NullLogger();
}
}
/**
* 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
*
* @return LoggerInterface The PSR-3 compliant logger instance
* @throws \Exception
*/
public function createDev()
{
$debugging = $this->config->get('system', 'debugging');
$stream = $this->config->get('system', 'dlogfile');
$developerIp = $this->config->get('system', 'dlogip');
if ((!isset($developerIp) || !$debugging) &&
(!is_file($stream) || is_writable($stream))) {
return new NullLogger();
}
$name = $this->config->get('system', 'logger_config') ?? 'stream';
/** @var LoggerInterface */
return $this->instanceManager->getInstance(LoggerInterface::class, $name, [self::DEV_CHANNEL, LogLevel::DEBUG, $stream]);
}
/**
* Mapping a legacy level to the PSR-3 compliant levels
*
* @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
*/
private static function mapLegacyConfigDebugLevel(string $level): string
{
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
case "4":
// legacy ALL
case "5":
return LogLevel::DEBUG;
// default if nothing set
default:
return $level;
}
}
}

View file

@ -0,0 +1,53 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Core\Logger\Factory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Logger\Type\ProfilerLogger as ProfilerLoggerClass;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/**
* The logger factory for the ProfilerLogger
*
* @see ProfilerLoggerClass
*/
class ProfilerLogger extends AbstractLoggerTypeFactory
{
/**
* Wraps a given Logger with profiling information in case profiling is enabled
*
* @param IManageConfigValues $config The system configuration
* @param LoggerInterface $logger The given logger class, which should get wrapped
* @param Profiler $profiler The profiler utility
*
* @return LoggerInterface The PSR-3 compliant logger instance
*/
public function create(IManageConfigValues $config, LoggerInterface $logger, Profiler $profiler): LoggerInterface
{
if ($config->get('system', 'profiling') ?? false) {
return $logger;
} else {
return new ProfilerLoggerClass($logger, $profiler);
}
}
}

View file

@ -0,0 +1,100 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Core\Logger\Factory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Logger\Capabilities\LogChannel;
use Friendica\Core\Logger\Exception\LoggerArgumentException;
use Friendica\Core\Logger\Exception\LoggerException;
use Friendica\Core\Logger\Type\StreamLogger as StreamLoggerClass;
use Friendica\Core\Logger\Util\FileSystem;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
/**
* The logger factory for the StreamLogger instance
*
* @see StreamLoggerClass
*/
class StreamLogger extends AbstractLoggerTypeFactory
{
/**
* Creates a new PSR-3 compliant stream logger instance
*
* @param IManageConfigValues $config The system configuration
* @param string|null $logfile (optional) A given logfile which should be used as stream (e.g. in case of
* developer logging)
* @param string|null $channel (optional) A given channel in case it is different from the default
*
* @return LoggerInterface The PSR-3 compliant logger instance
*
* @throws LoggerException in case the logger cannot get created
*/
public function create(IManageConfigValues $config, string $logfile = null, string $channel = null): LoggerInterface
{
$fileSystem = new FileSystem();
$logfile = $logfile ?? $config->get('system', 'logfile');
if ((@file_exists($logfile) && !@is_writable($logfile)) && !@is_writable(basename($logfile))) {
throw new LoggerArgumentException(sprintf('%s is not a valid logfile', $logfile));
}
$loglevel = static::mapLegacyConfigDebugLevel($config->get('system', 'loglevel'));
if (array_key_exists($loglevel, StreamLoggerClass::levelToInt)) {
$loglevel = StreamLoggerClass::levelToInt[$loglevel];
} else {
$loglevel = StreamLoggerClass::levelToInt[LogLevel::NOTICE];
}
$stream = $fileSystem->createStream($logfile);
return new StreamLoggerClass($channel ?? $this->channel, $this->introspection, $stream, $loglevel, getmypid());
}
/**
* 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
*
* @return LoggerInterface The PSR-3 compliant logger instance
*
* @throws LoggerException
*/
public function createDev(IManageConfigValues $config)
{
$debugging = $config->get('system', 'debugging');
$logfile = $config->get('system', 'dlogfile');
$developerIp = $config->get('system', 'dlogip');
if ((!isset($developerIp) || !$debugging) &&
(!is_file($logfile) || is_writable($logfile))) {
return new NullLogger();
}
return $this->create($config, $logfile, LogChannel::DEV);
}
}

View file

@ -0,0 +1,60 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Core\Logger\Factory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Logger\Exception\LoggerException;
use Friendica\Core\Logger\Type\SyslogLogger as SyslogLoggerClass;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* The logger factory for the SyslogLogger instance
*
* @see SyslogLoggerClass
*/
class SyslogLogger extends AbstractLoggerTypeFactory
{
/**
* Creates a new PSR-3 compliant syslog logger instance
*
* @param IManageConfigValues $config The system configuration
*
* @return LoggerInterface The PSR-3 compliant logger instance
*
* @throws LoggerException in case the logger cannot get created
*/
public function create(IManageConfigValues $config): LoggerInterface
{
$logOpts = $config->get('system', 'syslog_flags') ?? SyslogLoggerClass::DEFAULT_FLAGS;
$logFacility = $config->get('system', 'syslog_facility') ?? SyslogLoggerClass::DEFAULT_FACILITY;
$loglevel = SyslogLogger::mapLegacyConfigDebugLevel($config->get('system', 'loglevel'));
if (!array_key_exists($loglevel, SyslogLoggerClass::logLevels)) {
$loglevel = SyslogLoggerClass::logLevels[$loglevel];
} else {
$loglevel = SyslogLoggerClass::logLevels[LogLevel::NOTICE];
}
return new SyslogLoggerClass($this->channel, $this->introspection, $loglevel, $logOpts, $logFacility);
}
}