Skip to content

[12.x] Log: Add optional keys parameter to Log::withoutContext to remove selected context from future logs #55181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Illuminate/Log/LogManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,14 @@ public function sharedContext()
/**
* Flush the log context on all currently resolved channels.
*
* @param string[]|null $keys
* @return $this
*/
public function withoutContext()
public function withoutContext(?array $keys = null)
{
foreach ($this->channels as $channel) {
if (method_exists($channel, 'withoutContext')) {
$channel->withoutContext();
$channel->withoutContext($keys);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/Illuminate/Log/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,18 @@ public function withContext(array $context = [])
}

/**
* Flush the existing context array.
* Flush the log context on all currently resolved channels.
*
* @param string[]|null $keys
* @return $this
*/
public function withoutContext()
public function withoutContext(?array $keys = null)
{
$this->context = [];
if (is_array($keys)) {
$this->context = array_diff_key($this->context, array_flip($keys));
} else {
$this->context = [];
}

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @method static \Psr\Log\LoggerInterface driver(string|null $driver = null)
* @method static \Illuminate\Log\LogManager shareContext(array $context)
* @method static array sharedContext()
* @method static \Illuminate\Log\LogManager withoutContext()
* @method static \Illuminate\Log\LogManager withoutContext(string[]|null $keys = null)
* @method static \Illuminate\Log\LogManager flushSharedContext()
* @method static string|null getDefaultDriver()
* @method static void setDefaultDriver(string $name)
Expand Down
11 changes: 11 additions & 0 deletions tests/Log/LogLoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public function testContextIsFlushed()
$writer->error('foo');
}

public function testContextKeysCanBeRemovedForSubsequentLogs()
{
$writer = new Logger($monolog = m::mock(Monolog::class));
$writer->withContext(['bar' => 'baz', 'forget' => 'me']);
$writer->withoutContext(['forget']);

$monolog->shouldReceive('error')->once()->with('foo', ['bar' => 'baz']);

$writer->error('foo');
}

public function testLoggerFiresEventsDispatcher()
{
$writer = new Logger($monolog = m::mock(Monolog::class), $events = new Dispatcher);
Expand Down
Loading