1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yii 2 log message object |
4
|
|
|
* |
5
|
|
|
* @see https://github.com/sergeymakinen/yii2-log-message |
6
|
|
|
* @copyright Copyright (c) 2017 Sergey Makinen (https://makinen.ru) |
7
|
|
|
* @license https://github.com/sergeymakinen/yii2-log-message/blob/master/LICENSE MIT License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace sergeymakinen\yii\logmessage; |
11
|
|
|
|
12
|
|
|
use yii\base\InvalidConfigException; |
13
|
|
|
use yii\base\Object; |
14
|
|
|
use yii\console\Request as ConsoleRequest; |
15
|
|
|
use yii\helpers\Url; |
16
|
|
|
use yii\helpers\VarDumper; |
17
|
|
|
use yii\log\Logger; |
18
|
|
|
use yii\log\Target; |
19
|
|
|
use yii\web\Request as WebRequest; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* This class wraps a log message and exposes its properties as well as the current request/user details. |
23
|
|
|
* @property string $category message category. |
24
|
|
|
* @property string|null $commandLine command line. |
25
|
|
|
* @property bool $isConsoleRequest whether the current request is a console request. |
26
|
|
|
* @property string $level message level as a string. |
27
|
|
|
* @property string $prefix messsage prefix string. |
28
|
|
|
* @property string|null $sessionId session ID. |
29
|
|
|
* @property string|null $stackTrace stack trace as a string. |
30
|
|
|
* @property string $text message text. |
31
|
|
|
* @property float $timestamp message creation timestamp. |
32
|
|
|
* @property string|null $url absolute URL. |
33
|
|
|
* @property int|string|null $userId user identity ID. |
34
|
|
|
* @property string|null $userIp user IP address. |
35
|
|
|
*/ |
36
|
|
|
class Message extends Object |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var array raw message. |
40
|
|
|
*/ |
41
|
|
|
public $message; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Target message target. |
45
|
|
|
* Must be a [[Target]] instance or `null`. |
46
|
|
|
*/ |
47
|
|
|
public $target; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var bool whether the current request is a console request. |
51
|
|
|
*/ |
52
|
|
|
private $_isConsoleRequest; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor. |
56
|
|
|
* @param array $message message. |
57
|
|
|
* @param Target|null $target message target. |
58
|
|
|
* Must be a [[Target]] instance or `null`. |
59
|
|
|
* @param array $config name-value pairs that will be used to initialize the object properties. |
60
|
|
|
*/ |
61
|
143 |
|
public function __construct(array $message, $target = null, $config = []) |
62
|
|
|
{ |
63
|
143 |
|
$this->message = $message; |
64
|
143 |
|
$this->target = $target; |
65
|
143 |
|
parent::__construct($config); |
66
|
142 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
143 |
|
public function init() |
72
|
|
|
{ |
73
|
143 |
|
parent::init(); |
74
|
143 |
|
if ($this->target !== null && !$this->target instanceof Target) { |
75
|
1 |
|
throw new InvalidConfigException('`' . get_class($this) . '::target` should be an instance of `' . Target::className() . '`.'); |
76
|
|
|
} |
77
|
142 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the message category. |
81
|
|
|
* @return string message category. |
82
|
|
|
*/ |
83
|
12 |
|
public function getCategory() |
84
|
|
|
{ |
85
|
12 |
|
return $this->message[2]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Returns the command line. |
90
|
|
|
* @return string|null command line, `null` if not available. |
91
|
|
|
*/ |
92
|
12 |
|
public function getCommandLine() |
93
|
|
|
{ |
94
|
12 |
|
if (\Yii::$app === null || !$this->getIsConsoleRequest()) { |
95
|
8 |
|
return null; |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
$params = []; |
99
|
4 |
|
if (isset($_SERVER['argv'])) { |
100
|
4 |
|
$params = $_SERVER['argv']; |
101
|
4 |
|
} |
102
|
4 |
|
return implode(' ', $params); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns whether the current request is a console request. |
107
|
|
|
* @return bool whether the current request is a console request. |
108
|
|
|
* @throws InvalidConfigException if unable to determine. |
109
|
|
|
*/ |
110
|
33 |
|
public function getIsConsoleRequest() |
111
|
|
|
{ |
112
|
33 |
|
if ($this->_isConsoleRequest === null && \Yii::$app !== null) { |
113
|
32 |
|
if (\Yii::$app->getRequest() instanceof ConsoleRequest) { |
114
|
16 |
|
$this->_isConsoleRequest = true; |
115
|
32 |
|
} elseif (\Yii::$app->getRequest() instanceof WebRequest) { |
116
|
16 |
|
$this->_isConsoleRequest = false; |
117
|
16 |
|
} |
118
|
32 |
|
} |
119
|
33 |
|
if ($this->_isConsoleRequest === null) { |
120
|
1 |
|
throw new InvalidConfigException('Unable to determine if the application is a console or web application.'); |
121
|
|
|
} |
122
|
|
|
|
123
|
32 |
|
return $this->_isConsoleRequest; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns the message level as a string. |
128
|
|
|
* @return string message level as a string. |
129
|
|
|
*/ |
130
|
12 |
|
public function getLevel() |
131
|
|
|
{ |
132
|
12 |
|
return Logger::getLevelName($this->message[1]); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns a string to be prefixed to the message. |
137
|
|
|
* @return string messsage prefix string. |
138
|
|
|
*/ |
139
|
12 |
|
public function getPrefix() |
140
|
|
|
{ |
141
|
12 |
|
if ($this->target !== null) { |
142
|
8 |
|
return $this->target->getMessagePrefix($this->message); |
143
|
|
|
} else { |
144
|
12 |
|
return ''; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Returns the session ID. |
150
|
|
|
* @return string|null session ID, `null` if not available. |
151
|
|
|
*/ |
152
|
12 |
|
public function getSessionId() |
153
|
|
|
{ |
154
|
|
|
if ( |
155
|
12 |
|
\Yii::$app !== null |
156
|
12 |
|
&& \Yii::$app->has('session', true) |
157
|
12 |
|
&& \Yii::$app->getSession() !== null |
158
|
12 |
|
&& \Yii::$app->getSession()->getIsActive() |
159
|
12 |
|
) { |
160
|
4 |
|
return \Yii::$app->getSession()->getId(); |
161
|
|
|
} else { |
162
|
8 |
|
return null; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the additional stack trace as a string. |
168
|
|
|
* @return string|null stack trace, `null` if not available. |
169
|
|
|
*/ |
170
|
13 |
|
public function getStackTrace() |
171
|
|
|
{ |
172
|
13 |
|
if (!isset($this->message[4]) || empty($this->message[4])) { |
173
|
12 |
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
$traces = array_map(function ($trace) { |
177
|
1 |
|
return "in {$trace['file']}:{$trace['line']}"; |
178
|
1 |
|
}, $this->message[4]); |
179
|
1 |
|
return implode("\n", $traces); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Returns the message text. |
184
|
|
|
* @return string message text. |
185
|
|
|
*/ |
186
|
12 |
|
public function getText() |
187
|
|
|
{ |
188
|
12 |
|
$text = $this->message[0]; |
189
|
12 |
|
if (!is_string($text)) { |
190
|
6 |
|
if ($text instanceof \Throwable || $text instanceof \Exception) { |
191
|
3 |
|
$text = (string) $text; |
192
|
3 |
|
} else { |
193
|
3 |
|
$text = VarDumper::export($text); |
194
|
|
|
} |
195
|
6 |
|
} |
196
|
12 |
|
return $text; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Returns the message creation timestamp. |
201
|
|
|
* @return float message creation timestamp. |
202
|
|
|
*/ |
203
|
12 |
|
public function getTimestamp() |
204
|
|
|
{ |
205
|
12 |
|
return $this->message[3]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns the current absolute URL. |
210
|
|
|
* @return null|string absolute URL, `null` if not available. |
211
|
|
|
*/ |
212
|
12 |
|
public function getUrl() |
213
|
|
|
{ |
214
|
12 |
|
if (\Yii::$app === null || $this->getIsConsoleRequest()) { |
215
|
8 |
|
return null; |
216
|
|
|
} |
217
|
|
|
|
218
|
4 |
|
return Url::current([], true); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Returns the user identity ID. |
223
|
|
|
* @return int|string|null user identity ID, `null` if not available. |
224
|
|
|
*/ |
225
|
12 |
|
public function getUserId() |
226
|
|
|
{ |
227
|
|
|
if ( |
228
|
12 |
|
\Yii::$app !== null |
229
|
12 |
|
&& \Yii::$app->has('user', true) |
230
|
12 |
|
&& \Yii::$app->getUser() !== null |
231
|
12 |
|
) { |
232
|
4 |
|
$user = \Yii::$app->getUser()->getIdentity(false); |
233
|
4 |
|
if ($user !== null) { |
234
|
4 |
|
return $user->getId(); |
235
|
|
|
} |
236
|
|
|
} |
237
|
8 |
|
return null; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Returns the user IP address. |
242
|
|
|
* @return string|null user IP address, `null` if not available. |
243
|
|
|
*/ |
244
|
12 |
|
public function getUserIp() |
245
|
|
|
{ |
246
|
12 |
|
if (\Yii::$app === null || $this->getIsConsoleRequest()) { |
247
|
8 |
|
return null; |
248
|
|
|
} |
249
|
|
|
|
250
|
4 |
|
return \Yii::$app->getRequest()->getUserIP(); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|