diff options
Diffstat (limited to 'vendor/gipfl/log')
22 files changed, 872 insertions, 0 deletions
diff --git a/vendor/gipfl/log/LICENSE b/vendor/gipfl/log/LICENSE new file mode 100644 index 0000000..dd88e09 --- /dev/null +++ b/vendor/gipfl/log/LICENSE @@ -0,0 +1,21 @@ +The MIT License + +Copyright (c) 2018 Thomas Gelf + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/vendor/gipfl/log/composer.json b/vendor/gipfl/log/composer.json new file mode 100644 index 0000000..c300158 --- /dev/null +++ b/vendor/gipfl/log/composer.json @@ -0,0 +1,31 @@ +{ + "name": "gipfl/log", + "description": "Lightweight PSR-3 compatible logger", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Thomas Gelf", + "email": "thomas@gelf.net" + } + ], + "config": { + "sort-packages": true + }, + "autoload": { + "psr-4": { + "gipfl\\Log\\": "src" + } + }, + "require": { + "php": ">=5.6.0", + "psr/log": "^1", + "ext-iconv": "*" + }, + "require-dev": { + "gipfl/protocol-jsonrpc": ">=0.2", + "gipfl/systemd": ">=0.3" + }, + "suggest": { + } +} diff --git a/vendor/gipfl/log/src/AdditionalContextLogger.php b/vendor/gipfl/log/src/AdditionalContextLogger.php new file mode 100644 index 0000000..92891c5 --- /dev/null +++ b/vendor/gipfl/log/src/AdditionalContextLogger.php @@ -0,0 +1,25 @@ +<?php + +namespace gipfl\Log; + +use Psr\Log\LoggerInterface; + +class AdditionalContextLogger extends Logger +{ + /** @var array */ + protected $context; + + /** @var LoggerInterface */ + protected $wrappedLogger; + + public function __construct(array $context, LoggerInterface $logger) + { + $this->context = $context; + $this->wrappedLogger = $logger; + } + + public function log($level, $message, array $context = []) + { + $this->wrappedLogger->log($level, $message, $context + $this->context); + } +} diff --git a/vendor/gipfl/log/src/DummyLogger.php b/vendor/gipfl/log/src/DummyLogger.php new file mode 100644 index 0000000..3e7b0c4 --- /dev/null +++ b/vendor/gipfl/log/src/DummyLogger.php @@ -0,0 +1,44 @@ +<?php + +namespace gipfl\Log; + +use Psr\Log\LoggerInterface; + +class DummyLogger implements LoggerInterface +{ + public function emergency($message, array $context = []) + { + } + + public function alert($message, array $context = []) + { + } + + public function critical($message, array $context = []) + { + } + + public function error($message, array $context = []) + { + } + + public function warning($message, array $context = []) + { + } + + public function notice($message, array $context = []) + { + } + + public function info($message, array $context = []) + { + } + + public function debug($message, array $context = []) + { + } + + public function log($level, $message, array $context = []) + { + } +} diff --git a/vendor/gipfl/log/src/Filter/LogLevelFilter.php b/vendor/gipfl/log/src/Filter/LogLevelFilter.php new file mode 100644 index 0000000..f26773a --- /dev/null +++ b/vendor/gipfl/log/src/Filter/LogLevelFilter.php @@ -0,0 +1,39 @@ +<?php + +namespace gipfl\Log\Filter; + +use gipfl\Log\LogFilter; +use gipfl\Log\LogLevel; + +class LogLevelFilter implements LogFilter +{ + /** @var int */ + protected $level; + + /** + * @param string $level + */ + public function __construct($level) + { + $this->level = LogLevel::mapNameToNumeric($level); + } + + /** + * @param string $level + * @param string $message + * @param array $context + * @return bool + */ + public function wants($level, $message, $context = []) + { + return LogLevel::mapNameToNumeric($level) <= $this->level; + } + + /** + * @return string + */ + public function getLevel() + { + return LogLevel::mapNumericToName($this->level); + } +} diff --git a/vendor/gipfl/log/src/Formatter/StdOutFormatter.php b/vendor/gipfl/log/src/Formatter/StdOutFormatter.php new file mode 100644 index 0000000..363a0b2 --- /dev/null +++ b/vendor/gipfl/log/src/Formatter/StdOutFormatter.php @@ -0,0 +1,34 @@ +<?php + +namespace gipfl\Log\Formatter; + +use gipfl\Log\LogFormatter; +use function date; +use function microtime; + +class StdOutFormatter implements LogFormatter +{ + protected $dateFormat = 'Y-m-d H:i:s'; + + protected $showTimestamp = true; + + public function format($level, $message, $context = []) + { + // TODO: replace placeholders! + return $this->renderDatePrefix() . sprintf($message, $context); + } + + protected function renderDatePrefix() + { + if ($this->showTimestamp) { + return date($this->dateFormat, microtime(true)); + } + + return ''; + } + + public function setShowTimestamp($show = true) + { + $this->showTimestamp = $show; + } +} diff --git a/vendor/gipfl/log/src/IcingaWeb/IcingaLogger.php b/vendor/gipfl/log/src/IcingaWeb/IcingaLogger.php new file mode 100644 index 0000000..d5de5e9 --- /dev/null +++ b/vendor/gipfl/log/src/IcingaWeb/IcingaLogger.php @@ -0,0 +1,29 @@ +<?php + +namespace gipfl\Log\IcingaWeb; + +use Icinga\Application\Logger as IcingaApplicationLogger; +use Icinga\Exception\ConfigurationError; +use Psr\Log\LoggerInterface; + +class IcingaLogger extends IcingaApplicationLogger +{ + public static function replace(LoggerInterface $logger) + { + static::replaceRunningInstance(new LoggerLogWriter($logger)); + } + + public static function replaceRunningInstance(LoggerLogWriter $writer, $level = null) + { + try { + $instance = static::$instance; + if ($level !== null) { + $instance->setLevel($level); + } + + $instance->writer = $writer; + } catch (ConfigurationError $e) { + static::$instance->error($e->getMessage()); + } + } +} diff --git a/vendor/gipfl/log/src/IcingaWeb/LoggerLogWriter.php b/vendor/gipfl/log/src/IcingaWeb/LoggerLogWriter.php new file mode 100644 index 0000000..aea4da4 --- /dev/null +++ b/vendor/gipfl/log/src/IcingaWeb/LoggerLogWriter.php @@ -0,0 +1,32 @@ +<?php + +namespace gipfl\Log\IcingaWeb; + +use Icinga\Application\Logger as IcingaApplicationLogger; +use Icinga\Application\Logger\LogWriter; +use Icinga\Data\ConfigObject; +use Psr\Log\LoggerInterface; + +class LoggerLogWriter extends LogWriter +{ + protected $logger; + + protected static $severityMap = [ + IcingaApplicationLogger::DEBUG => 'debug', + IcingaApplicationLogger::INFO => 'info', + IcingaApplicationLogger::WARNING => 'warning', + IcingaApplicationLogger::ERROR => 'error', + ]; + + public function __construct(LoggerInterface $logger) + { + parent::__construct(new ConfigObject([])); + $this->logger = $logger; + } + + public function log($severity, $message) + { + $severity = static::$severityMap[$severity]; + $this->logger->$severity($message); + } +} diff --git a/vendor/gipfl/log/src/LogFilter.php b/vendor/gipfl/log/src/LogFilter.php new file mode 100644 index 0000000..79749f7 --- /dev/null +++ b/vendor/gipfl/log/src/LogFilter.php @@ -0,0 +1,14 @@ +<?php + +namespace gipfl\Log; + +interface LogFilter +{ + /** + * @param string $level + * @param string $message + * @param array $context + * @return bool + */ + public function wants($level, $message, $context = []); +} diff --git a/vendor/gipfl/log/src/LogFormatter.php b/vendor/gipfl/log/src/LogFormatter.php new file mode 100644 index 0000000..e6529dc --- /dev/null +++ b/vendor/gipfl/log/src/LogFormatter.php @@ -0,0 +1,8 @@ +<?php + +namespace gipfl\Log; + +interface LogFormatter +{ + public function format($level, $message, $context = []); +} diff --git a/vendor/gipfl/log/src/LogLevel.php b/vendor/gipfl/log/src/LogLevel.php new file mode 100644 index 0000000..599420e --- /dev/null +++ b/vendor/gipfl/log/src/LogLevel.php @@ -0,0 +1,66 @@ +<?php + +namespace gipfl\Log; + +use InvalidArgumentException; +use Psr\Log\LogLevel as PsrLogLevel; + +class LogLevel extends PsrLogLevel +{ + const LEVEL_EMERGENCY = 0; + const LEVEL_ALERT = 1; + const LEVEL_CRITICAL = 2; + const LEVEL_ERROR = 3; + const LEVEL_WARNING = 4; + const LEVEL_NOTICE = 5; + const LEVEL_INFO = 6; + const LEVEL_DEBUG = 7; + + const MAP_NAME_TO_LEVEL = [ + self::EMERGENCY => self::LEVEL_EMERGENCY, + self::ALERT => self::LEVEL_ALERT, + self::CRITICAL => self::LEVEL_CRITICAL, + self::ERROR => self::LEVEL_ERROR, + self::WARNING => self::LEVEL_WARNING, + self::NOTICE => self::LEVEL_NOTICE, + self::INFO => self::LEVEL_INFO, + self::DEBUG => self::LEVEL_DEBUG, + ]; + + const MAP_LEVEL_TO_NAME = [ + self::LEVEL_EMERGENCY => self::EMERGENCY, + self::LEVEL_ALERT => self::ALERT, + self::LEVEL_CRITICAL => self::CRITICAL, + self::LEVEL_ERROR => self::ERROR, + self::LEVEL_WARNING => self::WARNING, + self::LEVEL_NOTICE => self::NOTICE, + self::LEVEL_INFO => self::INFO, + self::LEVEL_DEBUG => self::DEBUG, + ]; + + /** + * @param string $name + * @return int + */ + public static function mapNameToNumeric($name) + { + if (array_key_exists($name, static::MAP_NAME_TO_LEVEL)) { + return static::MAP_NAME_TO_LEVEL[$name]; + } + + throw new InvalidArgumentException("$name is not a valid log level name"); + } + + /** + * @param int $number + * @return string + */ + public static function mapNumericToName($number) + { + if (array_key_exists($number, static::MAP_LEVEL_TO_NAME)) { + return static::MAP_LEVEL_TO_NAME[$number]; + } + + throw new InvalidArgumentException("$number is not a valid numeric log level"); + } +} diff --git a/vendor/gipfl/log/src/LogWriter.php b/vendor/gipfl/log/src/LogWriter.php new file mode 100644 index 0000000..8b91d5c --- /dev/null +++ b/vendor/gipfl/log/src/LogWriter.php @@ -0,0 +1,8 @@ +<?php + +namespace gipfl\Log; + +interface LogWriter +{ + public function write($level, $message); +} diff --git a/vendor/gipfl/log/src/LogWriterWithContext.php b/vendor/gipfl/log/src/LogWriterWithContext.php new file mode 100644 index 0000000..4372eda --- /dev/null +++ b/vendor/gipfl/log/src/LogWriterWithContext.php @@ -0,0 +1,8 @@ +<?php + +namespace gipfl\Log; + +interface LogWriterWithContext extends LogWriter +{ + public function write($level, $message, $context = []); +} diff --git a/vendor/gipfl/log/src/Logger.php b/vendor/gipfl/log/src/Logger.php new file mode 100644 index 0000000..1cbeb78 --- /dev/null +++ b/vendor/gipfl/log/src/Logger.php @@ -0,0 +1,169 @@ +<?php + +namespace gipfl\Log; + +use Psr\Log\LoggerInterface; +use function array_values; +use function spl_object_hash; + +class Logger implements LoggerInterface +{ + /** @deprecated please use LogLevel::LEVEL_EMERGENCY */ + const LEVEL_EMERGENCY = LogLevel::LEVEL_EMERGENCY; + /** @deprecated please use LogLevel::LEVEL_ALERT */ + const LEVEL_ALERT = LogLevel::LEVEL_ALERT; + /** @deprecated please use LogLevel::LEVEL_CRITICAL */ + const LEVEL_CRITICAL = LogLevel::LEVEL_CRITICAL; + /** @deprecated please use LogLevel::LEVEL_ERROR */ + const LEVEL_ERROR = LogLevel::LEVEL_ERROR; + /** @deprecated please use LogLevel::LEVEL_WARNING */ + const LEVEL_WARNING = LogLevel::LEVEL_WARNING; + /** @deprecated please use LogLevel::LEVEL_NOTICE */ + const LEVEL_NOTICE = LogLevel::LEVEL_NOTICE; + /** @deprecated please use LogLevel::LEVEL_INFO */ + const LEVEL_INFO = LogLevel::LEVEL_INFO; + /** @deprecated please use LogLevel::LEVEL_DEBUG */ + const LEVEL_DEBUG = LogLevel::LEVEL_DEBUG; + /** @deprecated Please use LogLevel::MAP_NAME_TO_LEVEL */ + const MAP_NAME_TO_LEVEL = LogLevel::MAP_NAME_TO_LEVEL; + + /** @var LogWriter[] */ + protected $writers = []; + + /** @var LogFilter[] */ + protected $filters = []; + + /** + * @param LogWriter $writer + */ + public function addWriter(LogWriter $writer) + { + $this->writers[spl_object_hash($writer)] = $writer; + } + + /** + * @param LogFilter $filter + */ + public function addFilter(LogFilter $filter) + { + $this->filters[spl_object_hash($filter)] = $filter; + } + + /** + * @return LogWriter[] + */ + public function getWriters() + { + return array_values($this->writers); + } + + /** + * @return LogFilter[] + */ + public function getFilters() + { + return array_values($this->filters); + } + + /** + * @param LogWriter $writer + */ + public function removeWriter(LogWriter $writer) + { + unset($this->filters[spl_object_hash($writer)]); + } + + /** + * @param LogFilter $filter + */ + public function removeFilter(LogFilter $filter) + { + unset($this->filters[spl_object_hash($filter)]); + } + + /** + * @deprecated Please use LogLevel::mapNameToNumeric() + */ + public static function mapLogLevel($name) + { + return LogLevel::mapNameToNumeric($name); + } + + public function emergency($message, array $context = []) + { + $this->log(__FUNCTION__, $message, $context); + } + + public function alert($message, array $context = []) + { + $this->log(__FUNCTION__, $message, $context); + } + + public function critical($message, array $context = []) + { + $this->log(__FUNCTION__, $message, $context); + } + + public function error($message, array $context = []) + { + $this->log(__FUNCTION__, $message, $context); + } + + public function warning($message, array $context = []) + { + $this->log(__FUNCTION__, $message, $context); + } + + public function notice($message, array $context = []) + { + $this->log(__FUNCTION__, $message, $context); + } + + public function info($message, array $context = []) + { + $this->log(__FUNCTION__, $message, $context); + } + + public function debug($message, array $context = []) + { + $this->log(__FUNCTION__, $message, $context); + } + + public function wants($level, $message, array $context = []) + { + foreach ($this->filters as $filter) { + if (! $filter->wants($level, $message, $context)) { + return false; + } + } + + return true; + } + + public function log($level, $message, array $context = []) + { + if (! $this->wants($level, $message, $context)) { + return; + } + + foreach ($this->writers as $writer) { + if ($writer instanceof LogWriterWithContext) { + $writer->write($level, $message, $context); + } else { + $writer->write($level, $this->formatMessage( + $message, + $context + )); + } + } + } + + protected function formatMessage($message, $context = []) + { + if (empty($context)) { + return $message; + } else { + return \sprintf($message, $context); + } + } +} diff --git a/vendor/gipfl/log/src/PrefixLogger.php b/vendor/gipfl/log/src/PrefixLogger.php new file mode 100644 index 0000000..a7273a2 --- /dev/null +++ b/vendor/gipfl/log/src/PrefixLogger.php @@ -0,0 +1,25 @@ +<?php + +namespace gipfl\Log; + +use Psr\Log\LoggerInterface; + +class PrefixLogger extends Logger +{ + /** @var string */ + protected $prefix; + + /** @var LoggerInterface */ + protected $wrappedLogger; + + public function __construct($prefix, LoggerInterface $logger) + { + $this->prefix = $prefix; + $this->wrappedLogger = $logger; + } + + public function log($level, $message, array $context = []) + { + $this->wrappedLogger->log($level, $this->prefix . $message, $context); + } +} diff --git a/vendor/gipfl/log/src/Writer/JournaldLogger.php b/vendor/gipfl/log/src/Writer/JournaldLogger.php new file mode 100644 index 0000000..b3b0125 --- /dev/null +++ b/vendor/gipfl/log/src/Writer/JournaldLogger.php @@ -0,0 +1,61 @@ +<?php + +namespace gipfl\Log\Writer; + +use gipfl\Log\LogLevel; +use gipfl\Log\LogWriterWithContext; +use gipfl\SystemD\NotificationSocket; +use React\EventLoop\LoopInterface; +use React\Stream\WritableStreamInterface; + +class JournaldLogger implements LogWriterWithContext +{ + const JOURNALD_SOCKET = '/run/systemd/journal/socket'; + + protected $socket; + + protected $extraFields = []; + + /** + * SystemdStdoutWriter constructor. + * @param LoopInterface $loop + * @param WritableStreamInterface|null $stdOut + */ + public function __construct($socket = null) + { + $this->socket = new NotificationSocket($socket ?: self::JOURNALD_SOCKET); + } + + /** + * @param string|null $identifier + * @return $this + */ + public function setIdentifier($identifier) + { + return $this->setExtraField('SYSLOG_IDENTIFIER', $identifier); + } + + /** + * @param string $name + * @param ?string $value + * @return $this + */ + public function setExtraField($name, $value) + { + if ($value === null) { + unset($this->extraFields[$name]); + } else { + $this->extraFields[$name] = (string) $value; + } + + return $this; + } + + public function write($level, $message, $context = []) + { + $this->socket->send([ + 'MESSAGE' => $message, + 'PRIORITY' => LogLevel::mapNameToNumeric($level), + ] + $context + $this->extraFields); + } +} diff --git a/vendor/gipfl/log/src/Writer/JsonRpcConnectionWriter.php b/vendor/gipfl/log/src/Writer/JsonRpcConnectionWriter.php new file mode 100644 index 0000000..e4042e7 --- /dev/null +++ b/vendor/gipfl/log/src/Writer/JsonRpcConnectionWriter.php @@ -0,0 +1,51 @@ +<?php + +namespace gipfl\Log\Writer; + +use gipfl\Log\LogWriterWithContext; +use gipfl\Protocol\JsonRpc\JsonRpcConnection; +use function iconv; +use function microtime; + +class JsonRpcConnectionWriter implements LogWriterWithContext +{ + const DEFAULT_RPC_METHOD = 'logger.log'; + + /** @var JsonRpcConnection */ + protected $connection; + + /** @var string */ + protected $method = self::DEFAULT_RPC_METHOD; + + /** @var array */ + protected $defaultContext; + + /** + * @param JsonRpcConnection $connection + * @param array $defaultContext + */ + public function __construct(JsonRpcConnection $connection, $defaultContext = []) + { + $this->connection = $connection; + $this->defaultContext = $defaultContext; + } + + /** + * @param string $method + */ + public function setMethod($method) + { + $this->method = $method; + } + + public function write($level, $message, $context = []) + { + $message = iconv('UTF-8', 'UTF-8//IGNORE', $message); + $this->connection->notification($this->method, $this->defaultContext + [ + 'level' => $level, + 'timestamp' => microtime(true), + 'message' => $message, + 'context' => $context, + ]); + } +} diff --git a/vendor/gipfl/log/src/Writer/JsonRpcWriter.php b/vendor/gipfl/log/src/Writer/JsonRpcWriter.php new file mode 100644 index 0000000..a2fa505 --- /dev/null +++ b/vendor/gipfl/log/src/Writer/JsonRpcWriter.php @@ -0,0 +1,54 @@ +<?php + +namespace gipfl\Log\Writer; + +use gipfl\Log\LogWriterWithContext; +use gipfl\Protocol\JsonRpc\Connection; +use function iconv; +use function microtime; + +/** + * @deprecated + */ +class JsonRpcWriter implements LogWriterWithContext +{ + const DEFAULT_RPC_METHOD = 'logger.log'; + + /** @var Connection */ + protected $connection; + + /** @var string */ + protected $method = self::DEFAULT_RPC_METHOD; + + /** @var array */ + protected $defaultContext; + + /** + * JsonRpcWriter constructor. + * @param Connection $connection + */ + public function __construct(Connection $connection, $defaultContext = []) + { + $this->connection = $connection; + $this->defaultContext = $defaultContext; + } + + /** + * @param string $method + */ + public function setMethod($method) + { + $this->method = $method; + } + + public function write($level, $message, $context = []) + { + $message = iconv('UTF-8', 'UTF-8//IGNORE', $message); + $this->connection->notification($this->method, $this->defaultContext + [ + 'level' => $level, + 'timestamp' => microtime(true), + 'message' => $message, + 'context' => $context, + ]); + } +} diff --git a/vendor/gipfl/log/src/Writer/ProxyLogWriter.php b/vendor/gipfl/log/src/Writer/ProxyLogWriter.php new file mode 100644 index 0000000..78d5262 --- /dev/null +++ b/vendor/gipfl/log/src/Writer/ProxyLogWriter.php @@ -0,0 +1,14 @@ +<?php + +namespace gipfl\Log\Writer; + +use gipfl\Log\Logger; +use gipfl\Log\LogWriterWithContext; + +class ProxyLogWriter extends Logger implements LogWriterWithContext +{ + public function write($level, $message, $context = []) + { + $this->log($level, $message, $context); + } +} diff --git a/vendor/gipfl/log/src/Writer/SyslogWriter.php b/vendor/gipfl/log/src/Writer/SyslogWriter.php new file mode 100644 index 0000000..86b8254 --- /dev/null +++ b/vendor/gipfl/log/src/Writer/SyslogWriter.php @@ -0,0 +1,34 @@ +<?php + +namespace gipfl\Log\Writer; + +use gipfl\Log\LogLevel; +use gipfl\Log\LogWriter; +use function openlog; +use function syslog; + +class SyslogWriter implements LogWriter +{ + /** @var string */ + protected $ident; + + /** @var string */ + protected $facility; + + /** + * SyslogWriter constructor. + * @param string $ident + * @param string $facility + */ + public function __construct($ident, $facility) + { + $this->ident = $ident; + $this->facility = $facility; + } + + public function write($level, $message) + { + openlog($this->ident, LOG_PID, $this->facility); + syslog(LogLevel::mapNameToNumeric($level), str_replace("\n", ' ', $message)); + } +} diff --git a/vendor/gipfl/log/src/Writer/SystemdStdoutWriter.php b/vendor/gipfl/log/src/Writer/SystemdStdoutWriter.php new file mode 100644 index 0000000..bb66525 --- /dev/null +++ b/vendor/gipfl/log/src/Writer/SystemdStdoutWriter.php @@ -0,0 +1,61 @@ +<?php + +namespace gipfl\Log\Writer; + +use gipfl\Log\LogLevel; +use gipfl\Log\LogWriter; +use InvalidArgumentException; +use React\EventLoop\LoopInterface; +use React\Stream\WritableResourceStream; +use React\Stream\WritableStreamInterface; +use function is_int; +use function sprintf; + +class SystemdStdoutWriter implements LogWriter +{ + // local0 + const DEFAULT_FACILITY = 10; + + /** @var WritableStreamInterface */ + protected $stdOut; + + /** @var int */ + protected $facility = self::DEFAULT_FACILITY; + + /** + * SystemdStdoutWriter constructor. + * @param LoopInterface $loop + * @param WritableStreamInterface|null $stdOut + */ + public function __construct(LoopInterface $loop, WritableStreamInterface $stdOut = null) + { + if ($stdOut === null) { + $this->stdOut = new WritableResourceStream(STDOUT, $loop); + } else { + $this->stdOut = $stdOut; + } + } + + /** + * @param int $facility + */ + public function setFacility($facility) + { + if (! is_int($facility)) { + throw new InvalidArgumentException('Facility needs to be an integer'); + } + if ($facility < 0 || $facility > 23) { + throw new InvalidArgumentException("Facility needs to be between 0 and 23, got $facility"); + } + $this->facility = $facility; + } + + public function write($level, $message) + { + $this->stdOut->write(sprintf( + "<%d>%s\n", + LogLevel::mapNameToNumeric($level), + $message + )); + } +} diff --git a/vendor/gipfl/log/src/Writer/WritableStreamWriter.php b/vendor/gipfl/log/src/Writer/WritableStreamWriter.php new file mode 100644 index 0000000..4ae877f --- /dev/null +++ b/vendor/gipfl/log/src/Writer/WritableStreamWriter.php @@ -0,0 +1,44 @@ +<?php + +namespace gipfl\Log\Writer; + +use gipfl\Log\LogWriter; +use React\Stream\WritableStreamInterface; + +class WritableStreamWriter implements LogWriter +{ + const DEFAULT_SEPARATOR = PHP_EOL; + + /** @var WritableStreamInterface */ + protected $stream; + + /** @var string */ + protected $separator = self::DEFAULT_SEPARATOR; + + /** + * WritableStreamWriter constructor. + * @param WritableStreamInterface $stream + */ + public function __construct(WritableStreamInterface $stream) + { + $this->setStream($stream); + } + + /** + * @param string $separator + */ + public function setSeparator($separator) + { + $this->separator = $separator; + } + + public function setStream(WritableStreamInterface $stream) + { + $this->stream = $stream; + } + + public function write($level, $message) + { + $this->stream->write("$level: $message" . $this->separator); + } +} |