Skip to content

Commit 80056e7

Browse files
committed
[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 ```
1 parent 192380e commit 80056e7

File tree

7 files changed

+75
-4
lines changed

7 files changed

+75
-4
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Added support for configuring PHP error level to log levels
8+
49
5.2.0
510
-----
611

DependencyInjection/Configuration.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,14 +1070,31 @@ private function addPhpErrorsSection(ArrayNodeDefinition $rootNode)
10701070
->info('PHP errors handling configuration')
10711071
->addDefaultsIfNotSet()
10721072
->children()
1073-
->scalarNode('log')
1073+
->variableNode('log')
10741074
->info('Use the application logger instead of the PHP logger for logging PHP errors.')
1075-
->example('"true" to use the default configuration: log all errors. "false" to disable. An integer bit field of E_* constants.')
1075+
->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.')
10761076
->defaultValue($this->debug)
10771077
->treatNullLike($this->debug)
1078+
->beforeNormalization()
1079+
->ifArray()
1080+
->then(function (array $v): array {
1081+
if (!($v[0]['type'] ?? false)) {
1082+
return $v;
1083+
}
1084+
1085+
// Fix XML normalization
1086+
1087+
$ret = [];
1088+
foreach ($v as ['type' => $type, 'logLevel' => $logLevel]) {
1089+
$ret[$type] = $logLevel;
1090+
}
1091+
1092+
return $ret;
1093+
})
1094+
->end()
10781095
->validate()
1079-
->ifTrue(function ($v) { return !(\is_int($v) || \is_bool($v)); })
1080-
->thenInvalid('The "php_errors.log" parameter should be either an integer or a boolean.')
1096+
->ifTrue(function ($v) { return !(\is_int($v) || \is_bool($v) || \is_array($v)); })
1097+
->thenInvalid('The "php_errors.log" parameter should be either an integer, a boolean, or an array')
10811098
->end()
10821099
->end()
10831100
->booleanNode('throw')

Resources/config/schema/symfony-1.0.xsd

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,18 @@
313313
</xsd:complexType>
314314

315315
<xsd:complexType name="php-errors">
316+
<xsd:sequence>
317+
<xsd:element name="log" type="logLevel" minOccurs="0" maxOccurs="unbounded" />
318+
</xsd:sequence>
316319
<xsd:attribute name="log" type="xsd:string" />
317320
<xsd:attribute name="throw" type="xsd:boolean" />
318321
</xsd:complexType>
319322

323+
<xsd:complexType name="logLevel" mixed="true">
324+
<xsd:attribute name="type" type="xsd:string" use="required" />
325+
<xsd:attribute name="logLevel" type="xsd:string" />
326+
</xsd:complexType>
327+
320328
<xsd:complexType name="marking_store">
321329
<xsd:sequence>
322330
<xsd:element name="argument" type="xsd:string" minOccurs="0" maxOccurs="unbounded" />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'php_errors' => [
5+
'log' => [
6+
\E_NOTICE => \Psr\Log\LogLevel::ERROR,
7+
\E_WARNING => \Psr\Log\LogLevel::ERROR,
8+
]
9+
],
10+
]);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" ?>
2+
<container xmlns="http://symfony.com/schema/dic/services"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:framework="http://symfony.com/schema/dic/symfony"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<framework:config>
9+
<framework:php-errors>
10+
<framework:log type="8" logLevel="error" />
11+
<framework:log type="2" logLevel="error" />
12+
</framework:php-errors>
13+
</framework:config>
14+
</container>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework:
2+
php_errors:
3+
log:
4+
!php/const \E_NOTICE: !php/const Psr\Log\LogLevel::ERROR
5+
!php/const \E_WARNING: !php/const Psr\Log\LogLevel::ERROR

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,18 @@ public function testPhpErrorsWithLogLevel()
498498
$this->assertSame(8, $definition->getArgument(2));
499499
}
500500

501+
public function testPhpErrorsWithLogLevels()
502+
{
503+
$container = $this->createContainerFromFile('php_errors_log_levels');
504+
505+
$definition = $container->getDefinition('debug.debug_handlers_listener');
506+
$this->assertEquals(new Reference('monolog.logger.php', ContainerInterface::NULL_ON_INVALID_REFERENCE), $definition->getArgument(1));
507+
$this->assertSame([
508+
\E_NOTICE => \Psr\Log\LogLevel::ERROR,
509+
\E_WARNING => \Psr\Log\LogLevel::ERROR,
510+
], $definition->getArgument(2));
511+
}
512+
501513
public function testRouter()
502514
{
503515
$container = $this->createContainerFromFile('full');

0 commit comments

Comments
 (0)