1
1
Fork 0

Adding UID and Level To String mapping

This commit is contained in:
Philipp Holzer 2019-02-27 17:29:32 +01:00 committed by Hypolite Petovan
parent ae40963507
commit d712d36814
2 changed files with 38 additions and 12 deletions

View file

@ -69,8 +69,8 @@ class Introspection implements ProcessorInterface
$i += $this->skipStackFramesCount;
return [
'file' => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
'file' => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
];
}

View file

@ -3,6 +3,7 @@
namespace Friendica\Util\Logger;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\Strings;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
@ -17,18 +18,34 @@ class SyslogLogger implements LoggerInterface
/**
* 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::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
@ -63,6 +80,12 @@ class SyslogLogger implements LoggerInterface
*/
private $introspection;
/**
* The UID of the current call
* @var string
*/
private $logUid;
/**
* @param string $channel The output channel
* @param string $level The minimum loglevel at which this logger will be triggered
@ -71,6 +94,7 @@ class SyslogLogger implements LoggerInterface
*/
public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
{
$this->logUid = Strings::getRandomHex(6);
$this->channel = $channel;
$this->logOpts = $logOpts;
$this->logFacility = $logFacility;
@ -133,19 +157,21 @@ class SyslogLogger implements LoggerInterface
*/
private function formatLog($level, $message, $context = [])
{
$record = $this->introspection->getRecord();
$record = array_merge($record, ['uid' => $this->logUid]);
$logMessage = '';
$logMessage .= $this->channel . ' ';
$logMessage .= '[' . $level . ']: ';
$logMessage .= '[' . $this->logToString[$level] . ']: ';
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
$logMessage .= @json_encode($context) . ' - ';
$logMessage .= @json_encode($this->introspection->getRecord());
$logMessage .= @json_encode($record);
return $logMessage;
}
/**
* Simple interpolation of PSR-3 compliant replacements ( between '{' and '}' )
* Simple interpolation of PSR-3 compliant replacements ( variables between '{' and '}' )
* @see https://www.php-fig.org/psr/psr-3/#12-message
*
* @param string $message