@@ -5,7 +5,9 @@ Wrap your PSR-3 logger with context accumulation and callable context elements.
5
5
## Inspiration
6
6
7
7
Inspired 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.
9
11
10
12
## Approach
11
13
@@ -17,8 +19,19 @@ additional `withContext` and `addContext` methods defined in this library's
17
19
Also provided is [ ` MonologStackLogger ` ] ( src/MonologStackLogger.php ) , which
18
20
decorates a ` Monolog\Logger ` and provides a working [ ` withName ` ] implementation.
19
21
22
+ ## Requirements
23
+
24
+ * PHP >= 8.3
25
+ * A PSR-3 compatible logger, such as [ Monolog] .
26
+
20
27
## Usage
21
28
29
+ ### Installation
30
+
31
+ ``` bash
32
+ $ composer install timdev/stack-logger
33
+ ```
34
+
22
35
### Context Stacking
23
36
24
37
``` php
@@ -55,13 +68,13 @@ successive calls.
55
68
*/
56
69
function complexProcessing(User $user, \TimDev\StackLogger\StackLogger $logger){
57
70
$logger = $logger->withContext(['user-id' => $user->id]);
58
- $logger->info(" Begin processing" );
71
+ $logger->info(' Begin processing' );
59
72
// => [2020-10-17 17:40:53] app.INFO: Begin processing. { "user-id": 123 }
60
73
61
74
foreach($user->getMemberships() as $membership){
62
75
$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 }
65
78
if ($membership->isExpired()){
66
79
$l->info('Membership is expired, stopping early.', ['expired-at' => $membership->expiredAt]);
67
80
// => [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){
71
84
$l->info('Done handling membership');
72
85
// => [2020-10-17 17:40:53] app.INFO: Done handling membership { "user-id": 123, 'membership-id' => 1001 }
73
86
}
74
- $logger->info(" Finished processing user." );
87
+ $logger->info(' Finished processing user.' );
75
88
// => [2020-10-17 17:40:53] app.INFO: Finished processing user. { "user-id": 123 }
76
89
}
77
90
```
@@ -101,7 +114,7 @@ call, even if the underlying logger is configured to ignore the log-level.
101
114
102
115
### NullLoggers
103
116
104
- All ` StackLogger ` implementations provide a static ` getNullLogger ()` method,
117
+ All ` StackLogger ` implementations provide a static ` makeNullLogger ()` method,
105
118
which returns an instance that is configured to discard all log messages. These
106
119
"null loggers" can be handy in tests, or as a default logger in classes that
107
120
can optionally accept a real logger:
@@ -113,7 +126,7 @@ class SomeService
113
126
{
114
127
public function __construct(?MonologStackLogger $logger = null)
115
128
{
116
- $this->logger = $logger ?? MonologStackLogger::getNullLogger ();
129
+ $this->logger = $logger ?? MonologStackLogger::makeNullLogger ();
117
130
}
118
131
}
119
132
```
@@ -122,9 +135,10 @@ class SomeService
122
135
123
136
- [ ] Make MonologStackLogger implement Monolog's ResettableInterface?
124
137
- [ ] 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.
126
139
127
140
[ similar functionality ] : https://getpino.io/#/docs/child-loggers
128
141
[ pinojs ] : https://github.com/pinojs/pino
129
142
[ PSR3 LoggerInterface ] : https://www.php-fig.org/psr/psr-3/
130
143
[ `withName` ] : https://github.com/Seldaek/monolog/blob/a54cd1f1782f62714e4d28651224316bb5540e08/src/Monolog/Logger.php#L163-L172
144
+ [ Monolog ] : https://github.com/seldaek/monolog
0 commit comments