Restructure Logger to new paradigm
This commit is contained in:
parent
0fe545ab17
commit
184f6cc255
28 changed files with 219 additions and 169 deletions
13
src/Core/Logger/Exception/LoggerArgumentException.php
Normal file
13
src/Core/Logger/Exception/LoggerArgumentException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Logger\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class LoggerArgumentException extends \InvalidArgumentException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
13
src/Core/Logger/Exception/LoggerException.php
Normal file
13
src/Core/Logger/Exception/LoggerException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Logger\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class LoggerException extends \Exception
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
291
src/Core/Logger/Factory/Logger.php
Normal file
291
src/Core/Logger/Factory/Logger.php
Normal file
|
@ -0,0 +1,291 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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;
|
||||
use Friendica\Database\Database;
|
||||
use Friendica\Util\FileSystem;
|
||||
use Friendica\Util\Introspection;
|
||||
use Friendica\Core\Logger\Type\Monolog\DevelopHandler;
|
||||
use Friendica\Core\Logger\Type\Monolog\IntrospectionProcessor;
|
||||
use Friendica\Core\Logger\Type\ProfilerLogger;
|
||||
use Friendica\Core\Logger\Type\StreamLogger;
|
||||
use Friendica\Core\Logger\Type\SyslogLogger;
|
||||
use Friendica\Core\Logger\Type\VoidLogger;
|
||||
use Friendica\Util\Profiler;
|
||||
use Monolog;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* A logger factory
|
||||
*/
|
||||
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',
|
||||
'Friendica\\Core\\Logger\\Type\\Monolog',
|
||||
];
|
||||
|
||||
/** @var string The log-channel (app, worker, ...) */
|
||||
private $channel;
|
||||
|
||||
public function __construct(string $channel)
|
||||
{
|
||||
$this->channel = $channel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new PSR-3 compliant logger instances
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @return LoggerInterface The PSR-3 compliant logger instance
|
||||
*/
|
||||
public function create(Database $database, IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem): LoggerInterface
|
||||
{
|
||||
if (empty($config->get('system', 'debugging', false))) {
|
||||
$logger = new VoidLogger();
|
||||
$database->setLogger($logger);
|
||||
return $logger;
|
||||
}
|
||||
|
||||
$introspection = new Introspection(self::$ignoreClassList);
|
||||
$level = $config->get('system', 'loglevel');
|
||||
$loglevel = self::mapLegacyConfigDebugLevel((string)$level);
|
||||
|
||||
switch ($config->get('system', 'logger_config', 'stream')) {
|
||||
case 'monolog':
|
||||
$loggerTimeZone = new \DateTimeZone('UTC');
|
||||
Monolog\Logger::setTimezone($loggerTimeZone);
|
||||
|
||||
$logger = new Monolog\Logger($this->channel);
|
||||
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
|
||||
$logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
|
||||
|
||||
$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 {
|
||||
static::addStreamHandler($logger, $stream, $loglevel);
|
||||
} catch (\Throwable $e) {
|
||||
// No Logger ..
|
||||
/// @todo isn't it possible to give the admin any hint about this wrong configuration?
|
||||
$logger = new VoidLogger();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case 'syslog':
|
||||
try {
|
||||
$logger = new SyslogLogger($this->channel, $introspection, $loglevel);
|
||||
} catch (\Throwable $e) {
|
||||
// No logger ...
|
||||
/// @todo isn't it possible to give the admin any hint about this wrong configuration?
|
||||
$logger = new VoidLogger();
|
||||
}
|
||||
break;
|
||||
|
||||
case 'stream':
|
||||
default:
|
||||
$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 (\Throwable $t) {
|
||||
// No logger ...
|
||||
/// @todo isn't it possible to give the admin any hint about this wrong configuration?
|
||||
$logger = new VoidLogger();
|
||||
}
|
||||
} else {
|
||||
/// @todo isn't it possible to give the admin any hint about this wrong configuration?
|
||||
$logger = new VoidLogger();
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
||||
$database->setLogger($logger);
|
||||
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
|
||||
*
|
||||
* @param IManageConfigValues $config The config
|
||||
* @param Profiler $profiler The profiler of the app
|
||||
* @param FileSystem $fileSystem FileSystem utils
|
||||
*
|
||||
* @return LoggerInterface The PSR-3 compliant logger instance
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function createDev(IManageConfigValues $config, Profiler $profiler, FileSystem $fileSystem)
|
||||
{
|
||||
$debugging = $config->get('system', 'debugging');
|
||||
$stream = $config->get('system', 'dlogfile');
|
||||
$developerIp = $config->get('system', 'dlogip');
|
||||
|
||||
if ((!isset($developerIp) || !$debugging) &&
|
||||
(!is_file($stream) || is_writable($stream))) {
|
||||
return new VoidLogger();
|
||||
}
|
||||
|
||||
$loggerTimeZone = new \DateTimeZone('UTC');
|
||||
Monolog\Logger::setTimezone($loggerTimeZone);
|
||||
|
||||
$introspection = new Introspection(self::$ignoreClassList);
|
||||
|
||||
switch ($config->get('system', 'logger_config', 'stream')) {
|
||||
|
||||
case 'monolog':
|
||||
$loggerTimeZone = new \DateTimeZone('UTC');
|
||||
Monolog\Logger::setTimezone($loggerTimeZone);
|
||||
|
||||
$logger = new Monolog\Logger(self::DEV_CHANNEL);
|
||||
$logger->pushProcessor(new Monolog\Processor\PsrLogMessageProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\ProcessIdProcessor());
|
||||
$logger->pushProcessor(new Monolog\Processor\UidProcessor());
|
||||
$logger->pushProcessor(new IntrospectionProcessor($introspection, LogLevel::DEBUG));
|
||||
|
||||
$logger->pushHandler(new DevelopHandler($developerIp));
|
||||
|
||||
static::addStreamHandler($logger, $stream, LogLevel::DEBUG);
|
||||
break;
|
||||
|
||||
case 'syslog':
|
||||
$logger = new SyslogLogger(self::DEV_CHANNEL, $introspection, LogLevel::DEBUG);
|
||||
break;
|
||||
|
||||
case 'stream':
|
||||
default:
|
||||
$logger = new StreamLogger(self::DEV_CHANNEL, $stream, $introspection, $fileSystem, LogLevel::DEBUG);
|
||||
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);
|
||||
}
|
||||
|
||||
return $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adding a handler to a given logger instance
|
||||
*
|
||||
* @param LoggerInterface $logger The logger instance
|
||||
* @param mixed $stream The stream which handles the logger output
|
||||
* @param string $level The level, for which this handler at least should handle logging
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
public static function addStreamHandler(LoggerInterface $logger, $stream, string $level = LogLevel::NOTICE)
|
||||
{
|
||||
if ($logger instanceof Monolog\Logger) {
|
||||
$loglevel = Monolog\Logger::toMonologLevel($level);
|
||||
|
||||
// fallback to notice if an invalid loglevel is set
|
||||
if (!is_int($loglevel)) {
|
||||
$loglevel = LogLevel::NOTICE;
|
||||
}
|
||||
|
||||
try {
|
||||
$fileHandler = new Monolog\Handler\StreamHandler($stream, $loglevel);
|
||||
|
||||
$formatter = new Monolog\Formatter\LineFormatter("%datetime% %channel% [%level_name%]: %message% %context% %extra%\n");
|
||||
$fileHandler->setFormatter($formatter);
|
||||
|
||||
$logger->pushHandler($fileHandler);
|
||||
} catch (\Exception $exception) {
|
||||
throw new LoggerException('Cannot create Monolog Logger.', $exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
206
src/Core/Logger/Type/AbstractLogger.php
Normal file
206
src/Core/Logger/Type/AbstractLogger.php
Normal file
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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\Type;
|
||||
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Util\Introspection;
|
||||
use Friendica\Util\Strings;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* This class contains all necessary dependencies and calls for Friendica
|
||||
* Every new Logger should extend this class and define, how addEntry() works
|
||||
*
|
||||
* Additional information for each Logger, who extends this class:
|
||||
* - Introspection
|
||||
* - UID for each call
|
||||
* - Channel of the current call (i.e. index, worker, daemon, ...)
|
||||
*/
|
||||
abstract class AbstractLogger implements LoggerInterface
|
||||
{
|
||||
/**
|
||||
* The output channel of this logger
|
||||
* @var string
|
||||
*/
|
||||
protected $channel;
|
||||
|
||||
/**
|
||||
* The Introspection for the current call
|
||||
* @var Introspection
|
||||
*/
|
||||
protected $introspection;
|
||||
|
||||
/**
|
||||
* The UID of the current call
|
||||
* @var string
|
||||
*/
|
||||
protected $logUid;
|
||||
|
||||
/**
|
||||
* Adds a new entry to the log
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function addEntry($level, string $message, array $context = []);
|
||||
|
||||
/**
|
||||
* @param string $channel The output channel
|
||||
* @param Introspection $introspection The introspection of the current call
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
public function __construct(string $channel, Introspection $introspection)
|
||||
{
|
||||
$this->channel = $channel;
|
||||
$this->introspection = $introspection;
|
||||
|
||||
try {
|
||||
$this->logUid = Strings::getRandomHex(6);
|
||||
} catch (\Exception $exception) {
|
||||
throw new LoggerException('Cannot generate log Id', $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
|
||||
*
|
||||
* @see https://www.php-fig.org/psr/psr-3/#12-message
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return string the interpolated message
|
||||
*/
|
||||
protected function psrInterpolate(string $message, array $context = []): string
|
||||
{
|
||||
$replace = [];
|
||||
foreach ($context as $key => $value) {
|
||||
// check that the value can be casted to string
|
||||
if (!is_array($value) && (!is_object($value) || method_exists($value, '__toString'))) {
|
||||
$replace['{' . $key . '}'] = $value;
|
||||
} elseif (is_array($value)) {
|
||||
$replace['{' . $key . '}'] = @json_encode($value);
|
||||
}
|
||||
}
|
||||
|
||||
return strtr($message, $replace);
|
||||
}
|
||||
|
||||
/**
|
||||
* JSON Encodes a complete array including objects with "__toString()" methods
|
||||
*
|
||||
* @param array $input an Input Array to encode
|
||||
*
|
||||
* @return false|string The json encoded output of the array
|
||||
*/
|
||||
protected function jsonEncodeArray(array $input)
|
||||
{
|
||||
$output = [];
|
||||
|
||||
foreach ($input as $key => $value) {
|
||||
if (is_object($value) && method_exists($value, '__toString')) {
|
||||
$output[$key] = $value->__toString();
|
||||
} else {
|
||||
$output[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return @json_encode($output);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::EMERGENCY, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function alert($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::ALERT, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function critical($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::CRITICAL, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function error($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::ERROR, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function warning($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::WARNING, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function notice($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::NOTICE, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function info($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::INFO, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function debug($message, array $context = [])
|
||||
{
|
||||
$this->addEntry(LogLevel::DEBUG, (string) $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
$this->addEntry($level, (string) $message, $context);
|
||||
}
|
||||
}
|
68
src/Core/Logger/Type/Monolog/DevelopHandler.php
Normal file
68
src/Core/Logger/Type/Monolog/DevelopHandler.php
Normal file
|
@ -0,0 +1,68 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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\Type\Monolog;
|
||||
|
||||
use Monolog\Handler;
|
||||
use Monolog\Logger;
|
||||
|
||||
/**
|
||||
* Simple handler for Friendica developers to use for deeper logging
|
||||
*
|
||||
* If you want to debug only interactions from your IP or the IP of a remote server for federation debug,
|
||||
* you'll use Logger::develop() for the duration of your work, and you clean it up when you're done before submitting your PR.
|
||||
*/
|
||||
class DevelopHandler extends Handler\AbstractHandler
|
||||
{
|
||||
/**
|
||||
* @var string The IP of the developer who wants to debug
|
||||
*/
|
||||
private $developerIp;
|
||||
|
||||
/**
|
||||
* @param string $developerIp The IP of the developer who wants to debug
|
||||
* @param int $level The minimum logging level at which this handler will be triggered
|
||||
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not
|
||||
*/
|
||||
public function __construct($developerIp, $level = Logger::DEBUG, bool $bubble = true)
|
||||
{
|
||||
parent::__construct($level, $bubble);
|
||||
|
||||
$this->developerIp = $developerIp;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handle(array $record): bool
|
||||
{
|
||||
if (!$this->isHandling($record)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Just in case the remote IP is the same as the developer IP log the output
|
||||
if (!is_null($this->developerIp) && $_SERVER['REMOTE_ADDR'] != $this->developerIp) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false === $this->bubble;
|
||||
}
|
||||
}
|
62
src/Core/Logger/Type/Monolog/IntrospectionProcessor.php
Normal file
62
src/Core/Logger/Type/Monolog/IntrospectionProcessor.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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\Type\Monolog;
|
||||
|
||||
use Friendica\Util\Introspection;
|
||||
use Monolog\Logger;
|
||||
use Monolog\Processor\ProcessorInterface;
|
||||
|
||||
/**
|
||||
* Injects line/file//function where the log message came from
|
||||
*/
|
||||
class IntrospectionProcessor implements ProcessorInterface
|
||||
{
|
||||
private $level;
|
||||
|
||||
private $introspection;
|
||||
|
||||
/**
|
||||
* @param Introspection $introspection Holds the Introspection of the current call
|
||||
* @param string|int $level The minimum logging level at which this Processor will be triggered
|
||||
*/
|
||||
public function __construct(Introspection $introspection, $level = Logger::DEBUG)
|
||||
{
|
||||
$this->level = Logger::toMonologLevel($level);
|
||||
$introspection->addClasses(['Monolog\\']);
|
||||
$this->introspection = $introspection;
|
||||
}
|
||||
|
||||
public function __invoke(array $record): array
|
||||
{
|
||||
// return if the level is not high enough
|
||||
if ($record['level'] < $this->level) {
|
||||
return $record;
|
||||
}
|
||||
// we should have the call source now
|
||||
$record['extra'] = array_merge(
|
||||
$record['extra'],
|
||||
$this->introspection->getRecord()
|
||||
);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
145
src/Core/Logger/Type/ProfilerLogger.php
Normal file
145
src/Core/Logger/Type/ProfilerLogger.php
Normal file
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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\Type;
|
||||
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* This Logger adds additional profiling data in case profiling is enabled.
|
||||
* It uses a predefined logger.
|
||||
*/
|
||||
class ProfilerLogger implements LoggerInterface
|
||||
{
|
||||
/**
|
||||
* The Logger of the current call
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* The Profiler for the current call
|
||||
* @var Profiler
|
||||
*/
|
||||
protected $profiler;
|
||||
|
||||
/**
|
||||
* ProfilerLogger constructor.
|
||||
* @param LoggerInterface $logger The Logger of the current call
|
||||
* @param Profiler $profiler The profiler of the current call
|
||||
*/
|
||||
public function __construct(LoggerInterface $logger, Profiler $profiler)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->profiler = $profiler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->emergency($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function alert($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->alert($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function critical($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->critical($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function error($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->error($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function warning($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->warning($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function notice($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->notice($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function info($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->info($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function debug($message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->debug($message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
$this->profiler->startRecording('file');
|
||||
$this->logger->log($level, $message, $context);
|
||||
$this->profiler->stopRecording();
|
||||
}
|
||||
}
|
27
src/Core/Logger/Type/README.md
Normal file
27
src/Core/Logger/Type/README.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
## Friendica\Util\Logger
|
||||
|
||||
This namespace contains the different implementations of a Logger.
|
||||
|
||||
### Configuration guideline
|
||||
|
||||
The following settings are possible for `logger_config`:
|
||||
- `monolog`: A Logging framework with lots of additions (see [Monolog](https://github.com/Seldaek/monolog/)). There are just Friendica additions inside the Monolog directory
|
||||
- [`stream`](StreamLogger.php): A small logger for files or streams
|
||||
- [`syslog`](SyslogLogger.php): Prints the logging output into the syslog
|
||||
|
||||
[`VoidLogger`](VoidLogger.php) is a fallback logger without any function if no debugging is enabled.
|
||||
|
||||
[`ProfilerLogger`](ProfilerLogger.php) is a wrapper around an existing logger in case profiling is enabled for Friendica.
|
||||
Every log call will be saved to the `Profiler` with a timestamp.
|
||||
|
||||
### Implementation guideline
|
||||
|
||||
Each logging implementation should pe capable of printing at least the following information:
|
||||
- An unique ID for each Request/Call
|
||||
- The process ID (PID)
|
||||
- A timestamp of the logging entry
|
||||
- The critically of the log entry
|
||||
- A log message
|
||||
- A context of the log message (f.e which user)
|
||||
|
||||
If possible, a Logger should extend [`AbstractLogger`](AbstractLogger.php), because it contains additional, Friendica specific business logic for each logging call.
|
203
src/Core/Logger/Type/StreamLogger.php
Normal file
203
src/Core/Logger/Type/StreamLogger.php
Normal file
|
@ -0,0 +1,203 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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\Type;
|
||||
|
||||
use Friendica\Core\Logger\Exception\LoggerArgumentException;
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Util\DateTimeFormat;
|
||||
use Friendica\Util\FileSystem;
|
||||
use Friendica\Util\Introspection;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* A Logger instance for logging into a stream (file, stdout, stderr)
|
||||
*/
|
||||
class StreamLogger extends AbstractLogger
|
||||
{
|
||||
/**
|
||||
* The minimum loglevel at which this logger will be triggered
|
||||
* @var string
|
||||
*/
|
||||
private $logLevel;
|
||||
|
||||
/**
|
||||
* The file URL of the stream (if needed)
|
||||
* @var string
|
||||
*/
|
||||
private $url;
|
||||
|
||||
/**
|
||||
* The stream, where the current logger is writing into
|
||||
* @var resource
|
||||
*/
|
||||
private $stream;
|
||||
|
||||
/**
|
||||
* The current process ID
|
||||
* @var int
|
||||
*/
|
||||
private $pid;
|
||||
|
||||
/**
|
||||
* @var FileSystem
|
||||
*/
|
||||
private $fileSystem;
|
||||
|
||||
/**
|
||||
* Translates LogLevel log levels to integer values
|
||||
* @var array
|
||||
*/
|
||||
private $levelToInt = [
|
||||
LogLevel::EMERGENCY => 0,
|
||||
LogLevel::ALERT => 1,
|
||||
LogLevel::CRITICAL => 2,
|
||||
LogLevel::ERROR => 3,
|
||||
LogLevel::WARNING => 4,
|
||||
LogLevel::NOTICE => 5,
|
||||
LogLevel::INFO => 6,
|
||||
LogLevel::DEBUG => 7,
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @param string|resource $stream The stream to write with this logger (either a file or a stream, i.e. stdout)
|
||||
* @param string $level The minimum loglevel at which this logger will be triggered
|
||||
*
|
||||
* @throws LoggerArgumentException
|
||||
*/
|
||||
public function __construct($channel, $stream, Introspection $introspection, FileSystem $fileSystem, string $level = LogLevel::DEBUG)
|
||||
{
|
||||
$this->fileSystem = $fileSystem;
|
||||
|
||||
parent::__construct($channel, $introspection);
|
||||
|
||||
if (is_resource($stream)) {
|
||||
$this->stream = $stream;
|
||||
} elseif (is_string($stream)) {
|
||||
$this->url = $stream;
|
||||
} else {
|
||||
throw new LoggerArgumentException('A stream must either be a resource or a string.');
|
||||
}
|
||||
|
||||
$this->pid = getmypid();
|
||||
if (array_key_exists($level, $this->levelToInt)) {
|
||||
$this->logLevel = $this->levelToInt[$level];
|
||||
} else {
|
||||
throw new LoggerArgumentException(sprintf('The level "%s" is not valid.', $level));
|
||||
}
|
||||
|
||||
$this->checkStream();
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if ($this->url && is_resource($this->stream)) {
|
||||
fclose($this->stream);
|
||||
}
|
||||
|
||||
$this->stream = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry to the log
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws LoggerException
|
||||
* @throws LoggerArgumentException
|
||||
*/
|
||||
protected function addEntry($level, string $message, array $context = [])
|
||||
{
|
||||
if (!array_key_exists($level, $this->levelToInt)) {
|
||||
throw new LoggerArgumentException(sprintf('The level "%s" is not valid.', $level));
|
||||
}
|
||||
|
||||
$logLevel = $this->levelToInt[$level];
|
||||
|
||||
if ($logLevel > $this->logLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->checkStream();
|
||||
|
||||
$formattedLog = $this->formatLog($level, $message, $context);
|
||||
fwrite($this->stream, $formattedLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a log record for the syslog output
|
||||
*
|
||||
* @param mixed $level The loglevel/priority
|
||||
* @param string $message The message
|
||||
* @param array $context The context of this call
|
||||
*
|
||||
* @return string the formatted syslog output
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
private function formatLog($level, string $message, array $context = []): string
|
||||
{
|
||||
$record = $this->introspection->getRecord();
|
||||
$record = array_merge($record, ['uid' => $this->logUid, 'process_id' => $this->pid]);
|
||||
|
||||
try {
|
||||
$logMessage = DateTimeFormat::utcNow(DateTimeFormat::ATOM) . ' ';
|
||||
} catch (\Exception $exception) {
|
||||
throw new LoggerException('Cannot get current datetime.', $exception);
|
||||
}
|
||||
$logMessage .= $this->channel . ' ';
|
||||
$logMessage .= '[' . strtoupper($level) . ']: ';
|
||||
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
|
||||
$logMessage .= $this->jsonEncodeArray($context) . ' - ';
|
||||
$logMessage .= $this->jsonEncodeArray($record);
|
||||
$logMessage .= PHP_EOL;
|
||||
|
||||
return $logMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks the current stream
|
||||
*
|
||||
* @throws LoggerException
|
||||
* @throws LoggerArgumentException
|
||||
*/
|
||||
private function checkStream()
|
||||
{
|
||||
if (is_resource($this->stream)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (empty($this->url)) {
|
||||
throw new LoggerArgumentException('Missing stream URL.');
|
||||
}
|
||||
|
||||
try {
|
||||
$this->stream = $this->fileSystem->createStream($this->url);
|
||||
} catch (\UnexpectedValueException $exception) {
|
||||
throw new LoggerException('Cannot create stream.', $exception);
|
||||
}
|
||||
}
|
||||
}
|
230
src/Core/Logger/Type/SyslogLogger.php
Normal file
230
src/Core/Logger/Type/SyslogLogger.php
Normal file
|
@ -0,0 +1,230 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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\Type;
|
||||
|
||||
use Friendica\Core\Logger\Exception\LoggerArgumentException;
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Util\Introspection;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* A Logger instance for syslogging (fast, but simple)
|
||||
* @see http://php.net/manual/en/function.syslog.php
|
||||
*/
|
||||
class SyslogLogger extends AbstractLogger
|
||||
{
|
||||
const IDENT = 'Friendica';
|
||||
|
||||
/**
|
||||
* Translates LogLevel log levels to syslog log priorities.
|
||||
* @var array
|
||||
*/
|
||||
private $logLevels = [
|
||||
LogLevel::DEBUG => LOG_DEBUG,
|
||||
LogLevel::INFO => LOG_INFO,
|
||||
LogLevel::NOTICE => LOG_NOTICE,
|
||||
LogLevel::WARNING => LOG_WARNING,
|
||||
LogLevel::ERROR => LOG_ERR,
|
||||
LogLevel::CRITICAL => LOG_CRIT,
|
||||
LogLevel::ALERT => LOG_ALERT,
|
||||
LogLevel::EMERGENCY => LOG_EMERG,
|
||||
];
|
||||
|
||||
/**
|
||||
* Translates log priorities to string outputs
|
||||
* @var array
|
||||
*/
|
||||
private $logToString = [
|
||||
LOG_DEBUG => 'DEBUG',
|
||||
LOG_INFO => 'INFO',
|
||||
LOG_NOTICE => 'NOTICE',
|
||||
LOG_WARNING => 'WARNING',
|
||||
LOG_ERR => 'ERROR',
|
||||
LOG_CRIT => 'CRITICAL',
|
||||
LOG_ALERT => 'ALERT',
|
||||
LOG_EMERG => 'EMERGENCY'
|
||||
];
|
||||
|
||||
/**
|
||||
* Indicates what logging options will be used when generating a log message
|
||||
* @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $logOpts;
|
||||
|
||||
/**
|
||||
* Used to specify what type of program is logging the message
|
||||
* @see http://php.net/manual/en/function.openlog.php#refsect1-function.openlog-parameters
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
private $logFacility;
|
||||
|
||||
/**
|
||||
* The minimum loglevel at which this logger will be triggered
|
||||
* @var int
|
||||
*/
|
||||
private $logLevel;
|
||||
|
||||
/**
|
||||
* A error message of the current operation
|
||||
* @var string
|
||||
*/
|
||||
private $errorMessage;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @param string $level The minimum loglevel at which this logger will be triggered
|
||||
* @param int $logOpts Indicates what logging options will be used when generating a log message
|
||||
* @param int $logFacility Used to specify what type of program is logging the message
|
||||
*
|
||||
* @throws LoggerArgumentException
|
||||
*/
|
||||
public function __construct($channel, Introspection $introspection, string $level = LogLevel::NOTICE, int $logOpts = LOG_PID, int $logFacility = LOG_USER)
|
||||
{
|
||||
parent::__construct($channel, $introspection);
|
||||
$this->logOpts = $logOpts;
|
||||
$this->logFacility = $logFacility;
|
||||
$this->logLevel = $this->mapLevelToPriority($level);
|
||||
$this->introspection->addClasses([self::class]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry to the syslog
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @throws LoggerArgumentException in case the level isn't valid
|
||||
* @throws LoggerException In case the syslog cannot be opened for writing
|
||||
*/
|
||||
protected function addEntry($level, string $message, array $context = [])
|
||||
{
|
||||
$logLevel = $this->mapLevelToPriority($level);
|
||||
|
||||
if ($logLevel > $this->logLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
$formattedLog = $this->formatLog($logLevel, $message, $context);
|
||||
$this->write($logLevel, $formattedLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* Maps the LogLevel (@see LogLevel) to a SysLog priority (@see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters)
|
||||
*
|
||||
* @param string $level A LogLevel
|
||||
*
|
||||
* @return int The SysLog priority
|
||||
*
|
||||
* @throws LoggerArgumentException If the loglevel isn't valid
|
||||
*/
|
||||
public function mapLevelToPriority(string $level): int
|
||||
{
|
||||
if (!array_key_exists($level, $this->logLevels)) {
|
||||
throw new LoggerArgumentException(sprintf('The level "%s" is not valid.', $level));
|
||||
}
|
||||
|
||||
return $this->logLevels[$level];
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the Syslog
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
closelog();
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a message to the syslog
|
||||
*
|
||||
* @see http://php.net/manual/en/function.syslog.php#refsect1-function.syslog-parameters
|
||||
*
|
||||
* @param int $priority The Priority
|
||||
* @param string $message The message of the log
|
||||
*
|
||||
* @throws LoggerException In case the syslog cannot be opened/written
|
||||
*/
|
||||
private function write(int $priority, string $message)
|
||||
{
|
||||
set_error_handler([$this, 'customErrorHandler']);
|
||||
$opened = openlog(self::IDENT, $this->logOpts, $this->logFacility);
|
||||
restore_error_handler();
|
||||
|
||||
if (!$opened) {
|
||||
throw new LoggerException(sprintf('Can\'t open syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
|
||||
}
|
||||
|
||||
$this->syslogWrapper($priority, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a log record for the syslog output
|
||||
*
|
||||
* @param int $level The loglevel/priority
|
||||
* @param string $message The message
|
||||
* @param array $context The context of this call
|
||||
*
|
||||
* @return string the formatted syslog output
|
||||
*/
|
||||
private function formatLog(int $level, string $message, array $context = []): string
|
||||
{
|
||||
$record = $this->introspection->getRecord();
|
||||
$record = array_merge($record, ['uid' => $this->logUid]);
|
||||
|
||||
$logMessage = $this->channel . ' ';
|
||||
$logMessage .= '[' . $this->logToString[$level] . ']: ';
|
||||
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
|
||||
$logMessage .= $this->jsonEncodeArray($context) . ' - ';
|
||||
$logMessage .= $this->jsonEncodeArray($record);
|
||||
|
||||
return $logMessage;
|
||||
}
|
||||
|
||||
private function customErrorHandler($code, $msg)
|
||||
{
|
||||
$this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* A syslog wrapper to make syslog functionality testable
|
||||
*
|
||||
* @param int $level The syslog priority
|
||||
* @param string $entry The message to send to the syslog function
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
protected function syslogWrapper(int $level, string $entry)
|
||||
{
|
||||
set_error_handler([$this, 'customErrorHandler']);
|
||||
$written = syslog($level, $entry);
|
||||
restore_error_handler();
|
||||
|
||||
if (!$written) {
|
||||
throw new LoggerException(sprintf('Can\'t write into syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility));
|
||||
}
|
||||
}
|
||||
}
|
159
src/Core/Logger/Type/VoidLogger.php
Normal file
159
src/Core/Logger/Type/VoidLogger.php
Normal file
|
@ -0,0 +1,159 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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\Type;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* A Logger instance to not log
|
||||
*/
|
||||
class VoidLogger implements LoggerInterface
|
||||
{
|
||||
/**
|
||||
* System is unusable.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function emergency($message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Action must be taken immediately.
|
||||
*
|
||||
* Example: Entire website down, database unavailable, etc. This should
|
||||
* trigger the SMS alerts and wake you up.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function alert($message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Critical conditions.
|
||||
*
|
||||
* Example: Application component unavailable, unexpected exception.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function critical($message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Runtime errors that do not require immediate action but should typically
|
||||
* be logged and monitored.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function error($message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Exceptional occurrences that are not errors.
|
||||
*
|
||||
* Example: Use of deprecated APIs, poor use of an API, undesirable things
|
||||
* that are not necessarily wrong.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function warning($message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normal but significant events.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function notice($message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interesting events.
|
||||
*
|
||||
* Example: User logs in, SQL logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function info($message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detailed debug information.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function debug($message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs with an arbitrary level.
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function log($level, $message, array $context = array())
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
235
src/Core/Logger/Type/WorkerLogger.php
Normal file
235
src/Core/Logger/Type/WorkerLogger.php
Normal file
|
@ -0,0 +1,235 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (C) 2010-2021, 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\Type;
|
||||
|
||||
use Friendica\Core\Logger\Exception\LoggerException;
|
||||
use Friendica\Util\Strings;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* A Logger for specific worker tasks, which adds a worker id to it.
|
||||
* Uses the decorator pattern (https://en.wikipedia.org/wiki/Decorator_pattern)
|
||||
*/
|
||||
class WorkerLogger implements LoggerInterface
|
||||
{
|
||||
/**
|
||||
* @var LoggerInterface The original Logger instance
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @var string the current worker ID
|
||||
*/
|
||||
private $workerId;
|
||||
|
||||
/**
|
||||
* @var string The called function name
|
||||
*/
|
||||
private $functionName;
|
||||
|
||||
/**
|
||||
* @param LoggerInterface $logger The logger for worker entries
|
||||
* @param string $functionName The current function name of the worker
|
||||
* @param int $idLength The length of the generated worker ID
|
||||
*
|
||||
* @throws LoggerException
|
||||
*/
|
||||
public function __construct(LoggerInterface $logger, string $functionName = '', int $idLength = 7)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->functionName = $functionName;
|
||||
try {
|
||||
$this->workerId = Strings::getRandomHex($idLength);
|
||||
} catch (\Exception $exception) {
|
||||
throw new LoggerException('Cannot generate random Hex.', $exception);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the function name for additional logging
|
||||
*
|
||||
* @param string $functionName
|
||||
*/
|
||||
public function setFunctionName(string $functionName)
|
||||
{
|
||||
$this->functionName = $functionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the worker context for each log entry
|
||||
*
|
||||
* @param array $context
|
||||
*/
|
||||
private function addContext(array &$context)
|
||||
{
|
||||
$context['worker_id'] = $this->workerId;
|
||||
$context['worker_cmd'] = $this->functionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the worker ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getWorkerId(): string
|
||||
{
|
||||
return $this->workerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* System is unusable.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function emergency($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->emergency($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Action must be taken immediately.
|
||||
*
|
||||
* Example: Entire website down, database unavailable, etc. This should
|
||||
* trigger the SMS alerts and wake you up.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function alert($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->alert($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Critical conditions.
|
||||
*
|
||||
* Example: Application component unavailable, unexpected exception.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function critical($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->critical($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runtime errors that do not require immediate action but should typically
|
||||
* be logged and monitored.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function error($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->error($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exceptional occurrences that are not errors.
|
||||
*
|
||||
* Example: Use of deprecated APIs, poor use of an API, undesirable things
|
||||
* that are not necessarily wrong.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function warning($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->warning($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Normal but significant events.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function notice($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->notice($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Interesting events.
|
||||
*
|
||||
* Example: User logs in, SQL logs.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function info($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->info($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detailed debug information.
|
||||
*
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function debug($message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->debug($message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs with an arbitrary level.
|
||||
*
|
||||
* @param mixed $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function log($level, $message, array $context = [])
|
||||
{
|
||||
$this->addContext($context);
|
||||
$this->logger->log($level, $message, $context);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue