-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #48747 [HttpKernel] Allow using
#[WithLogLevel]
for setting…
… custom log level for exceptions (angelov) This PR was squashed before being merged into the 6.3 branch. Discussion ---------- [HttpKernel] Allow using `#[WithLogLevel]` for setting custom log level for exceptions | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | TODO With these changes, we're extending the functionality for setting a custom log level for a given exception by introducing a new `WithLogLevel` attribute. Example: ```php #[WithLogLevel(\Psr\Log\LogLevel::CRITICAL)] class AccountBalanceIsNegative extends \Exception { } ``` This will have a lower priority compared to the log level set using the option in the configuration files. (Meaning that if we have both an attribute declared on an exception class, and another level set for the same class in the configuration, the one from the configuration will be used.) Commits ------- 0a2563d [HttpKernel] Allow using `#[WithLogLevel]` for setting custom log level for exceptions
- Loading branch information
Showing
5 changed files
with
147 additions
and
15 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/HttpKernel/Attribute/WithLogLevel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Attribute; | ||
|
||
use Psr\Log\LogLevel; | ||
|
||
/** | ||
* @author Dejan Angelov <angelovdejan@protonmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final class WithLogLevel | ||
{ | ||
/** | ||
* @param LogLevel::* $level | ||
*/ | ||
public function __construct(public readonly string $level) | ||
{ | ||
if (!\defined('Psr\Log\LogLevel::'.strtoupper($this->level))) { | ||
throw new \InvalidArgumentException(sprintf('Invalid log level "%s".', $this->level)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/Symfony/Component/HttpKernel/Tests/Attribute/WithLogLevelTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\Attribute; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Log\LogLevel; | ||
use Symfony\Component\HttpKernel\Attribute\WithLogLevel; | ||
|
||
/** | ||
* @author Dejan Angelov <angelovdejan@protonmail.com> | ||
*/ | ||
class WithLogLevelTest extends TestCase | ||
{ | ||
public function testWithValidLogLevel() | ||
{ | ||
$logLevel = LogLevel::NOTICE; | ||
|
||
$attribute = new WithLogLevel($logLevel); | ||
|
||
$this->assertSame($logLevel, $attribute->level); | ||
} | ||
|
||
public function testWithInvalidLogLevel() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Invalid log level "invalid".'); | ||
|
||
new WithLogLevel('invalid'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters