|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Phoole (PHP7.2+) |
|
5
|
|
|
* |
|
6
|
|
|
* @category Library |
|
7
|
|
|
* @package Phoole\Logger |
|
8
|
|
|
* @copyright Copyright (c) 2019 Hong Zhang |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Phoole\Logger\Entry; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\InvalidArgumentException; |
|
15
|
|
|
use Phoole\Logger\Processor\ProcessorInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Log message |
|
19
|
|
|
* |
|
20
|
|
|
* @package Phoole\Logger |
|
21
|
|
|
*/ |
|
22
|
|
|
class LogEntry implements LogEntryInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use LogLevelTrait; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $message = 'log message'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $level; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $context; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $level |
|
|
|
|
|
|
43
|
|
|
* @param string $message |
|
44
|
|
|
* @param array $context |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(string $message = '', array $context = []) |
|
47
|
|
|
{ |
|
48
|
|
|
if (!empty($message)) { |
|
49
|
|
|
$this->message = $message; |
|
50
|
|
|
} |
|
51
|
|
|
$this->context = $context; |
|
52
|
|
|
|
|
53
|
|
|
foreach ($this->getProcessors() as $processorClass) { |
|
54
|
|
|
(new $processorClass())->process($this); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getMessage(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->message; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getLevel(): string |
|
70
|
|
|
{ |
|
71
|
|
|
return (string) $this->level; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getContext(): array |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->context; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritDoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setLevel(string $level): LogEntryInterface |
|
86
|
|
|
{ |
|
87
|
|
|
if (!isset($this->convert[$level])) { |
|
88
|
|
|
throw new InvalidArgumentException("unknown log level"); |
|
89
|
|
|
} |
|
90
|
|
|
$this->level = $level; |
|
91
|
|
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritDoc} |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setContext(array $context): LogEntryInterface |
|
98
|
|
|
{ |
|
99
|
|
|
$this->context = $context; |
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritDoc} |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getProcessors(): array |
|
107
|
|
|
{ |
|
108
|
|
|
return array(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritDoc} |
|
113
|
|
|
*/ |
|
114
|
|
|
public function __toString(): string |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->interpolate($this->getMessage(), $this->getContext()); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $message message |
|
121
|
|
|
* @param array $context |
|
122
|
|
|
* @return string result |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function interpolate(string $message, array $context): string |
|
125
|
|
|
{ |
|
126
|
|
|
$replace = array(); |
|
127
|
|
|
foreach ($context as $key => $val) { |
|
128
|
|
|
if ( |
|
129
|
|
|
!is_array($val) && |
|
130
|
|
|
(!is_object($val) || |
|
131
|
|
|
method_exists($val, '__toString')) |
|
132
|
|
|
) { |
|
133
|
|
|
$replace['{' . $key . '}'] = $val; |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
return strtr($message, $replace); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.