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