|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RDV\Bundle\MigrationBundle\Log; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Log\AbstractLogger; |
|
8
|
|
|
use Psr\Log\LogLevel; |
|
9
|
|
|
|
|
10
|
|
|
class OutputLogger extends AbstractLogger |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var OutputInterface */ |
|
13
|
|
|
protected $output; |
|
14
|
|
|
|
|
15
|
|
|
/** @var bool */ |
|
16
|
|
|
protected $alwaysLogErrors; |
|
17
|
|
|
|
|
18
|
|
|
/** @var int|null */ |
|
19
|
|
|
protected $verbosity; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string|null */ |
|
22
|
|
|
protected $indent; |
|
23
|
|
|
|
|
24
|
|
|
/** @var bool */ |
|
25
|
|
|
protected $useTags; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor |
|
29
|
|
|
* |
|
30
|
|
|
* @param OutputInterface $output output object, e.g. cli output |
|
31
|
|
|
* @param bool $alwaysLogErrors always log messages with level more than ERROR |
|
32
|
|
|
* @param int|null $verbosity verbosity level, NULL or OutputInterface::VERBOSITY_* |
|
33
|
|
|
* @param string|null $indent ident string prefix - allow to prepend some string to all messages |
|
34
|
|
|
* @param bool $useTags use message level-based output tags |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct( |
|
37
|
|
|
OutputInterface $output, |
|
38
|
|
|
$alwaysLogErrors = true, |
|
39
|
|
|
$verbosity = null, |
|
40
|
|
|
$indent = null, |
|
41
|
|
|
$useTags = false |
|
42
|
|
|
) { |
|
43
|
|
|
$this->output = $output; |
|
44
|
|
|
$this->alwaysLogErrors = $alwaysLogErrors; |
|
45
|
|
|
$this->verbosity = $verbosity; |
|
46
|
|
|
$this->indent = $indent; |
|
47
|
|
|
$this->useTags = $useTags; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritdoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function log($level, $message, array $context = array()) |
|
54
|
|
|
{ |
|
55
|
|
|
$verbosity = $this->getVerbosity(); |
|
56
|
|
|
|
|
57
|
|
|
$isHighQuiteMode = in_array( |
|
58
|
|
|
$level, |
|
59
|
|
|
[LogLevel::EMERGENCY, LogLevel::ALERT, LogLevel::CRITICAL, LogLevel::ERROR] |
|
60
|
|
|
) |
|
61
|
|
|
&& !$this->alwaysLogErrors |
|
62
|
|
|
&& $verbosity === OutputInterface::VERBOSITY_QUIET; |
|
63
|
|
|
|
|
64
|
|
|
$isMiddleLevelQuiet = in_array( |
|
65
|
|
|
$level, |
|
66
|
|
|
[LogLevel::WARNING, LogLevel::NOTICE] |
|
67
|
|
|
) |
|
68
|
|
|
&& $verbosity < OutputInterface::VERBOSITY_NORMAL; |
|
69
|
|
|
|
|
70
|
|
|
$isInfoLevelQuiet = $level == LogLevel::INFO |
|
71
|
|
|
&& $verbosity < OutputInterface::VERBOSITY_VERBOSE; |
|
72
|
|
|
|
|
73
|
|
|
$isDebugLevelQuiet = $level == LogLevel::DEBUG |
|
74
|
|
|
&& $verbosity < OutputInterface::VERBOSITY_DEBUG; |
|
75
|
|
|
|
|
76
|
|
|
if ($isHighQuiteMode | $isMiddleLevelQuiet | $isInfoLevelQuiet | $isDebugLevelQuiet) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$this->output->writeln($this->formatMessage($level, $message)); |
|
81
|
|
|
|
|
82
|
|
|
// based on PSR-3 recommendations if an Exception object is passed in the context data, |
|
83
|
|
|
// it MUST be in the 'exception' key. |
|
84
|
|
|
if (isset($context['exception']) && $context['exception'] instanceof \Exception) { |
|
85
|
|
|
$this->output->writeln( |
|
86
|
|
|
sprintf($this->formatMessage(LogLevel::ERROR, $message), LogLevel::ERROR, (string)$context['exception']) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return verbosity level |
|
93
|
|
|
* |
|
94
|
|
|
* @return int |
|
95
|
|
|
*/ |
|
96
|
|
|
protected function getVerbosity() |
|
97
|
|
|
{ |
|
98
|
|
|
return null === $this->verbosity |
|
99
|
|
|
? $this->output->getVerbosity() |
|
100
|
|
|
: $this->verbosity; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $level |
|
105
|
|
|
* @param string $message |
|
106
|
|
|
* |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function formatMessage($level, $message) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->useTags && is_string($message)) { |
|
112
|
|
|
$message = sprintf($this->getMessageTemplate($level), $level, $message); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (!is_null($this->indent) && is_string($message)) { |
|
116
|
|
|
$message = $this->indent . $message; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $message; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $level |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function getMessageTemplate($level) |
|
129
|
|
|
{ |
|
130
|
|
|
switch ($level) { |
|
131
|
|
|
case LogLevel::EMERGENCY: |
|
132
|
|
|
case LogLevel::ALERT: |
|
133
|
|
|
case LogLevel::CRITICAL: |
|
134
|
|
|
case LogLevel::ERROR: |
|
135
|
|
|
$result = '<error>[%s]</error> %s'; |
|
136
|
|
|
break; |
|
137
|
|
|
case LogLevel::WARNING: |
|
138
|
|
|
$result = '<comment>[%s]</comment> %s'; |
|
139
|
|
|
break; |
|
140
|
|
|
default: |
|
141
|
|
|
$result = '<info>[%s]</info> %s'; |
|
142
|
|
|
break; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $result; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|