2019-02-27 10:40:35 -05:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Friendica\Util\Logger;
|
|
|
|
|
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
|
|
|
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
|
|
|
|
{
|
2019-02-27 10:46:53 -05:00
|
|
|
const IDENT = 'Friendica';
|
|
|
|
|
2019-02-27 10:40:35 -05:00
|
|
|
/**
|
|
|
|
* Translates LogLevel log levels to syslog log priorities.
|
|
|
|
*/
|
|
|
|
private $logLevels = [
|
2019-02-27 11:03:01 -05:00
|
|
|
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,
|
2019-02-27 10:40:35 -05:00
|
|
|
LogLevel::EMERGENCY => LOG_EMERG,
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
2019-02-27 11:03:01 -05:00
|
|
|
* The channel of the current process (added to each message)
|
2019-02-27 10:40:35 -05:00
|
|
|
* @var string
|
|
|
|
*/
|
2019-02-27 10:46:53 -05:00
|
|
|
private $channel;
|
2019-02-27 10:40:35 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 Introspector for the current call
|
|
|
|
* @var Introspection
|
|
|
|
*/
|
|
|
|
private $introspection;
|
|
|
|
|
|
|
|
/**
|
2019-02-27 10:46:53 -05:00
|
|
|
* @param string $channel The output channel
|
2019-02-27 11:03:01 -05:00
|
|
|
* @param string $level The minimum loglevel at which this logger will be triggered
|
2019-02-27 10:40:35 -05:00
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER)
|
|
|
|
{
|
2019-02-27 10:46:53 -05:00
|
|
|
$this->channel = $channel;
|
2019-02-27 10:40:35 -05:00
|
|
|
$this->logOpts = $logOpts;
|
|
|
|
$this->logFacility = $logFacility;
|
|
|
|
$this->logLevel = $this->mapLevelToPriority($level);
|
|
|
|
$this->introspection = $introspection;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2019-02-27 11:03:01 -05:00
|
|
|
* @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
|
2019-02-27 10:40:35 -05:00
|
|
|
*
|
|
|
|
* @throws InternalServerErrorException if syslog cannot be used
|
|
|
|
*/
|
|
|
|
private function write($priority, $message)
|
|
|
|
{
|
2019-02-27 10:46:53 -05:00
|
|
|
if (!openlog(self::IDENT, $this->logOpts, $this->logFacility)) {
|
|
|
|
throw new InternalServerErrorException('Can\'t open syslog for ident "' . $this->channel . '" and facility "' . $this->logFacility . '""');
|
2019-02-27 10:40:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
syslog($priority, $message);
|
|
|
|
}
|
|
|
|
|
2019-02-27 10:46:53 -05:00
|
|
|
/**
|
|
|
|
* Closes the Syslog
|
|
|
|
*/
|
2019-02-27 10:40:35 -05:00
|
|
|
public function close()
|
|
|
|
{
|
|
|
|
closelog();
|
|
|
|
}
|
|
|
|
|
2019-02-27 10:46:53 -05:00
|
|
|
/**
|
|
|
|
* Formats a log record for the syslog output
|
|
|
|
*
|
2019-02-27 11:03:01 -05:00
|
|
|
* @param int $level The loglevel/priority
|
2019-02-27 10:46:53 -05:00
|
|
|
* @param string $message The message
|
2019-02-27 11:03:01 -05:00
|
|
|
* @param array $context The context of this call
|
2019-02-27 10:46:53 -05:00
|
|
|
*
|
|
|
|
* @return string the formatted syslog output
|
|
|
|
*/
|
2019-02-27 10:40:35 -05:00
|
|
|
private function formatLog($level, $message, $context = [])
|
|
|
|
{
|
2019-02-27 11:03:01 -05:00
|
|
|
$logMessage = '';
|
2019-02-27 10:40:35 -05:00
|
|
|
|
2019-02-27 10:46:53 -05:00
|
|
|
$logMessage .= $this->channel . ' ';
|
2019-02-27 10:40:35 -05:00
|
|
|
$logMessage .= '[' . $level . ']: ';
|
2019-02-27 11:03:01 -05:00
|
|
|
$logMessage .= $this->psrInterpolate($message, $context) . ' ';
|
|
|
|
$logMessage .= @json_encode($context) . ' - ';
|
|
|
|
$logMessage .= @json_encode($this->introspection->getRecord());
|
2019-02-27 10:40:35 -05:00
|
|
|
|
|
|
|
return $logMessage;
|
|
|
|
}
|
|
|
|
|
2019-02-27 11:03:01 -05:00
|
|
|
/**
|
|
|
|
* Simple interpolation of PSR-3 compliant replacements ( 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
|
|
|
|
*
|
|
|
|
* @param int $level
|
|
|
|
* @param string $message
|
|
|
|
* @param array $context
|
|
|
|
*
|
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
|
|
|
*/
|
2019-02-27 10:40:35 -05:00
|
|
|
private function addEntry($level, $message, $context = [])
|
|
|
|
{
|
|
|
|
if ($level >= $this->logLevel) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$formattedLog = $this->formatLog($level, $message, $context);
|
|
|
|
$this->write($level, $formattedLog);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function emergency($message, array $context = array())
|
|
|
|
{
|
|
|
|
$this->addEntry(LOG_EMERG, $message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function alert($message, array $context = array())
|
|
|
|
{
|
|
|
|
$this->addEntry(LOG_ALERT, $message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function critical($message, array $context = array())
|
|
|
|
{
|
|
|
|
$this->addEntry(LOG_CRIT, $message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function error($message, array $context = array())
|
|
|
|
{
|
|
|
|
$this->addEntry(LOG_ERR, $message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function warning($message, array $context = array())
|
|
|
|
{
|
|
|
|
$this->addEntry(LOG_WARNING, $message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function notice($message, array $context = array())
|
|
|
|
{
|
|
|
|
$this->addEntry(LOG_NOTICE, $message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function info($message, array $context = array())
|
|
|
|
{
|
|
|
|
$this->addEntry(LOG_INFO, $message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function debug($message, array $context = array())
|
|
|
|
{
|
|
|
|
$this->addEntry(LOG_DEBUG, $message, $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
2019-02-27 11:03:01 -05:00
|
|
|
* @throws InternalServerErrorException if the syslog isn't available
|
2019-02-27 10:40:35 -05:00
|
|
|
*/
|
|
|
|
public function log($level, $message, array $context = array())
|
|
|
|
{
|
|
|
|
$logLevel = $this->mapLevelToPriority($level);
|
|
|
|
$this->addEntry($logLevel, $message, $context);
|
|
|
|
}
|
|
|
|
}
|