Skip to content
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

prevent recursion with log events #39366

Closed
wants to merge 1 commit into from
Closed
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
10 changes: 8 additions & 2 deletions lib/private/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,21 @@ public function debug(string $message, array $context = []) {
* @return void
*/
public function log(int $level, string $message, array $context = []) {
static $inLogEvent = false;
$minLevel = $this->getLogLevel($context);

array_walk($context, [$this->normalizer, 'format']);

$app = $context['app'] ?? 'no app in context';
$entry = $this->interpolateMessage($context, $message);

if ($this->eventDispatcher) {
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry));
if ($this->eventDispatcher && !$inLogEvent) {
$inLogEvent = true;
try {
$this->eventDispatcher->dispatchTyped(new BeforeMessageLoggedEvent($app, $level, $entry));
} catch (Throwable $e) {
// ignore exceptions from inside the logger, as trying to log them would lead to recursion
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that means a syntax error in BeforeMessageLoggedEvent class would go unnoticed, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, but that seems better than breaking any logging from such an error.

(also static analysis would detect that specific error)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the inLogEvent boolean is already avoiding recursion on this call, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but an exception in the logging would still cause issues. We have similar logic in other logging places iirc

}
}

try {
Expand Down
Loading