@@ -5,7 +5,9 @@ Wrap your PSR-3 logger with context accumulation and callable context elements.
55## Inspiration
66
77Inspired by the [ similar functionality] in [ pinojs] . Design and implementation
8- details differ, but the core idea remains:
8+ details differ, but the core idea remains: push scoped context on a logger
9+ and have it automatically pop off when the scope ends. This push/pop behavior is
10+ why I think of it as a "stack" logger.
911
1012## Approach
1113
@@ -17,8 +19,19 @@ additional `withContext` and `addContext` methods defined in this library's
1719Also provided is [ ` MonologStackLogger ` ] ( src/MonologStackLogger.php ) , which
1820decorates a ` Monolog\Logger ` and provides a working [ ` withName ` ] implementation.
1921
22+ ## Requirements
23+
24+ * PHP >= 8.3
25+ * A PSR-3 compatible logger, such as [ Monolog] .
26+
2027## Usage
2128
29+ ### Installation
30+
31+ ``` bash
32+ $ composer install timdev/stack-logger
33+ ```
34+
2235### Context Stacking
2336
2437``` php
@@ -55,13 +68,13 @@ successive calls.
5568 */
5669function complexProcessing(User $user, \TimDev\StackLogger\StackLogger $logger){
5770 $logger = $logger->withContext(['user-id' => $user->id]);
58- $logger->info(" Begin processing" );
71+ $logger->info(' Begin processing' );
5972 // => [2020-10-17 17:40:53] app.INFO: Begin processing. { "user-id": 123 }
6073
6174 foreach($user->getMemberships() as $membership){
6275 $l = $logger->withContext(['membership_id'=>$membership->id]);
63- $l->info(" Checking membership" );
64- // => [2020-10-17 17:40:53] app.INFO: Checking membership. { "user-id": 123, ' membership-id' => 1001 }
76+ $l->info(' Checking membership' );
77+ // => [2020-10-17 17:40:53] app.INFO: Checking membership. { "user-id": 123, " membership-id" => 1001 }
6578 if ($membership->isExpired()){
6679 $l->info('Membership is expired, stopping early.', ['expired-at' => $membership->expiredAt]);
6780 // => [2020-10-17 17:40:53] app.INFO: Membership is expired, stopping early. { "user-id": 123, "membership-id" => 1001, "expired-at": "2020-06-30T12:00:00Z' }
@@ -71,7 +84,7 @@ function complexProcessing(User $user, \TimDev\StackLogger\StackLogger $logger){
7184 $l->info('Done handling membership');
7285 // => [2020-10-17 17:40:53] app.INFO: Done handling membership { "user-id": 123, 'membership-id' => 1001 }
7386 }
74- $logger->info(" Finished processing user." );
87+ $logger->info(' Finished processing user.' );
7588 // => [2020-10-17 17:40:53] app.INFO: Finished processing user. { "user-id": 123 }
7689}
7790```
@@ -101,7 +114,7 @@ call, even if the underlying logger is configured to ignore the log-level.
101114
102115### NullLoggers
103116
104- All ` StackLogger ` implementations provide a static ` getNullLogger ()` method,
117+ All ` StackLogger ` implementations provide a static ` makeNullLogger ()` method,
105118which returns an instance that is configured to discard all log messages. These
106119"null loggers" can be handy in tests, or as a default logger in classes that
107120can optionally accept a real logger:
@@ -113,7 +126,7 @@ class SomeService
113126{
114127 public function __construct(?MonologStackLogger $logger = null)
115128 {
116- $this->logger = $logger ?? MonologStackLogger::getNullLogger ();
129+ $this->logger = $logger ?? MonologStackLogger::makeNullLogger ();
117130 }
118131}
119132```
@@ -122,9 +135,10 @@ class SomeService
122135
123136- [ ] Make MonologStackLogger implement Monolog's ResettableInterface?
124137- [ ] Consider how this might play with Laravel, the insanely popular PHP
125- framework that I do my best to avoid. 😜
138+ framework that I don't personally use much. PRs welcome.
126139
127140[ similar functionality ] : https://getpino.io/#/docs/child-loggers
128141[ pinojs ] : https://github.com/pinojs/pino
129142[ PSR3 LoggerInterface ] : https://www.php-fig.org/psr/psr-3/
130143[ `withName` ] : https://github.com/Seldaek/monolog/blob/a54cd1f1782f62714e4d28651224316bb5540e08/src/Monolog/Logger.php#L163-L172
144+ [ Monolog ] : https://github.com/seldaek/monolog
0 commit comments