Adapt Logger\Introspection

- Create an interface
- Add it as constructor parameter
This commit is contained in:
Philipp 2022-12-25 16:48:09 +01:00
parent bfc1c157f1
commit 10864e50c7
No known key found for this signature in database
GPG Key ID: 24A7501396EB5432
4 changed files with 68 additions and 22 deletions

View File

@ -0,0 +1,52 @@
<?php
/**
* @copyright Copyright (C) 2010-2022, 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\Capabilities;
use Friendica\Core\Logger\Factory\Logger;
use Friendica\Util\Profiler;
interface IHaveCallIntrospections
{
/**
* A list of classes, which shouldn't get logged
*
* @var string[]
*/
public const IGNORE_CLASS_LIST = [
Logger::class,
Profiler::class,
'Friendica\\Core\\Logger\\Type',
];
/**
* Adds new classes to get skipped
*
* @param array $classNames
*/
public function addClasses(array $classNames): void;
/**
* Returns the introspection record of the current call
*
* @return array
*/
public function getRecord(): array;
}

View File

@ -23,11 +23,11 @@ namespace Friendica\Core\Logger\Factory;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core;
use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
use Friendica\Core\Logger\Exception\LogLevelException;
use Friendica\Database\Database;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\FileSystem;
use Friendica\Core\Logger\Util\Introspection;
use Friendica\Core\Logger\Type\ProfilerLogger;
use Friendica\Core\Logger\Type\StreamLogger;
use Friendica\Core\Logger\Type\SyslogLogger;
@ -43,17 +43,6 @@ class Logger
{
const DEV_CHANNEL = 'dev';
/**
* A list of classes, which shouldn't get logged
*
* @var string[]
*/
private static $ignoreClassList = [
Core\Logger::class,
Profiler::class,
'Friendica\\Core\\Logger\\Type',
];
/** @var string The log-channel (app, worker, ...) */
private $channel;
@ -79,7 +68,7 @@ class Logger
*
* @return LoggerInterface The PSR-3 compliant logger instance
*/
public function create(Database $database, IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, ?string $minLevel = null): LoggerInterface
public function create(Database $database, IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, IHaveCallIntrospections $introspection, ?string $minLevel = null): LoggerInterface
{
if (empty($config->get('system', 'debugging', false))) {
$logger = new NullLogger();
@ -87,7 +76,6 @@ class Logger
return $logger;
}
$introspection = new Introspection(self::$ignoreClassList);
$minLevel = $minLevel ?? $config->get('system', 'loglevel');
$loglevel = self::mapLegacyConfigDebugLevel((string)$minLevel);
@ -99,7 +87,7 @@ class Logger
$logger = new SyslogLogger($this->channel, $introspection, $loglevel, $config->get('system', 'syslog_flags', SyslogLogger::DEFAULT_FLAGS), $config->get('system', 'syslog_facility', SyslogLogger::DEFAULT_FACILITY));
} catch (LogLevelException $exception) {
// If there's a wrong config value for loglevel, try again with standard
$logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
$logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
} catch (\Throwable $e) {
// No logger ...
@ -134,7 +122,7 @@ class Logger
$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
$logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
$logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
} catch (\Throwable $t) {
// No logger ...
@ -145,7 +133,7 @@ class Logger
$logger = new SyslogLogger($this->channel, $introspection, $loglevel);
} catch (LogLevelException $exception) {
// If there's a wrong config value for loglevel, try again with standard
$logger = $this->create($database, $config, $profiler, $fileSystem, LogLevel::NOTICE);
$logger = $this->create($database, $config, $profiler, $fileSystem, $introspection, LogLevel::NOTICE);
$logger->warning('Invalid loglevel set in config.', ['loglevel' => $loglevel]);
} catch (\Throwable $e) {
// No logger ...
@ -182,7 +170,7 @@ class Logger
* @return LoggerInterface The PSR-3 compliant logger instance
* @throws \Exception
*/
public static function createDev(IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem)
public static function createDev(IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem, IHaveCallIntrospections $introspection)
{
$debugging = $config->get('system', 'debugging');
$stream = $config->get('system', 'dlogfile');
@ -193,8 +181,6 @@ class Logger
return new NullLogger();
}
$introspection = new Introspection(self::$ignoreClassList);
$name = $config->get('system', 'logger_config', 'stream');
switch ($name) {

View File

@ -21,10 +21,12 @@
namespace Friendica\Core\Logger\Util;
use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
/**
* Get Introspection information about the current call
*/
class Introspection
class Introspection implements IHaveCallIntrospections
{
/** @var int */
private $skipStackFramesCount;
@ -52,7 +54,7 @@ class Introspection
*
* @param array $classNames
*/
public function addClasses(array $classNames)
public function addClasses(array $classNames): void
{
$this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
}

View File

@ -176,6 +176,12 @@ return [
['createDev', [], Dice::CHAIN_CALL],
]
],
\Friendica\Core\Logger\Capabilities\IHaveCallIntrospections::class => [
'instanceOf' => \Friendica\Core\Logger\Util\Introspection::class,
'constructParams' => [
\Friendica\Core\Logger\Util\Introspection::IGNORE_CLASS_LIST,
],
],
Cache\Capability\ICanCache::class => [
'instanceOf' => Cache\Factory\Cache::class,
'call' => [