-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurrentUserProcessor.php
More file actions
42 lines (35 loc) · 970 Bytes
/
CurrentUserProcessor.php
File metadata and controls
42 lines (35 loc) · 970 Bytes
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
<?php
namespace WebmanTech\Logger\Processors;
use Monolog\LogRecord;
use Monolog\Processor\ProcessorInterface;
/**
* @deprecated 建议使用如下 Processor 替换
* @see RequestIpProcessor
* @see AuthUserIdProcessor
*/
class CurrentUserProcessor implements ProcessorInterface
{
public function __construct(protected \Closure|null $getUserId = null)
{
}
/**
* @inheritDoc
*/
public function __invoke(array|LogRecord $record): array|LogRecord
{
$extra = $record['extra'] ?? [];
if (!isset($extra['ip'])) {
$ip = '0.0.0.0';
if ($request = request()) {
$ip = $request->getRealIp();
}
$extra['ip'] = $ip;
}
if (!isset($extra['userId']) && $this->getUserId) {
$userId = call_user_func($this->getUserId);
$extra['userId'] = $userId;
}
$record['extra'] = $extra;
return $record;
}
}