|
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 float |
|
62
|
|
|
* @access protected |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $timestamp; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* is log stopped propagation ? |
|
68
|
|
|
* |
|
69
|
|
|
* @var bool |
|
70
|
|
|
* @access protected |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $stopped = false; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create the log entry |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $channel |
|
78
|
|
|
* @param string $level |
|
79
|
|
|
* @param string $message |
|
80
|
|
|
* @param array $context |
|
81
|
|
|
* @param float $timestamp |
|
82
|
|
|
* @throws InvalidArgumentException if level invalid |
|
83
|
|
|
* @access protected |
|
84
|
|
|
*/ |
|
85
|
|
|
public function __construct( |
|
86
|
|
|
/*# string */ $channel, |
|
87
|
|
|
/*# string */ $level, |
|
88
|
|
|
/*# string */ $message, |
|
89
|
|
|
array $context = [], |
|
90
|
|
|
/*# float */ $timestamp = 0 |
|
91
|
|
|
) { |
|
92
|
|
|
$this |
|
93
|
|
|
->setChannel($channel) |
|
94
|
|
|
->setLevel($level) |
|
95
|
|
|
->setMessage($message) |
|
96
|
|
|
->setContext($context) |
|
97
|
|
|
->setTimestamp($timestamp); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritDoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setChannel( |
|
104
|
|
|
/*# string */ $channel |
|
105
|
|
|
) { |
|
106
|
|
|
$this->channel = $channel; |
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritDoc} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getChannel()/*# : string */ |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->channel; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* {@inheritDoc} |
|
120
|
|
|
*/ |
|
121
|
|
|
public function setMessage( |
|
122
|
|
|
/*# string */ $message |
|
123
|
|
|
) { |
|
124
|
|
|
$this->message = (string) $message; |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* {@inheritDoc} |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getMessage()/*# : string */ |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->message; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* {@inheritDoc} |
|
138
|
|
|
*/ |
|
139
|
|
|
public function setLevel( |
|
140
|
|
|
/*# string */ $level = PsrLogLevel::INFO |
|
141
|
|
|
) { |
|
142
|
|
|
if (!isset(LogLevel::$levels[$level])) { |
|
143
|
|
|
throw new InvalidArgumentException( |
|
144
|
|
|
Message::get(Message::LOG_LEVEL_INVALID, $level), |
|
145
|
|
|
Message::LOG_LEVEL_INVALID |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
$this->level = $level; |
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* {@inheritDoc} |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getLevel()/*# : string */ |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->level; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* {@inheritDoc} |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setTimestamp( |
|
164
|
|
|
/*# : float */ $timestamp = 0 |
|
165
|
|
|
) { |
|
166
|
|
|
$this->timestamp = (float) ($timestamp ?: microtime(true)); |
|
167
|
|
|
return $this; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* {@inheritDoc} |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getTimestamp()/*# : float */ |
|
174
|
|
|
{ |
|
175
|
|
|
return $this->timestamp; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* {@inheritDoc} |
|
180
|
|
|
*/ |
|
181
|
|
|
public function setContext(array $context) |
|
182
|
|
|
{ |
|
183
|
|
|
$this->context = $context; |
|
184
|
|
|
return $this; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* {@inheritDoc} |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getContext()/*# : array */ |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->context; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
|
|
* {@inheritDoc} |
|
197
|
|
|
*/ |
|
198
|
|
|
public function stopPropagation(/*# : bool */ $flag = true) |
|
199
|
|
|
{ |
|
200
|
|
|
$this->stopped = (bool) $flag; |
|
201
|
|
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* {@inheritDoc} |
|
206
|
|
|
*/ |
|
207
|
|
|
public function isPropagationStopped()/*# : bool */ |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->stopped; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Use the default formatter |
|
214
|
|
|
* |
|
215
|
|
|
* {@inheritDoc} |
|
216
|
|
|
*/ |
|
217
|
|
|
public function __toString()/*# : string */ |
|
218
|
|
|
{ |
|
219
|
|
|
$formatter = new DefaultFormatter(); |
|
220
|
|
|
return $formatter($this); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|