|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Drush Cerbere command line tools. |
|
5
|
|
|
* Copyright (C) 2015 - Sebastien Malot <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU General Public License as published by |
|
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU General Public License along |
|
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
|
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace Cerbere\Event; |
|
23
|
|
|
|
|
24
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
25
|
|
|
use Psr\Log\LoggerInterface; |
|
26
|
|
|
use Psr\Log\LogLevel; |
|
27
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class CerbereLoggerListener |
|
31
|
|
|
* @package Cerbere\Event |
|
32
|
|
|
*/ |
|
33
|
|
|
class CerbereLoggerListener implements EventSubscriberInterface, LoggerAwareInterface |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var LoggerInterface |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $logger; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Mapping of event to log level. |
|
42
|
|
|
* |
|
43
|
|
|
* @var array |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $logLevelMappings = array( |
|
46
|
|
|
CerbereEvents::CERBERE_FILE_DISCOVERED => LogLevel::INFO, |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param LoggerInterface $logger |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(LoggerInterface $logger) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->setLogger($logger); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Returns the log level mapping for an event. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $eventName |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
* |
|
64
|
|
|
* @throws \DomainException |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getLogLevelMapping($eventName) |
|
67
|
|
|
{ |
|
68
|
|
|
if (!isset($this->logLevelMappings[$eventName])) { |
|
69
|
|
|
throw new \DomainException('Unknown event: ' . $eventName); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->logLevelMappings[$eventName]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return LoggerInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getLogger() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->logger; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Sets a logger instance on the object |
|
85
|
|
|
* |
|
86
|
|
|
* @param LoggerInterface $logger |
|
87
|
|
|
* |
|
88
|
|
|
* @return null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function setLogger(LoggerInterface $logger) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->logger = $logger; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Returns an array of event names this subscriber wants to listen to. |
|
97
|
|
|
* |
|
98
|
|
|
* The array keys are event names and the value can be: |
|
99
|
|
|
* |
|
100
|
|
|
* * The method name to call (priority defaults to 0) |
|
101
|
|
|
* * An array composed of the method name to call and the priority |
|
102
|
|
|
* * An array of arrays composed of the method names to call and respective |
|
103
|
|
|
* priorities, or 0 if unset |
|
104
|
|
|
* |
|
105
|
|
|
* For instance: |
|
106
|
|
|
* |
|
107
|
|
|
* * array('eventName' => 'methodName') |
|
108
|
|
|
* * array('eventName' => array('methodName', $priority)) |
|
109
|
|
|
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')) |
|
110
|
|
|
* |
|
111
|
|
|
* @return array The event names to listen to |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function getSubscribedEvents() |
|
114
|
|
|
{ |
|
115
|
|
|
return array( |
|
116
|
|
|
CerbereEvents::CERBERE_FILE_DISCOVERED => array('onFileDiscover', 0), |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param CerbereFileDiscoverEvent $event |
|
122
|
|
|
*/ |
|
123
|
|
|
public function onFileDiscover(CerbereFileDiscoverEvent $event) |
|
124
|
|
|
{ |
|
125
|
|
|
$context = array( |
|
126
|
|
|
'parser' => $event->getParser()->getCode(), |
|
127
|
|
|
'filename' => $event->getFilename(), |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
$this->log('debug', 'New file discovered', $context); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $method |
|
135
|
|
|
* @param string $message |
|
136
|
|
|
* @param array $context |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function log($method, $message, array $context = array()) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->logger->$method($message, $context); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Sets the log level mapping for an event. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $eventName |
|
147
|
|
|
* @param string|false $logLevel |
|
148
|
|
|
* |
|
149
|
|
|
* @return $this |
|
150
|
|
|
*/ |
|
151
|
|
|
public function setLogLevelMapping($eventName, $logLevel) |
|
152
|
|
|
{ |
|
153
|
|
|
$this->logLevelMappings[$eventName] = $logLevel; |
|
154
|
|
|
|
|
155
|
|
|
return $this; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|