1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GitWrapper\EventSubscriber; |
6
|
|
|
|
7
|
|
|
use GitWrapper\Event\AbstractGitEvent; |
8
|
|
|
use GitWrapper\Event\GitBypassEvent; |
9
|
|
|
use GitWrapper\Event\GitErrorEvent; |
10
|
|
|
use GitWrapper\Event\GitOutputEvent; |
11
|
|
|
use GitWrapper\Event\GitPrepareEvent; |
12
|
|
|
use GitWrapper\Event\GitSuccessEvent; |
13
|
|
|
use GitWrapper\Exception\GitException; |
14
|
|
|
use Psr\Log\LoggerAwareInterface; |
15
|
|
|
use Psr\Log\LoggerInterface; |
16
|
|
|
use Psr\Log\LogLevel; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
|
19
|
|
|
final class GitLoggerEventSubscriber implements EventSubscriberInterface, LoggerAwareInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Mapping of event to log level. |
23
|
|
|
* |
24
|
|
|
* @var string[] |
25
|
|
|
*/ |
26
|
|
|
private $logLevelMappings = [ |
27
|
|
|
GitPrepareEvent::class => LogLevel::INFO, |
28
|
|
|
GitOutputEvent::class => LogLevel::DEBUG, |
29
|
|
|
GitSuccessEvent::class => LogLevel::INFO, |
30
|
|
|
GitErrorEvent::class => LogLevel::ERROR, |
31
|
|
|
GitBypassEvent::class => LogLevel::INFO, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var LoggerInterface |
36
|
|
|
*/ |
37
|
|
|
private $logger; |
38
|
|
|
|
39
|
|
|
public function __construct(LoggerInterface $logger) |
40
|
|
|
{ |
41
|
|
|
$this->logger = $logger; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Required by interface |
46
|
|
|
*/ |
47
|
|
|
public function setLogger(LoggerInterface $logger): void |
48
|
|
|
{ |
49
|
|
|
$this->logger = $logger; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function setLogLevelMapping(string $eventName, string $logLevel): void |
53
|
|
|
{ |
54
|
|
|
$this->logLevelMappings[$eventName] = $logLevel; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns the log level mapping for an event. |
59
|
|
|
*/ |
60
|
|
|
public function getLogLevelMapping(string $eventName): string |
61
|
|
|
{ |
62
|
|
|
if (! isset($this->logLevelMappings[$eventName])) { |
63
|
|
|
throw new GitException(sprintf('Unknown event "%s"', $eventName)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $this->logLevelMappings[$eventName]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return int[][]|string[][] |
71
|
|
|
*/ |
72
|
|
|
public static function getSubscribedEvents(): array |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
GitPrepareEvent::class => ['onPrepare', 0], |
76
|
|
|
GitOutputEvent::class => ['handleOutput', 0], |
77
|
|
|
GitSuccessEvent::class => ['onSuccess', 0], |
78
|
|
|
GitErrorEvent::class => ['onError', 0], |
79
|
|
|
GitBypassEvent::class => ['onBypass', 0], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Adds a log message using the level defined in the mappings. |
85
|
|
|
* |
86
|
|
|
* @param mixed[] $context |
87
|
|
|
*/ |
88
|
|
|
public function log(AbstractGitEvent $gitEvent, string $message, array $context = []): void |
89
|
|
|
{ |
90
|
|
|
$method = $this->getLogLevelMapping(get_class($gitEvent)); |
91
|
|
|
$context += [ |
92
|
|
|
'command' => $gitEvent->getProcess()->getCommandLine(), |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
$this->logger->{$method}($message, $context); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function onPrepare(GitPrepareEvent $gitPrepareEvent): void |
99
|
|
|
{ |
100
|
|
|
$this->log($gitPrepareEvent, 'Git command preparing to run'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function handleOutput(GitOutputEvent $gitOutputEvent): void |
104
|
|
|
{ |
105
|
|
|
$context = ['error' => $gitOutputEvent->isError()]; |
106
|
|
|
$this->log($gitOutputEvent, $gitOutputEvent->getBuffer(), $context); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function onSuccess(GitSuccessEvent $gitSuccessEvent): void |
110
|
|
|
{ |
111
|
|
|
$this->log($gitSuccessEvent, 'Git command successfully run'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function onError(GitErrorEvent $gitErrorEvent): void |
115
|
|
|
{ |
116
|
|
|
$this->log($gitErrorEvent, 'Error running Git command'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function onBypass(GitBypassEvent $gitBypassEvent): void |
120
|
|
|
{ |
121
|
|
|
$this->log($gitBypassEvent, 'Git command bypassed'); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|