mirror of
https://github.com/friendica/friendica
synced 2024-12-22 23:20:16 +00:00
Moving Profiling to class
This commit is contained in:
parent
4810ca570f
commit
25b6db6aca
9 changed files with 214 additions and 481 deletions
|
@ -4,14 +4,13 @@
|
|||
*/
|
||||
namespace Friendica\Core;
|
||||
|
||||
use Friendica\BaseObject;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* @brief Logger functions
|
||||
*/
|
||||
class Logger extends BaseObject
|
||||
class Logger
|
||||
{
|
||||
/**
|
||||
* @see Logger::error()
|
||||
|
@ -96,13 +95,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function emergency($message, $context = [])
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->emergency($message, $context);
|
||||
self::getApp()->GetProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -120,13 +113,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function alert($message, $context = [])
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->alert($message, $context);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -143,13 +130,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function critical($message, $context = [])
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->critical($message, $context);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -165,14 +146,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function error($message, $context = [])
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
echo "not set!?\n";
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->error($message, $context);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -190,13 +164,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function warning($message, $context = [])
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->warning($message, $context);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -211,13 +179,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function notice($message, $context = [])
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->notice($message, $context);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -234,13 +196,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function info($message, $context = [])
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->info($message, $context);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -255,13 +211,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function debug($message, $context = [])
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->debug($message, $context);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -275,13 +225,7 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function log($msg, $level = LogLevel::INFO)
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$logger->log($level, $msg);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -296,12 +240,10 @@ class Logger extends BaseObject
|
|||
*/
|
||||
public static function devLog($msg, $level = LogLevel::DEBUG)
|
||||
{
|
||||
if (!isset(self::$logger)) {
|
||||
if (!isset(self::$devLogger)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$stamp1 = microtime(true);
|
||||
self::$devLogger->log($level, $msg);
|
||||
self::getApp()->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ class DependencyFactory
|
|||
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||
// needed to call PConfig::init()
|
||||
Factory\ConfigFactory::createPConfig($configCache);
|
||||
$logger = Factory\LoggerFactory::create($channel, $config);
|
||||
$logger = Factory\LoggerFactory::create($channel, $config, $profiler);
|
||||
Factory\LoggerFactory::createDev($channel, $config);
|
||||
|
||||
return new App($basePath, $config, $logger, $profiler, $isBackend);
|
||||
|
|
|
@ -38,13 +38,14 @@ class LoggerFactory
|
|||
*
|
||||
* @param string $channel The channel of the logger instance
|
||||
* @param Configuration $config The config
|
||||
* @param Profiler $profiler The profiler of the app
|
||||
*
|
||||
* @return LoggerInterface The PSR-3 compliant logger instance
|
||||
*
|
||||
* @throws \Exception
|
||||
* @throws InternalServerErrorException
|
||||
*/
|
||||
public static function create($channel, Configuration $config)
|
||||
public static function create($channel, Configuration $config, Profiler $profiler)
|
||||
{
|
||||
if (empty($config->get('system', 'debugging', false))) {
|
||||
$logger = new VoidLogger();
|
||||
|
@ -58,7 +59,7 @@ class LoggerFactory
|
|||
case 'syslog':
|
||||
$level = $config->get('system', 'loglevel');
|
||||
|
||||
$logger = new SyslogLogger($channel, $introspection, $level);
|
||||
$logger = new SyslogLogger($channel, $introspection, $profiler, $level);
|
||||
break;
|
||||
case 'monolog':
|
||||
default:
|
||||
|
|
185
src/Util/Logger/AbstractFriendicaLogger.php
Normal file
185
src/Util/Logger/AbstractFriendicaLogger.php
Normal file
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Util\Logger;
|
||||
|
||||
use Friendica\Core\System;
|
||||
use Friendica\Util\Introspection;
|
||||
use Friendica\Util\Profiler;
|
||||
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
|
||||
*
|
||||
* Contains:
|
||||
* - Introspection
|
||||
* - Automatic Friendica profiling
|
||||
*/
|
||||
abstract class AbstractFriendicaLogger implements LoggerInterface
|
||||
{
|
||||
/**
|
||||
* The output channel of this logger
|
||||
* @var string
|
||||
*/
|
||||
protected $channel;
|
||||
|
||||
/**
|
||||
* The Introspection for the current call
|
||||
* @var Introspection
|
||||
*/
|
||||
protected $introspection;
|
||||
|
||||
/**
|
||||
* The Profiler for the current call
|
||||
* @var Profiler
|
||||
*/
|
||||
protected $profiler;
|
||||
|
||||
/**
|
||||
* The UID of the current call
|
||||
* @var string
|
||||
*/
|
||||
protected $logUid;
|
||||
|
||||
/**
|
||||
* Adds a new entry to the log
|
||||
*
|
||||
* @param int $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function addEntry($level, $message, $context = []);
|
||||
|
||||
/**
|
||||
* @param string $channel The output channel
|
||||
* @param Introspection $introspection The introspection of the current call
|
||||
* @param Profiler $profiler The profiler of the current call
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct($channel, Introspection $introspection, Profiler $profiler)
|
||||
{
|
||||
$this->channel = $channel;
|
||||
$this->introspection = $introspection;
|
||||
$this->profiler = $profiler;
|
||||
$this->logUid = Strings::getRandomHex(6);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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($message, array $context = array())
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function emergency($message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry(LogLevel::EMERGENCY, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function alert($message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry(LogLevel::ALERT, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function critical($message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry(LogLevel::CRITICAL, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function error($message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry(LogLevel::ERROR, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function warning($message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry(LogLevel::WARNING, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function notice($message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry(LogLevel::NOTICE, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function info($message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry(LogLevel::INFO, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function debug($message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry(LogLevel::DEBUG, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function log($level, $message, array $context = array())
|
||||
{
|
||||
$stamp1 = microtime(true);
|
||||
$this->addEntry($level, $message, $context);
|
||||
$this->profiler->saveTimestamp($stamp1, 'file', System::callstack());
|
||||
}
|
||||
}
|
|
@ -2,300 +2,30 @@
|
|||
|
||||
namespace Friendica\Util\Logger;
|
||||
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Util\Introspection;
|
||||
use Friendica\Util\Strings;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use Friendica\Util\Profiler;
|
||||
|
||||
/**
|
||||
* A Logger instance for logging into a stream
|
||||
* @see http://php.net/manual/en/function.syslog.php
|
||||
*/
|
||||
class StreamLogger implements LoggerInterface
|
||||
class StreamLogger extends AbstractFriendicaLogger
|
||||
{
|
||||
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'
|
||||
];
|
||||
|
||||
/**
|
||||
* The channel of the current process (added to each message)
|
||||
* @var string
|
||||
*/
|
||||
private $channel;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* The Introspection for the current call
|
||||
* @var Introspection
|
||||
*/
|
||||
private $introspection;
|
||||
|
||||
/**
|
||||
* The UID of the current call
|
||||
* @var string
|
||||
*/
|
||||
private $logUid;
|
||||
|
||||
/**
|
||||
* @param string $channel The output channel
|
||||
* @param Introspection $introspection The introspection of the current call
|
||||
* @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 \Exception
|
||||
*/
|
||||
public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
|
||||
public function __construct($channel, Introspection $introspection, Profiler $profiler)
|
||||
{
|
||||
$this->logUid = Strings::getRandomHex(6);
|
||||
$this->channel = $channel;
|
||||
$this->logOpts = $logOpts;
|
||||
$this->logFacility = $logFacility;
|
||||
$this->logLevel = $this->mapLevelToPriority($level);
|
||||
$this->introspection = $introspection;
|
||||
parent::__construct($channel, $introspection, $profiler);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 \Psr\Log\InvalidArgumentException If the loglevel isn't valid
|
||||
*/
|
||||
public function mapLevelToPriority($level)
|
||||
{
|
||||
if (!array_key_exists($level, $this->logLevels)) {
|
||||
throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.');
|
||||
}
|
||||
|
||||
return $this->logLevels[$level];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 InternalServerErrorException if syslog cannot be used
|
||||
*/
|
||||
private function write($priority, $message)
|
||||
{
|
||||
if (!openlog(self::IDENT, $this->logOpts, $this->logFacility)) {
|
||||
throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->channel . '" and facility "' . $this->logFacility . '""');
|
||||
}
|
||||
|
||||
syslog($priority, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the Syslog
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
closelog();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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($level, $message, $context = [])
|
||||
{
|
||||
$record = $this->introspection->getRecord();
|
||||
$record = array_merge($record, ['uid' => $this->logUid]);
|
||||
$logMessage = '';
|
||||
|
||||
$logMessage .= $this->channel . ' ';
|
||||
$logMessage .= '[' . $this->logToString[$level] . ']: ';
|
||||
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
|
||||
$logMessage .= @json_encode($context) . ' - ';
|
||||
$logMessage .= @json_encode($record);
|
||||
|
||||
return $logMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
|
||||
* @see https://www.php-fig.org/psr/psr-3/#12-message
|
||||
* Adds a new entry to the log
|
||||
*
|
||||
* @param int $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
* @param array $context
|
||||
*
|
||||
* @return string the interpolated message
|
||||
* @return void
|
||||
*/
|
||||
private function psrInterpolate($message, array $context = array())
|
||||
protected function addEntry($level, $message, $context = [])
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry to the syslog
|
||||
*
|
||||
* @param int $level
|
||||
* @param string $message
|
||||
* @param array $context
|
||||
*
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
private function addEntry($level, $message, $context = [])
|
||||
{
|
||||
if ($level >= $this->logLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
$formattedLog = $this->formatLog($level, $message, $context);
|
||||
$this->write($level, $formattedLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function emergency($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_EMERG, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function alert($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_ALERT, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function critical($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_CRIT, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function error($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_ERR, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function warning($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_WARNING, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function notice($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_NOTICE, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function info($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_INFO, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function debug($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_DEBUG, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function log($level, $message, array $context = array())
|
||||
{
|
||||
$logLevel = $this->mapLevelToPriority($level);
|
||||
$this->addEntry($logLevel, $message, $context);
|
||||
// TODO: Implement addEntry() method.
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,16 +4,15 @@ namespace Friendica\Util\Logger;
|
|||
|
||||
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||
use Friendica\Util\Introspection;
|
||||
use Friendica\Util\Strings;
|
||||
use Friendica\Util\Profiler;
|
||||
use Psr\Log\InvalidArgumentException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
|
||||
/**
|
||||
* A Logger instance for syslogging (fast, but simple)
|
||||
* @see http://php.net/manual/en/function.syslog.php
|
||||
*/
|
||||
class SyslogLogger implements LoggerInterface
|
||||
class SyslogLogger extends AbstractFriendicaLogger
|
||||
{
|
||||
const IDENT = 'Friendica';
|
||||
|
||||
|
@ -47,12 +46,6 @@ class SyslogLogger implements LoggerInterface
|
|||
LOG_EMERG => 'EMERGENCY'
|
||||
];
|
||||
|
||||
/**
|
||||
* The channel of the current process (added to each message)
|
||||
* @var string
|
||||
*/
|
||||
private $channel;
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
@ -76,34 +69,20 @@ class SyslogLogger implements LoggerInterface
|
|||
private $logLevel;
|
||||
|
||||
/**
|
||||
* The Introspection for the current call
|
||||
* @var Introspection
|
||||
*/
|
||||
private $introspection;
|
||||
|
||||
/**
|
||||
* The UID of the current call
|
||||
* @var string
|
||||
*/
|
||||
private $logUid;
|
||||
|
||||
/**
|
||||
* @param string $channel The output channel
|
||||
* @param Introspection $introspection The introspection of the current call
|
||||
* {@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 \Exception
|
||||
*/
|
||||
public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
|
||||
public function __construct($channel, Introspection $introspection, Profiler $profiler, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
|
||||
{
|
||||
$this->logUid = Strings::getRandomHex(6);
|
||||
$this->channel = $channel;
|
||||
parent::__construct($channel, $introspection, $profiler);
|
||||
$this->logOpts = $logOpts;
|
||||
$this->logFacility = $logFacility;
|
||||
$this->logLevel = $this->mapLevelToPriority($level);
|
||||
$this->introspection = $introspection;
|
||||
$this->introspection->addClasses(array(self::class));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -174,30 +153,6 @@ class SyslogLogger implements LoggerInterface
|
|||
return $logMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
private function psrInterpolate($message, array $context = array())
|
||||
{
|
||||
$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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry to the syslog
|
||||
*
|
||||
|
@ -207,95 +162,15 @@ class SyslogLogger implements LoggerInterface
|
|||
*
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
private function addEntry($level, $message, $context = [])
|
||||
protected function addEntry($level, $message, $context = [])
|
||||
{
|
||||
if ($level >= $this->logLevel) {
|
||||
$logLevel = $this->mapLevelToPriority($level);
|
||||
|
||||
if ($logLevel >= $this->logLevel) {
|
||||
return;
|
||||
}
|
||||
|
||||
$formattedLog = $this->formatLog($level, $message, $context);
|
||||
$this->write($level, $formattedLog);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function emergency($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_EMERG, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function alert($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_ALERT, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function critical($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_CRIT, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function error($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_ERR, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function warning($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_WARNING, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function notice($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_NOTICE, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function info($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_INFO, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function debug($message, array $context = array())
|
||||
{
|
||||
$this->addEntry(LOG_DEBUG, $message, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* @throws InternalServerErrorException if the syslog isn't available
|
||||
*/
|
||||
public function log($level, $message, array $context = array())
|
||||
{
|
||||
$logLevel = $this->mapLevelToPriority($level);
|
||||
$this->addEntry($logLevel, $message, $context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ class ApiTest extends DatabaseTest
|
|||
Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);
|
||||
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||
Factory\ConfigFactory::createPConfig($configCache);
|
||||
$logger = Factory\LoggerFactory::create('test', $config);
|
||||
$logger = Factory\LoggerFactory::create('test', $config, $profiler);
|
||||
$this->app = new App($basePath, $config, $logger, $profiler, false);
|
||||
|
||||
parent::setUp();
|
||||
|
|
|
@ -20,7 +20,7 @@ class DBATest extends DatabaseTest
|
|||
Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);
|
||||
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||
Factory\ConfigFactory::createPConfig($configCache);
|
||||
$logger = Factory\LoggerFactory::create('test', $config);
|
||||
$logger = Factory\LoggerFactory::create('test', $config, $profiler);
|
||||
$this->app = new App($basePath, $config, $logger, $profiler, false);
|
||||
|
||||
parent::setUp();
|
||||
|
|
|
@ -20,7 +20,7 @@ class DBStructureTest extends DatabaseTest
|
|||
Factory\DBFactory::init($basePath, $configCache, $profiler, $_SERVER);
|
||||
$config = Factory\ConfigFactory::createConfig($configCache);
|
||||
Factory\ConfigFactory::createPConfig($configCache);
|
||||
$logger = Factory\LoggerFactory::create('test', $config);
|
||||
$logger = Factory\LoggerFactory::create('test', $config, $profiler);
|
||||
$this->app = new App($basePath, $config, $logger, $profiler, false);
|
||||
|
||||
parent::setUp();
|
||||
|
|
Loading…
Reference in a new issue