X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FLogger%2FSyslogLogger.php;h=83c3fc3ce57e8707ebc4891fb7e564fba199eb65;hb=3940e804e3ee4ac921e109f62a73fac2becaa611;hp=25d8f2f5e2e7e112f1af9919f9d6d8571823904d;hpb=25b6db6aca0180544f87fc251bdee5cb8266aff2;p=friendica.git diff --git a/src/Util/Logger/SyslogLogger.php b/src/Util/Logger/SyslogLogger.php index 25d8f2f5e2..83c3fc3ce5 100644 --- a/src/Util/Logger/SyslogLogger.php +++ b/src/Util/Logger/SyslogLogger.php @@ -4,15 +4,13 @@ namespace Friendica\Util\Logger; use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Util\Introspection; -use Friendica\Util\Profiler; -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 AbstractFriendicaLogger +class SyslogLogger extends AbstractLogger { const IDENT = 'Friendica'; @@ -68,23 +66,50 @@ class SyslogLogger extends AbstractFriendicaLogger */ 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 + * @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, Profiler $profiler, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER) + public function __construct($channel, Introspection $introspection, $level = LogLevel::NOTICE, $logOpts = LOG_PID, $logFacility = LOG_USER) { - parent::__construct($channel, $introspection, $profiler); + parent::__construct($channel, $introspection); $this->logOpts = $logOpts; $this->logFacility = $logFacility; $this->logLevel = $this->mapLevelToPriority($level); $this->introspection->addClasses(array(self::class)); } + /** + * Adds a new entry to the syslog + * + * @param int $level + * @param string $message + * @param array $context + * + * @throws InternalServerErrorException if the syslog isn't available + */ + protected function addEntry($level, $message, $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 ) * @@ -97,12 +122,20 @@ class SyslogLogger extends AbstractFriendicaLogger public function mapLevelToPriority($level) { if (!array_key_exists($level, $this->logLevels)) { - throw new InvalidArgumentException('LogLevel \'' . $level . '\' isn\'t valid.'); + throw new \InvalidArgumentException(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 @@ -114,19 +147,15 @@ class SyslogLogger extends AbstractFriendicaLogger */ 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 . '""'); + set_error_handler([$this, 'customErrorHandler']); + $opened = openlog(self::IDENT, $this->logOpts, $this->logFacility); + restore_error_handler(); + + if (!$opened) { + throw new \UnexpectedValueException(sprintf('Can\'t open syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility)); } - syslog($priority, $message); - } - - /** - * Closes the Syslog - */ - public function close() - { - closelog(); + $this->syslogWrapper($priority, $message); } /** @@ -153,24 +182,25 @@ class SyslogLogger extends AbstractFriendicaLogger return $logMessage; } + private function customErrorHandler($code, $msg) + { + $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg); + } + /** - * Adds a new entry to the syslog - * - * @param int $level - * @param string $message - * @param array $context + * A syslog wrapper to make syslog functionality testable * - * @throws InternalServerErrorException if the syslog isn't available + * @param int $level The syslog priority + * @param string $entry The message to send to the syslog function */ - protected function addEntry($level, $message, $context = []) + protected function syslogWrapper($level, $entry) { - $logLevel = $this->mapLevelToPriority($level); + set_error_handler([$this, 'customErrorHandler']); + $written = syslog($level, $entry); + restore_error_handler(); - if ($logLevel >= $this->logLevel) { - return; + if (!$written) { + throw new \UnexpectedValueException(sprintf('Can\'t write into syslog for ident "%s" and facility "%s": ' . $this->errorMessage, $this->channel, $this->logFacility)); } - - $formattedLog = $this->formatLog($level, $message, $context); - $this->write($level, $formattedLog); } }