File tree 4 files changed +23
-6
lines changed
4 files changed +23
-6
lines changed Original file line number Diff line number Diff line change @@ -514,13 +514,14 @@ public function sharedContext()
514
514
/**
515
515
* Flush the log context on all currently resolved channels.
516
516
*
517
+ * @param string[]|null $keys
517
518
* @return $this
518
519
*/
519
- public function withoutContext ()
520
+ public function withoutContext (? array $ keys = null )
520
521
{
521
522
foreach ($ this ->channels as $ channel ) {
522
523
if (method_exists ($ channel , 'withoutContext ' )) {
523
- $ channel ->withoutContext ();
524
+ $ channel ->withoutContext ($ keys );
524
525
}
525
526
}
526
527
Original file line number Diff line number Diff line change @@ -202,13 +202,18 @@ public function withContext(array $context = [])
202
202
}
203
203
204
204
/**
205
- * Flush the existing context array .
205
+ * Flush the log context on all currently resolved channels .
206
206
*
207
+ * @param string[]|null $keys
207
208
* @return $this
208
209
*/
209
- public function withoutContext ()
210
+ public function withoutContext (? array $ keys = null )
210
211
{
211
- $ this ->context = [];
212
+ if (is_array ($ keys )) {
213
+ $ this ->context = array_diff_key ($ this ->context , array_flip ($ keys ));
214
+ } else {
215
+ $ this ->context = [];
216
+ }
212
217
213
218
return $ this ;
214
219
}
Original file line number Diff line number Diff line change 9
9
* @method static \Psr\Log\LoggerInterface driver(string|null $driver = null)
10
10
* @method static \Illuminate\Log\LogManager shareContext(array $context)
11
11
* @method static array sharedContext()
12
- * @method static \Illuminate\Log\LogManager withoutContext()
12
+ * @method static \Illuminate\Log\LogManager withoutContext(string[]|null $keys = null )
13
13
* @method static \Illuminate\Log\LogManager flushSharedContext()
14
14
* @method static string|null getDefaultDriver()
15
15
* @method static void setDefaultDriver(string $name)
Original file line number Diff line number Diff line change @@ -47,6 +47,17 @@ public function testContextIsFlushed()
47
47
$ writer ->error ('foo ' );
48
48
}
49
49
50
+ public function testContextKeysCanBeRemovedForSubsequentLogs ()
51
+ {
52
+ $ writer = new Logger ($ monolog = m::mock (Monolog::class));
53
+ $ writer ->withContext (['bar ' => 'baz ' , 'forget ' => 'me ' ]);
54
+ $ writer ->withoutContext (['forget ' ]);
55
+
56
+ $ monolog ->shouldReceive ('error ' )->once ()->with ('foo ' , ['bar ' => 'baz ' ]);
57
+
58
+ $ writer ->error ('foo ' );
59
+ }
60
+
50
61
public function testLoggerFiresEventsDispatcher ()
51
62
{
52
63
$ writer = new Logger ($ monolog = m::mock (Monolog::class), $ events = new Dispatcher );
You can’t perform that action at this time.
0 commit comments