From 72452a9a18eb11ecea8c71434fa324784609f02e Mon Sep 17 00:00:00 2001 From: Jesse Donat Date: Fri, 19 Jan 2024 14:32:00 -0600 Subject: [PATCH] Adds interfaces --- README.md | 68 ++++++++++++++++++- src/Interfaces/LoggerWithContextInterface.php | 9 +++ src/Interfaces/MultiLoggerInterface.php | 9 +++ .../WithAdditionalLoggersInterface.php | 15 ++++ src/Interfaces/WithContextInterface.php | 19 ++++++ src/LoggerWithContext.php | 11 +-- src/MemoryLogger.php | 2 +- src/MultiLogger.php | 7 +- 8 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 src/Interfaces/LoggerWithContextInterface.php create mode 100644 src/Interfaces/MultiLoggerInterface.php create mode 100644 src/Interfaces/WithAdditionalLoggersInterface.php create mode 100644 src/Interfaces/WithContextInterface.php diff --git a/README.md b/README.md index 61fcb7d..00cf2d3 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Latest Stable Version](https://poser.pugx.org/corpus/loggers/version)](https://packagist.org/packages/corpus/loggers) [![License](https://poser.pugx.org/corpus/loggers/license)](https://packagist.org/packages/corpus/loggers) -[![ci.yml](https://github.com/CorpusPHP/Loggers/actions/workflows/ci.yml/badge.svg?)](https://github.com/CorpusPHP/Loggers/actions/workflows/ci.yml) +[![ci.yml](https://github.com/CorpusPHP/Loggers/actions/workflows/ci.yml/badge.svg)](https://github.com/CorpusPHP/Loggers/actions/workflows/ci.yml) Utilities for and Loggers for [PSR-3: Logger Interface](https://www.php-fig.org/psr/psr-3/). @@ -97,6 +97,72 @@ array ( ## Documentation +### Class: \Corpus\Loggers\Interfaces\LoggerWithContextInterface + +#### Method: LoggerWithContextInterface->withContext + +```php +function withContext(array $context) : self +``` + +Returns a new instance with the given context +replacing the existing context. + +--- + +#### Method: LoggerWithContextInterface->withAddedContext + +```php +function withAddedContext(array $context) : self +``` + +Returns a new instance with the given context +added to the existing context. + +### Class: \Corpus\Loggers\Interfaces\MultiLoggerInterface + +#### Method: MultiLoggerInterface->withAdditionalLoggers + +```php +function withAdditionalLoggers(\Psr\Log\LoggerInterface ...$loggers) : self +``` + +withAdditionalLoggers returns a new instance with the given loggers +added to the list of loggers to delegate to. + +### Class: \Corpus\Loggers\Interfaces\WithAdditionalLoggersInterface + +#### Method: WithAdditionalLoggersInterface->withAdditionalLoggers + +```php +function withAdditionalLoggers(\Psr\Log\LoggerInterface ...$loggers) : self +``` + +withAdditionalLoggers returns a new instance with the given loggers +added to the list of loggers to delegate to. + +### Class: \Corpus\Loggers\Interfaces\WithContextInterface + +#### Method: WithContextInterface->withContext + +```php +function withContext(array $context) : self +``` + +Returns a new instance with the given context +replacing the existing context. + +--- + +#### Method: WithContextInterface->withAddedContext + +```php +function withAddedContext(array $context) : self +``` + +Returns a new instance with the given context +added to the existing context. + ### Class: \Corpus\Loggers\LoggerVerbosityFilter LoggerVerbosityFilter mutes log messages based on a given integer verbosity level. diff --git a/src/Interfaces/LoggerWithContextInterface.php b/src/Interfaces/LoggerWithContextInterface.php new file mode 100644 index 0000000..371a821 --- /dev/null +++ b/src/Interfaces/LoggerWithContextInterface.php @@ -0,0 +1,9 @@ +logger->log($level, $message, array_merge($this->context, $context)); } - /** - * Returns a new instance with the given context - * replacing the existing context. - */ public function withContext( array $context ) : self { $clone = clone $this; $clone->context = $context; @@ -51,10 +48,6 @@ public function withContext( array $context ) : self { return $clone; } - /** - * Returns a new instance with the given context - * added to the existing context. - */ public function withAddedContext( array $context ) : self { $clone = clone $this; $clone->context = array_merge($clone->context, $context); diff --git a/src/MemoryLogger.php b/src/MemoryLogger.php index 5940f50..0e328a2 100644 --- a/src/MemoryLogger.php +++ b/src/MemoryLogger.php @@ -55,7 +55,7 @@ public function clearLogs() : void { * * It is exposed publicly so that it may be used in tests. */ - public static function makeLogRecord($level, $message, array $context = []) : array { + public static function makeLogRecord( $level, $message, array $context = [] ) : array { return [ self::KEY_LEVEL => $level, self::KEY_MESSAGE => $message, diff --git a/src/MultiLogger.php b/src/MultiLogger.php index 1889c70..15498e6 100644 --- a/src/MultiLogger.php +++ b/src/MultiLogger.php @@ -2,13 +2,14 @@ namespace Corpus\Loggers; +use Corpus\Loggers\Interfaces\MultiLoggerInterface; use Psr\Log\LoggerInterface; use Psr\Log\LoggerTrait; /** * MultiLogger is a logger that delegates to multiple other loggers. */ -class MultiLogger implements LoggerInterface { +class MultiLogger implements MultiLoggerInterface { use LoggerTrait; @@ -22,10 +23,6 @@ public function __construct( LoggerInterface ...$loggers ) { $this->loggers = $loggers; } - /** - * withAdditionalLoggers returns a new instance with the given loggers - * added to the list of loggers to delegate to. - */ public function withAdditionalLoggers( LoggerInterface ...$loggers ) : self { $clone = clone $this; $clone->loggers = array_merge($clone->loggers, $loggers);