|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Cecil. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Arnaud Ligny <[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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Cecil\Logger; |
|
15
|
|
|
|
|
16
|
|
|
use Cecil\Builder; |
|
17
|
|
|
use Psr\Log\AbstractLogger; |
|
18
|
|
|
use Psr\Log\InvalidArgumentException; |
|
19
|
|
|
use Psr\Log\LogLevel; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* PrintLogger class. |
|
23
|
|
|
* |
|
24
|
|
|
* This logger prints messages to the standard output. |
|
25
|
|
|
* It supports different log levels and can be configured to print only messages |
|
26
|
|
|
* up to a certain verbosity level. |
|
27
|
|
|
*/ |
|
28
|
|
|
class PrintLogger extends AbstractLogger |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var int */ |
|
31
|
|
|
protected $printLevelMax = null; |
|
32
|
|
|
|
|
33
|
|
|
/** @var array */ |
|
34
|
|
|
protected $verbosityLevelMap = [ |
|
35
|
|
|
LogLevel::EMERGENCY => Builder::VERBOSITY_NORMAL, |
|
36
|
|
|
LogLevel::ALERT => Builder::VERBOSITY_NORMAL, |
|
37
|
|
|
LogLevel::CRITICAL => Builder::VERBOSITY_NORMAL, |
|
38
|
|
|
LogLevel::ERROR => Builder::VERBOSITY_NORMAL, |
|
39
|
|
|
LogLevel::WARNING => Builder::VERBOSITY_NORMAL, |
|
40
|
|
|
LogLevel::NOTICE => Builder::VERBOSITY_NORMAL, |
|
41
|
|
|
LogLevel::INFO => Builder::VERBOSITY_VERBOSE, |
|
42
|
|
|
LogLevel::DEBUG => Builder::VERBOSITY_DEBUG, |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Print only the $printLevelMax. |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function __construct(?int $printLevelMax = null) |
|
49
|
|
|
{ |
|
50
|
1 |
|
$this->printLevelMax = $printLevelMax; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function log($level, string|\Stringable $message, array $context = []): void |
|
57
|
|
|
{ |
|
58
|
1 |
|
if (!isset($this->verbosityLevelMap[$level])) { |
|
59
|
|
|
throw new InvalidArgumentException(\sprintf('The log level "%s" does not exist.', $level)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
if ($this->printLevelMax !== null && $this->verbosityLevelMap[$level] > $this->printLevelMax) { |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
$level = $level != LogLevel::INFO ? "[$level] " : ''; |
|
67
|
|
|
|
|
68
|
1 |
|
if (isset($context['progress'])) { |
|
69
|
1 |
|
printf( |
|
70
|
1 |
|
"%s%s (%s/%s)\n", |
|
71
|
1 |
|
$level, |
|
72
|
1 |
|
$this->interpolate($message, $context), |
|
73
|
1 |
|
$context['progress'][0], |
|
74
|
1 |
|
$context['progress'][1] |
|
75
|
1 |
|
); |
|
76
|
|
|
|
|
77
|
1 |
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
printf("%s%s\n", $level, $this->interpolate($message, $context)); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Interpolates context values into the message placeholders. |
|
85
|
|
|
* |
|
86
|
|
|
* @author PHP Framework Interoperability Group |
|
87
|
|
|
*/ |
|
88
|
1 |
|
protected function interpolate(string $message, array $context): string |
|
89
|
|
|
{ |
|
90
|
1 |
|
if (false === strpos($message, '{')) { |
|
91
|
1 |
|
return $message; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
$replacements = []; |
|
95
|
1 |
|
foreach ($context as $key => $val) { |
|
96
|
1 |
|
if (null === $val || \is_scalar($val) || (\is_object($val) && method_exists($val, '__toString'))) { |
|
97
|
|
|
$replacements["{{$key}}"] = $val; |
|
98
|
1 |
|
} elseif ($val instanceof \DateTimeInterface) { |
|
99
|
|
|
$replacements["{{$key}}"] = $val->format(\DateTime::RFC3339); |
|
100
|
1 |
|
} elseif (\is_object($val)) { |
|
101
|
|
|
$replacements["{{$key}}"] = '[object ' . \get_class($val) . ']'; |
|
102
|
|
|
} else { |
|
103
|
1 |
|
$replacements["{{$key}}"] = '[' . \gettype($val) . ']'; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return strtr($message, $replacements); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Format expression to string. |
|
112
|
|
|
*/ |
|
113
|
|
|
public static function format($expression): string |
|
114
|
|
|
{ |
|
115
|
|
|
return str_replace(["\n", ' '], '', var_export($expression, true)); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|