|
1
|
|
|
<?php |
|
2
|
|
|
namespace League\Tactician\Logger\Formatter; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use Psr\Log\LoggerInterface; |
|
6
|
|
|
use Psr\Log\LogLevel; |
|
7
|
|
|
use League\Tactician\Handler\CommandNameExtractor\ClassNameExtractor; |
|
8
|
|
|
use League\Tactician\Handler\CommandNameExtractor\CommandNameExtractor; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Returns log messages only dump the Command name& Exception's class name. |
|
12
|
|
|
*/ |
|
13
|
|
|
class CommandNameFormatter implements Formatter |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var CommandNameExtractor |
|
17
|
|
|
*/ |
|
18
|
|
|
private $commandNameExtractor; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $commandReceivedLevel; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $commandSucceededLevel; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
private $commandFailedLevel; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $commandReceivedLevel |
|
37
|
|
|
* @param string $commandSucceededLevel |
|
38
|
|
|
* @param string $commandFailedLevel |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( |
|
41
|
|
|
CommandNameExtractor $commandNameExtractor = null, |
|
42
|
|
|
$commandReceivedLevel = LogLevel::DEBUG, |
|
43
|
|
|
$commandSucceededLevel = LogLevel::DEBUG, |
|
44
|
|
|
$commandFailedLevel = LogLevel::ERROR |
|
45
|
|
|
) { |
|
46
|
|
|
$this->commandNameExtractor = $commandNameExtractor ?: new ClassNameExtractor(); |
|
47
|
|
|
$this->commandReceivedLevel = $commandReceivedLevel; |
|
48
|
|
|
$this->commandSucceededLevel = $commandSucceededLevel; |
|
49
|
|
|
$this->commandFailedLevel = $commandFailedLevel; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the extracted command name |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
private function getCommandName($command) |
|
58
|
|
|
{ |
|
59
|
|
|
return $this->commandNameExtractor->extract($command); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritDoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function logCommandReceived(LoggerInterface $logger, $command) |
|
66
|
|
|
{ |
|
67
|
|
|
$logger->log( |
|
68
|
|
|
$this->commandReceivedLevel, |
|
69
|
|
|
'Command received: ' . $this->getCommandName($command), |
|
70
|
|
|
[] |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function logCommandSucceeded(LoggerInterface $logger, $command, $returnValue) |
|
78
|
|
|
{ |
|
79
|
|
|
$logger->log( |
|
80
|
|
|
$this->commandSucceededLevel, |
|
81
|
|
|
'Command succeeded: ' . $this->getCommandName($command), |
|
82
|
|
|
[] |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* {@inheritDoc} |
|
88
|
|
|
*/ |
|
89
|
|
|
public function logCommandFailed(LoggerInterface $logger, $command, Exception $e) |
|
90
|
|
|
{ |
|
91
|
|
|
$logger->log( |
|
92
|
|
|
$this->commandFailedLevel, |
|
93
|
|
|
'Command failed: ' . $this->getCommandName($command), |
|
94
|
|
|
['exception' => $e] |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|