File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -514,13 +514,14 @@ public function sharedContext()
514514 /**
515515 * Flush the log context on all currently resolved channels.
516516 *
517+ * @param string[]|null $keys
517518 * @return $this
518519 */
519- public function withoutContext ()
520+ public function withoutContext (? array $ keys = null )
520521 {
521522 foreach ($ this ->channels as $ channel ) {
522523 if (method_exists ($ channel , 'withoutContext ' )) {
523- $ channel ->withoutContext ();
524+ $ channel ->withoutContext ($ keys );
524525 }
525526 }
526527
Original file line number Diff line number Diff line change @@ -202,13 +202,18 @@ public function withContext(array $context = [])
202202 }
203203
204204 /**
205- * Flush the existing context array .
205+ * Flush the log context on all currently resolved channels .
206206 *
207+ * @param string[]|null $keys
207208 * @return $this
208209 */
209- public function withoutContext ()
210+ public function withoutContext (? array $ keys = null )
210211 {
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+ }
212217
213218 return $ this ;
214219 }
Original file line number Diff line number Diff line change 99 * @method static \Psr\Log\LoggerInterface driver(string|null $driver = null)
1010 * @method static \Illuminate\Log\LogManager shareContext(array $context)
1111 * @method static array sharedContext()
12- * @method static \Illuminate\Log\LogManager withoutContext()
12+ * @method static \Illuminate\Log\LogManager withoutContext(string[]|null $keys = null )
1313 * @method static \Illuminate\Log\LogManager flushSharedContext()
1414 * @method static string|null getDefaultDriver()
1515 * @method static void setDefaultDriver(string $name)
Original file line number Diff line number Diff line change @@ -47,6 +47,17 @@ public function testContextIsFlushed()
4747 $ writer ->error ('foo ' );
4848 }
4949
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+
5061 public function testLoggerFiresEventsDispatcher ()
5162 {
5263 $ writer = new Logger ($ monolog = m::mock (Monolog::class), $ events = new Dispatcher );
You can’t perform that action at this time.
0 commit comments