Skip to content

Commit

Permalink
Improve docs
Browse files Browse the repository at this point in the history
Signed-off-by: Jesse Donat <[email protected]>
  • Loading branch information
donatj committed Nov 25, 2023
1 parent b1d5b1a commit f6a260d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,79 @@ Returns a new instance with the specified verbosity level callback.
function log($level, $message [, array $context = []]) : void
```

### Class: \Corpus\Loggers\LoggerWithContext

LoggerWithContext is a logger that adds a given context to all log messages
before delegating to another logger.

This is useful for adding context to all log messages, such as the current
request ID, IP address or the current user ID.

#### Method: LoggerWithContext->__construct

```php
function __construct(\Psr\Log\LoggerInterface $logger [, array $context = []])
```

Create a new LoggerWithContext instance with the given logger and context.

The given context will be added to all log messages.

##### Parameters:

- ***\Psr\Log\LoggerInterface*** `$logger` - The logger to delegate to.
- ***array*** `$context` - The context to add to all log messages.

---

#### Method: LoggerWithContext->log

```php
function log($level, $message [, array $context = []])
```

---

#### Method: LoggerWithContext->withContext

```php
function withContext(array $context) : self
```

Returns a new instance with the given context
replacing the existing context.

---

#### Method: LoggerWithContext->withAddedContext

```php
function withAddedContext(array $context) : self
```

Returns a new instance with the given context
added to the existing context.

### Class: \Corpus\Loggers\LogLevelFilter

#### Method: LogLevelFilter->__construct

```php
function __construct(\Corpus\Loggers\LoggerWithContext $logger, array $levels [, bool $exclude = false])
```

##### Parameters:

- ***string[]*** `$levels`

---

#### Method: LogLevelFilter->log

```php
function log($level, $message [, array $context = []])
```

### Class: \Corpus\Loggers\LogLevelLoggerMux

LogLevelLoggerMux multiplexes logs to different loggers based on the log level.
Expand Down
15 changes: 12 additions & 3 deletions src/LoggerWithContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Psr\Log\LoggerTrait;

/**
* LoggerWithContext is a logger that adds a given context to all log messages.
* LoggerWithContext is a logger that adds a given context to all log messages
* before delegating to another logger.
*
* This is useful for adding context to all log messages, such as the current
* request ID, IP address or the current user ID.
Expand All @@ -18,6 +19,14 @@ class LoggerWithContext implements LoggerInterface {
private array $context;
private LoggerInterface $logger;

/**
* Create a new LoggerWithContext instance with the given logger and context.
*
* The given context will be added to all log messages.
*
* @param LoggerInterface $logger The logger to delegate to.
* @param array $context The context to add to all log messages.
*/
public function __construct( LoggerInterface $logger, array $context = [] ) {
$this->logger = $logger;
$this->context = $context;
Expand All @@ -31,7 +40,7 @@ public function log( $level, $message, array $context = [] ) : void {
}

/**
* withContext returns a new instance with the given context
* Returns a new instance with the given context
* replacing the existing context.
*/
public function withContext( array $context ) : self {
Expand All @@ -42,7 +51,7 @@ public function withContext( array $context ) : self {
}

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

0 comments on commit f6a260d

Please sign in to comment.