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