8000 minor #46828 [HttpKernel] Strip exception file paths from log message… · symfony/symfony@db1d6eb · GitHub
[go: up one dir, main page]

Skip to content

Commit db1d6eb

Browse files
committed
minor #46828 [HttpKernel] Strip exception file paths from log messages (ostrolucky)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpKernel] Strip exception file paths from log messages | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | In log messages, I would like to strip full file path to a file where error occurred. Instead, I suggest to use name of the file only. There are 2 reasons: 1. Whole $exception is supplied to logger anyways, so filepath is not lost from logs. I am only removing it from log _message_, not log entry. There isn't really a need to have filepath both in log message and log context. 2. We, similarly as lot of people use symlinks for deployments. This means real path after every release changes (/`app/releases/385`/ -> `/app/releases/386/` and so on). Since `Exception::getFile()` contains real path and not symlink path (`/app/current/`), every release log messages for exact same errors change. This causes error deduplication of 3rd parties like NewRelic and other APM solutions to not trigger, which means we are getting alerts for same old errors after every release. Commits ------- 272c13e [HttpKernel] Strip exception file paths from log messages
2 parents 7597687 + 272c13e commit db1d6eb

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/Symfony/Component/HttpKernel/EventListener/ErrorListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function logKernelException(ExceptionEvent $event)
9090

9191
$e = FlattenException::createFromThrowable($throwable);
9292

93-
$this->logException($throwable, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()), $logLevel);
93+
$this->logException($throwable, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', $e->getClass(), $e->getMessage(), basename($e->getFile()), $e->getLine()), $logLevel);
9494
}
9595

9696
/**
@@ -118,7 +118,7 @@ public function onKernelException(ExceptionEvent $event)
118118
} catch (\Exception $e) {
119119
$f = FlattenException::createFromThrowable($e);
120120

121-
$this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', $f->getClass(), $f->getMessage(), $e->getFile(), $e->getLine()));
121+
$this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', $f->getClass(), $f->getMessage(), basename($e->getFile()), $e->getLine()));
122122

123123
$prev = $e;
124124
do {

src/Symfony/Component/HttpKernel/Tests/EventListener/ErrorListenerTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ public function testHandleWithLogger($event, $event2)
9696
}
9797

9898
$this->assertEquals(3, $logger->countErrors());
99-
$this->assertCount(3, $logger->getLogs('critical'));
99+
$logs = $logger->getLogs('critical');
100+
$this->assertCount(3, $logs);
101+
$this->assertStringStartsWith('Uncaught PHP Exception Exception: "foo" at ErrorListenerTest.php line', $logs[0]);
102+
$this->assertStringStartsWith('Uncaught PHP Exception Exception: "foo" at ErrorListenerTest.php line', $logs[1]);
103+
$this->assertStringStartsWith('Exception thrown when handling an exception (RuntimeException: bar at ErrorListenerTest.php line', $logs[2]);
100104
}
101105

102106
public function testHandleWithLoggerAndCustomConfiguration()

0 commit comments

Comments
 (0)
0