1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Logger |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Logger\Entry; |
16
|
|
|
|
17
|
|
|
use Phossa2\Logger\LogLevel; |
18
|
|
|
use Phossa2\Logger\Message\Message; |
19
|
|
|
use Psr\Log\LogLevel as PsrLogLevel; |
20
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
21
|
|
|
use Phossa2\Logger\Formatter\DefaultFormatter; |
22
|
|
|
use Phossa2\Logger\Exception\InvalidArgumentException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* LogEntry |
26
|
|
|
* |
27
|
|
|
* @package Phossa2\Logger |
28
|
|
|
* @author Hong Zhang <[email protected]> |
29
|
|
|
* @see ObjectAbstract |
30
|
|
|
* @see LogEntryInterface |
31
|
|
|
* @version 2.0.0 |
32
|
|
|
* @since 2.0.0 added |
33
|
|
|
*/ |
34
|
|
|
class LogEntry extends ObjectAbstract implements LogEntryInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
* @access protected |
39
|
|
|
*/ |
40
|
|
|
protected $channel; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
* @access protected |
45
|
|
|
*/ |
46
|
|
|
protected $message; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
* @access protected |
51
|
|
|
*/ |
52
|
|
|
protected $level; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array |
56
|
|
|
* @access protected |
57
|
|
|
*/ |
58
|
|
|
protected $context; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
* @access protected |
63
|
|
|
*/ |
64
|
|
|
protected $formatted; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var float |
68
|
|
|
* @access protected |
69
|
|
|
*/ |
70
|
|
|
protected $timestamp; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* is log stopped propagation ? |
74
|
|
|
* |
75
|
|
|
* @var bool |
76
|
|
|
* @access protected |
77
|
|
|
*/ |
78
|
|
|
protected $stopped = false; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create the log entry |
82
|
|
|
* |
83
|
|
|
* @param string $channel |
84
|
|
|
* @param string $level |
85
|
|
|
* @param string $message |
86
|
|
|
* @param array $context |
87
|
|
|
* @param float $timestamp |
88
|
|
|
* @throws InvalidArgumentException if level invalid |
89
|
|
|
* @access protected |
90
|
|
|
*/ |
91
|
|
|
public function __construct( |
92
|
|
|
/*# string */ $channel, |
93
|
|
|
/*# string */ $level, |
94
|
|
|
/*# string */ $message, |
95
|
|
|
array $context = [], |
96
|
|
|
/*# float */ $timestamp = 0 |
97
|
|
|
) { |
98
|
|
|
$this |
99
|
|
|
->setChannel($channel) |
100
|
|
|
->setLevel($level) |
101
|
|
|
->setMessage($message) |
102
|
|
|
->setContext($context) |
103
|
|
|
->setTimestamp($timestamp); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritDoc} |
108
|
|
|
*/ |
109
|
|
|
public function setChannel( |
110
|
|
|
/*# string */ $channel |
111
|
|
|
) { |
112
|
|
|
$this->channel = $channel; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritDoc} |
118
|
|
|
*/ |
119
|
|
|
public function getChannel()/*# : string */ |
120
|
|
|
{ |
121
|
|
|
return $this->channel; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritDoc} |
126
|
|
|
*/ |
127
|
|
|
public function setMessage( |
128
|
|
|
/*# string */ $message |
129
|
|
|
) { |
130
|
|
|
$this->message = (string) $message; |
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritDoc} |
136
|
|
|
*/ |
137
|
|
|
public function getMessage()/*# : string */ |
138
|
|
|
{ |
139
|
|
|
return $this->message; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritDoc} |
144
|
|
|
*/ |
145
|
|
|
public function setLevel( |
146
|
|
|
/*# string */ $level = PsrLogLevel::INFO |
147
|
|
|
) { |
148
|
|
View Code Duplication |
if (!isset(LogLevel::$levels[$level])) { |
149
|
|
|
throw new InvalidArgumentException( |
150
|
|
|
Message::get(Message::LOG_LEVEL_INVALID, $level), |
151
|
|
|
Message::LOG_LEVEL_INVALID |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
$this->level = $level; |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritDoc} |
160
|
|
|
*/ |
161
|
|
|
public function getLevel()/*# : string */ |
162
|
|
|
{ |
163
|
|
|
return $this->level; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* {@inheritDoc} |
168
|
|
|
*/ |
169
|
|
|
public function setFormatted(/*# string */ $formatted) |
170
|
|
|
{ |
171
|
|
|
$this->formatted = $formatted; |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* {@inheritDoc} |
177
|
|
|
*/ |
178
|
|
|
public function getFormatted()/*# string */ |
179
|
|
|
{ |
180
|
|
|
if (null === $this->formatted) { |
181
|
|
|
$formatter = new DefaultFormatter(); |
182
|
|
|
$formatter($this); |
183
|
|
|
} |
184
|
|
|
return $this->formatted; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritDoc} |
189
|
|
|
*/ |
190
|
|
|
public function setTimestamp( |
191
|
|
|
/*# : float */ $timestamp = 0 |
192
|
|
|
) { |
193
|
|
|
$this->timestamp = (float) ($timestamp ?: microtime(true)); |
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritDoc} |
199
|
|
|
*/ |
200
|
|
|
public function getTimestamp()/*# : float */ |
201
|
|
|
{ |
202
|
|
|
return $this->timestamp; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* {@inheritDoc} |
207
|
|
|
*/ |
208
|
|
|
public function setContext(array $context) |
209
|
|
|
{ |
210
|
|
|
$this->context = $context; |
211
|
|
|
return $this; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* {@inheritDoc} |
216
|
|
|
*/ |
217
|
|
|
public function getContext()/*# : array */ |
218
|
|
|
{ |
219
|
|
|
return $this->context; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* {@inheritDoc} |
224
|
|
|
*/ |
225
|
|
|
public function stopPropagation(/*# : bool */ $flag = true) |
226
|
|
|
{ |
227
|
|
|
$this->stopped = (bool) $flag; |
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* {@inheritDoc} |
233
|
|
|
*/ |
234
|
|
|
public function isPropagationStopped()/*# : bool */ |
235
|
|
|
{ |
236
|
|
|
return $this->stopped; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* {@inheritDoc} |
241
|
|
|
*/ |
242
|
|
|
public function __toString()/*# : string */ |
243
|
|
|
{ |
244
|
|
|
return $this->getFormatted(); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|