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