[FrameworkBundle] Added support for configuring PHP error level to log level

This commit allow the following configuration
```yaml
    php_errors:
        log:
            !php/const \E_DEPRECATED: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_USER_DEPRECATED: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_NOTICE: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_USER_NOTICE: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_STRICT: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_WARNING: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_USER_WARNING: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_COMPILE_WARNING: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_CORE_WARNING: !php/const Psr\Log\LogLevel::ERROR
            !php/const \E_USER_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_RECOVERABLE_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_COMPILE_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_PARSE: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
            !php/const \E_CORE_ERROR: !php/const Psr\Log\LogLevel::CRITICAL
```
This commit is contained in:
Grégoire Pineau 2020-11-06 15:54:42 +01:00
parent 751352c741
commit 45519f2e9e
7 changed files with 75 additions and 4 deletions

View File

@ -1,6 +1,11 @@
CHANGELOG CHANGELOG
========= =========
5.3.0
-----
* Added support for configuring PHP error level to log levels
5.2.0 5.2.0
----- -----

View File

@ -1070,14 +1070,31 @@ class Configuration implements ConfigurationInterface
->info('PHP errors handling configuration') ->info('PHP errors handling configuration')
->addDefaultsIfNotSet() ->addDefaultsIfNotSet()
->children() ->children()
->scalarNode('log') ->variableNode('log')
->info('Use the application logger instead of the PHP logger for logging PHP errors.') ->info('Use the application logger instead of the PHP logger for logging PHP errors.')
->example('"true" to use the default configuration: log all errors. "false" to disable. An integer bit field of E_* constants.') ->example('"true" to use the default configuration: log all errors. "false" to disable. An integer bit field of E_* constants, or an array mapping E_* constants to log levels.')
->defaultValue($this->debug) ->defaultValue($this->debug)
->treatNullLike($this->debug) ->treatNullLike($this->debug)
->beforeNormalization()
->ifArray()
->then(function (array $v): array {
if (!($v[0]['type'] ?? false)) {
return $v;
}
// Fix XML normalization
$ret = [];
foreach ($v as ['type' => $type, 'logLevel' => $logLevel]) {
$ret[$type] = $logLevel;
}
return $ret;
})
->end()
->validate() ->validate()
->ifTrue(function ($v) { return !(\is_int($v) || \is_bool($v)); }) ->ifTrue(function ($v) { return !(\is_int($v) || \is_bool($v) || \is_array($v)); })
->thenInvalid('The "php_errors.log" parameter should be either an integer or a boolean.') ->thenInvalid('The "php_errors.log" parameter should be either an integer, a boolean, or an array')
->end() ->end()
->end() ->end()
->booleanNode('throw') ->booleanNode('throw')

View File

@ -313,10 +313,18 @@
</xsd:complexType> </xsd:complexType>
<xsd:complexType name="php-errors"> <xsd:complexType name="php-errors">
<xsd:sequence>
<xsd:element name="log" type="logLevel" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="log" type="xsd:string" /> <xsd:attribute name="log" type="xsd:string" />
<xsd:attribute name="throw" type="xsd:boolean" /> <xsd:attribute name="throw" type="xsd:boolean" />
</xsd:complexType> </xsd:complexType>
<xsd:complexType name="logLevel" mixed="true">
<xsd:attribute name="type" type="xsd:string" use="required" />
<xsd:attribute name="logLevel" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="marking_store"> <xsd:complexType name="marking_store">
<xsd:sequence> <xsd:sequence>
<xsd:element name="argument" type="xsd:string" minOccurs="0" maxOccurs="unbounded" /> <xsd:element name="argument" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />

View File

@ -0,0 +1,10 @@
<?php
$container->loadFromExtension('framework', [
'php_errors' => [
'log' => [
\E_NOTICE => \Psr\Log\LogLevel::ERROR,
\E_WARNING => \Psr\Log\LogLevel::ERROR,
]
],
]);

View File

@ -0,0 +1,14 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
<framework:config>
<framework:php-errors>
<framework:log type="8" logLevel="error" />
<framework:log type="2" logLevel="error" />
</framework:php-errors>
</framework:config>
</container>

View File

@ -0,0 +1,5 @@
framework:
php_errors:
log:
!php/const \E_NOTICE: !php/const Psr\Log\LogLevel::ERROR
!php/const \E_WARNING: !php/const Psr\Log\LogLevel::ERROR

View File

@ -498,6 +498,18 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertSame(8, $definition->getArgument(2)); $this->assertSame(8, $definition->getArgument(2));
} }
public function testPhpErrorsWithLogLevels()
{
$container = $this->createContainerFromFile('php_errors_log_levels');
$definition = $container->getDefinition('debug.debug_handlers_listener');
$this->assertEquals(new Reference('monolog.logger.php', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
$this->assertSame([
\E_NOTICE => \Psr\Log\LogLevel::ERROR,
\E_WARNING => \Psr\Log\LogLevel::ERROR,
], $definition->getArgument(2));
}
public function testRouter() public function testRouter()
{ {
$container = $this->createContainerFromFile('full'); $container = $this->createContainerFromFile('full');