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