Skip to content

Commit

Permalink
Adds interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
donatj committed Jan 19, 2024
1 parent f238270 commit 72452a9
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 16 deletions.
68 changes: 67 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).
Expand Down Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions src/Interfaces/LoggerWithContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Corpus\Loggers\Interfaces;

use Psr\Log\LoggerInterface;

interface LoggerWithContextInterface extends LoggerInterface, WithContextInterface {

}
9 changes: 9 additions & 0 deletions src/Interfaces/MultiLoggerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Corpus\Loggers\Interfaces;

use Psr\Log\LoggerInterface;

interface MultiLoggerInterface extends LoggerInterface, WithAdditionalLoggersInterface {

}
15 changes: 15 additions & 0 deletions src/Interfaces/WithAdditionalLoggersInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Corpus\Loggers\Interfaces;

use Psr\Log\LoggerInterface;

interface WithAdditionalLoggersInterface {

/**
* withAdditionalLoggers returns a new instance with the given loggers
* added to the list of loggers to delegate to.
*/
public function withAdditionalLoggers( LoggerInterface ...$loggers ) : self;

}
19 changes: 19 additions & 0 deletions src/Interfaces/WithContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Corpus\Loggers\Interfaces;

interface WithContextInterface {

/**
* Returns a new instance with the given context
* replacing the existing context.
*/
public function withContext( array $context ) : self;

/**
* Returns a new instance with the given context
* added to the existing context.
*/
public function withAddedContext( array $context ) : self;

}
11 changes: 2 additions & 9 deletions src/LoggerWithContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Corpus\Loggers;

use Corpus\Loggers\Interfaces\LoggerWithContextInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;

Expand All @@ -12,7 +13,7 @@
* This is useful for adding context to all log messages, such as the current
* request ID, IP address or the current user ID.
*/
class LoggerWithContext implements LoggerInterface {
class LoggerWithContext implements LoggerWithContextInterface {

use LoggerTrait;

Expand Down Expand Up @@ -40,21 +41,13 @@ public function log( $level, $message, array $context = [] ) : void {
$this->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;

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);
Expand Down
2 changes: 1 addition & 1 deletion src/MemoryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions src/MultiLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand Down

0 comments on commit 72452a9

Please sign in to comment.