|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Symfony package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace allejo\stakx\Core; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\AbstractLogger; |
|
15
|
|
|
use Psr\Log\InvalidArgumentException; |
|
16
|
|
|
use Psr\Log\LogLevel; |
|
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PSR-3 compliant console logger. |
|
21
|
|
|
* |
|
22
|
|
|
* This class is based entirely on Symfony's ConsoleLogger interface with minor modifications. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Kévin Dunglas <[email protected]> |
|
25
|
|
|
* |
|
26
|
|
|
* @see http://www.php-fig.org/psr/psr-3/ |
|
27
|
|
|
* @see https://github.com/symfony/console/blob/master/Logger/ConsoleLogger.php |
|
28
|
|
|
*/ |
|
29
|
|
|
class StakxLogger extends AbstractLogger |
|
30
|
|
|
{ |
|
31
|
|
|
const INFO = 'info'; |
|
|
|
|
|
|
32
|
|
|
const ERROR = 'error'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var OutputInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $output; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var array |
|
41
|
|
|
*/ |
|
42
|
|
|
private $verbosityLevelMap = array( |
|
43
|
|
|
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL, |
|
44
|
|
|
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL, |
|
45
|
|
|
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL, |
|
46
|
|
|
LogLevel::ERROR => OutputInterface::VERBOSITY_NORMAL, |
|
47
|
|
|
LogLevel::WARNING => OutputInterface::VERBOSITY_NORMAL, |
|
48
|
|
|
LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE, |
|
49
|
|
|
LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE, |
|
50
|
|
|
LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG, |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private $formatLevelMap = array( |
|
57
|
|
|
LogLevel::EMERGENCY => self::ERROR, |
|
58
|
|
|
LogLevel::ALERT => self::ERROR, |
|
59
|
|
|
LogLevel::CRITICAL => self::ERROR, |
|
60
|
|
|
LogLevel::ERROR => self::ERROR, |
|
61
|
|
|
LogLevel::WARNING => self::INFO, |
|
62
|
|
|
LogLevel::NOTICE => self::INFO, |
|
63
|
|
|
LogLevel::INFO => self::INFO, |
|
64
|
|
|
LogLevel::DEBUG => self::INFO, |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* ConsoleInterface constructor. |
|
69
|
|
|
* |
|
70
|
|
|
* @param OutputInterface $output |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function __construct(OutputInterface $output) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$this->output = $output; |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Return the OutputInterface object. |
|
79
|
|
|
* |
|
80
|
|
|
* @return OutputInterface |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function getOutputInterface() |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->output; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Logs with an arbitrary level. |
|
89
|
|
|
* |
|
90
|
|
|
* @param mixed $level |
|
91
|
|
|
* @param string $message |
|
92
|
|
|
* @param array $context |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function log($level, $message, array $context = array()) |
|
95
|
|
|
{ |
|
96
|
1 |
|
if (!isset($this->verbosityLevelMap[$level])) |
|
97
|
|
|
{ |
|
98
|
|
|
throw new InvalidArgumentException(sprintf('The log level "%s" does not exist.', $level)); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
$verbosity = $this->output->getVerbosity(); |
|
102
|
|
|
|
|
103
|
1 |
|
if ($verbosity >= $this->verbosityLevelMap[$level]) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$prefix = ''; |
|
106
|
|
|
|
|
107
|
1 |
|
if ($verbosity >= OutputInterface::VERBOSITY_VERBOSE) |
|
108
|
|
|
{ |
|
109
|
|
|
$prefix = sprintf('[%s] ', date('H:i:s')); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
$this->output->writeln( |
|
113
|
1 |
|
sprintf('<%1$s>%2$s[%3$s] %4$s</%1$s>', |
|
114
|
1 |
|
$this->formatLevelMap[$level], |
|
115
|
|
|
$prefix, |
|
116
|
|
|
$level, |
|
117
|
1 |
|
$this->interpolate($message, $context) |
|
118
|
|
|
) |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
1 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Writes a message to the output and adds a newline at the end. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string|array $messages The message as an array of lines of a single string |
|
127
|
|
|
* @param int $options A bitmask of options (one of the OUTPUT or VERBOSITY constants), 0 is considered |
|
128
|
|
|
* the same as self::OUTPUT_NORMAL | self::VERBOSITY_NORMAL |
|
129
|
|
|
*/ |
|
130
|
|
|
public function writeln($messages, $options = 0) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->output->writeln($messages, $options); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Interpolates context values into the message placeholders. |
|
137
|
|
|
* |
|
138
|
|
|
* @author PHP Framework Interoperability Group |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $message |
|
141
|
|
|
* @param array $context |
|
142
|
|
|
* |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
1 |
|
private function interpolate($message, array $context) |
|
146
|
|
|
{ |
|
147
|
|
|
// build a replacement array with braces around the context keys |
|
148
|
1 |
|
$replace = array(); |
|
149
|
1 |
|
foreach ($context as $key => $val) |
|
150
|
|
|
{ |
|
151
|
|
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) |
|
152
|
|
|
{ |
|
153
|
|
|
$replace[sprintf('{%s}', $key)] = $val; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
// interpolate replacement values into the message and return |
|
158
|
1 |
|
return strtr($message, $replace); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.