|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\Behat3SymfonyExtension\Logger; |
|
3
|
|
|
|
|
4
|
|
|
use Monolog\Logger; |
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
6
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
|
8
|
|
|
use Symfony\Component\HttpKernel\HttpKernelInterface; |
|
9
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This class will log on : |
|
13
|
|
|
* - kernel.request event => log that a request has been handled |
|
14
|
|
|
* - kernel.exception event => log that an exception has been thrown |
|
15
|
|
|
* |
|
16
|
|
|
* It's really usefull to understand what happens behind the scene when a behat step is executed |
|
17
|
|
|
*/ |
|
18
|
|
|
class SfKernelEventLogger implements EventSubscriberInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var Logger */ |
|
21
|
|
|
private $logger; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param Logger $logger |
|
25
|
|
|
* @throws \Exception |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function __construct(Logger $logger) |
|
28
|
|
|
{ |
|
29
|
4 |
|
$this->logger = $logger; |
|
30
|
4 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public static function getSubscribedEvents() |
|
36
|
|
|
{ |
|
37
|
|
|
return array( |
|
38
|
1 |
|
KernelEvents::REQUEST => 'onKernelRequest', |
|
39
|
1 |
|
KernelEvents::EXCEPTION => 'onKernelException', |
|
40
|
1 |
|
); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param GetResponseEvent $event |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function onKernelRequest(GetResponseEvent $event) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$this->log( |
|
49
|
2 |
|
'[REQUEST_HANDLED]', |
|
50
|
|
|
array( |
|
51
|
2 |
|
'type' => ($event->getRequestType() == HttpKernelInterface::MASTER_REQUEST |
|
52
|
2 |
|
? 'Master' |
|
53
|
1 |
|
: 'Sub' |
|
54
|
2 |
|
), |
|
55
|
2 |
|
'method' => $event->getRequest()->getMethod(), |
|
56
|
2 |
|
'uri' => $event->getRequest()->getUri(), |
|
57
|
|
|
) |
|
58
|
2 |
|
); |
|
59
|
2 |
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param GetResponseForExceptionEvent $event |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function onKernelException(GetResponseForExceptionEvent $event) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$this->log( |
|
67
|
1 |
|
'[EXCEPTION_THROWN]', |
|
68
|
|
|
array( |
|
69
|
1 |
|
'message' => $event->getException()->getMessage(), |
|
70
|
1 |
|
), |
|
71
|
|
|
Logger::CRITICAL |
|
72
|
1 |
|
); |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $message |
|
77
|
|
|
* @param array $context |
|
78
|
|
|
* @param int $level |
|
79
|
|
|
*/ |
|
80
|
3 |
|
private function log($message, array $context = array(), $level = Logger::DEBUG) |
|
81
|
|
|
{ |
|
82
|
3 |
|
$this->logger->addRecord( |
|
83
|
3 |
|
$level, |
|
84
|
3 |
|
sprintf( |
|
85
|
3 |
|
'[SfKernelEventLogger] - %s', |
|
86
|
|
|
$message |
|
87
|
3 |
|
), |
|
88
|
|
|
$context |
|
89
|
3 |
|
); |
|
90
|
3 |
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|