12
12
namespace Symfony \Component \HttpKernel \EventListener ;
13
13
14
14
use Psr \Log \LoggerInterface ;
15
+ use Psr \Log \LogLevel ;
15
16
use Symfony \Component \Debug \Exception \FlattenException ;
16
17
use Symfony \Component \HttpFoundation \Request ;
17
18
use Symfony \Component \HttpKernel \Log \DebugLoggerInterface ;
@@ -30,6 +31,7 @@ class ExceptionListener implements EventSubscriberInterface
30
31
{
31
32
protected $ controller ;
32
33
protected $ logger ;
34
+ public static $ httpStatusCodeLogLevel = [];
33
35
34
36
public function __construct ($ controller , LoggerInterface $ logger = null )
35
37
{
@@ -91,11 +93,19 @@ public static function getSubscribedEvents()
91
93
protected function logException (\Exception $ exception , $ message )
92
94
{
93
95
if (null !== $ this ->logger ) {
94
- if (!$ exception instanceof HttpExceptionInterface || $ exception ->getStatusCode () >= 500 ) {
95
- $ this ->logger ->critical ($ message , array ('exception ' => $ exception ));
96
- } else {
97
- $ this ->logger ->error ($ message , array ('exception ' => $ exception ));
96
+ $ logLevel = LogLevel::ERROR ;
97
+ if ($ exception instanceof HttpExceptionInterface) {
98
+ $ statusCode = $ exception ->getStatusCode ();
99
+ if (isset (static ::httpStatusCodeLogLevel[$ statusCode ])) {
100
+ $ logLevel = static ::httpStatusCodeLogLevel[$ statusCode ];
101
+ } else if ($ statusCode >= 500 ) {
102
+ $ logLevel = LogLevel::CRITICAL ;
103
+ } else if ($ statusCode >= 400 ) {
104
+ $ logLevel = LogLevel::WARNING ;
105
+ }
98
106
}
107
+
108
+ $ this ->logger ->log ($ logLevel , $ message , array ('exception ' => $ exception ));
99
109
}
100
110
}
101
111
0 commit comments