-
Notifications
You must be signed in to change notification settings - Fork 3
/
WebProcessor.php
60 lines (48 loc) · 1.33 KB
/
WebProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace Mellivora\Logger\Processor;
use Monolog\Logger;
/**
* 用于获取当前 Web 请求信息
*/
class WebProcessor
{
protected $level;
protected $serverData = [];
protected $serverKeys = [
'HTTP_USER_AGENT',
'HTTP_HOST',
'HTTP_REFERER',
'REQUEST_URI',
'REQUEST_METHOD',
'REMOTE_ADDR',
];
protected $postData = [];
public function __construct(
$level = Logger::DEBUG,
array $serverKeys = null,
array $serverData = null,
array $postData = null
) {
$this->level = Logger::toMonologLevel($level);
$this->serverData = $serverData ?: $_SERVER;
$this->postData = $postData ?: $_POST;
if ($serverKeys !== null) {
$this->serverKeys = $serverKeys;
}
}
public function __invoke(array $record)
{
if ($record['level'] < $this->level || in_array(php_sapi_name(), ['cli', 'phpdbg'])) {
return $record;
}
foreach ($this->serverKeys as $key) {
if (array_key_exists($key, $this->serverData)) {
$record['extra'][strtolower($key)] = $this->serverData[$key];
}
}
if ($this->postData) {
$record['extra']['post'] = $this->postData;
}
return $record;
}
}