Skip to content

Commit 87e43e6

Browse files
[12.x] Log: Add optional keys parameter to Log::withoutContext to remove selected context from future logs (#55181)
* feat(log): pass context keys to withoutContext to forget * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d4776ff commit 87e43e6

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

src/Illuminate/Log/LogManager.php

+3-2
Original file line numberDiff line numberDiff 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

src/Illuminate/Log/Logger.php

+8-3
Original file line numberDiff line numberDiff 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
}

src/Illuminate/Support/Facades/Log.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
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)

tests/Log/LogLoggerTest.php

+11
Original file line numberDiff line numberDiff 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);

0 commit comments

Comments
 (0)