-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.php
More file actions
141 lines (126 loc) · 3.99 KB
/
Logger.php
File metadata and controls
141 lines (126 loc) · 3.99 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
namespace WebmanTech\Logger;
use InvalidArgumentException;
use Monolog\Utils;
use Throwable;
use WeakMap;
use WebmanTech\CommonUtils\Log;
use WebmanTech\Logger\Helper\ConfigHelper;
class Logger
{
/**
* @var string
*/
protected static $defaultLevel = 'info';
/**
* 合并到 config/log.php 中的配置
* @return array
*/
public static function getLogChannelConfigs(): array
{
$logChannelManager = new LogChannelManager((array)ConfigHelper::get('log-channel'));
return $logChannelManager->buildLogChannelConfigs();
}
/**
* 记录在用的 Logger 实例,后续用于释放资源
* @var null|WeakMap<\Monolog\Logger, string>
*/
private static ?WeakMap $loggerInstances = null;
public static function __callStatic(string $name, array $arguments): void
{
$level = static::$defaultLevel;
$context = [];
// 从参数中提取 level 和 context(支持位置参数和命名参数混合使用)
foreach ($arguments as $key => $value) {
if (is_int($key)) {
// 位置参数
if ($key === 1) {
$level = $value;
} elseif ($key === 2) {
$context = $value;
}
} elseif (is_string($key)) {
// 命名参数
if ($key === 'level' || $key === 'type') {
$level = $value;
} elseif ($key === 'context') {
$context = $value;
}
}
}
if (self::$loggerInstances === null) {
self::$loggerInstances = new WeakMap();
}
try {
$channelLogger = Log::channel($name);
if (!isset(self::$loggerInstances[$channelLogger])) {
/** @phpstan-ignore-next-line */
self::$loggerInstances[$channelLogger] = $name;
}
} catch (Throwable $e) {
if ($e->getMessage() === 'Undefined index: ' . $name) {
if (!in_array($name, (array)ConfigHelper::get('log-channel.channels', []))) {
// 未在 channels 中配置的
throw new InvalidArgumentException('请先在 log-channel.php 配置中配置 channels');
}
// 在 channels 中配置了,但所有 handler 都关闭的情况
return;
}
throw $e;
}
$channelLogger->log($level, static::formatMessage($arguments[0]), (array)$context);
}
/**
* 重置,比如 flush message
*/
public static function reset(?string $name = null): void
{
if (self::$loggerInstances === null) {
return;
}
foreach (self::$loggerInstances as $logger => $channelName) {
if ($name) {
if ($name === $channelName) {
$logger->reset();
break;
}
continue;
}
// 不指定 name 时全部都释放一遍
$logger->reset();
}
}
/**
* 关闭,比如 关闭文件句柄 占用
*/
public static function close(?string $name = null): void
{
if (self::$loggerInstances === null) {
return;
}
foreach (self::$loggerInstances as $logger => $channelName) {
if ($name) {
if ($name === $channelName) {
$logger->close();
break;
}
continue;
}
// 不指定 name 时全部都释放一遍
$logger->close();
}
}
/**
* 格式化日志信息
* @param string|array|mixed $message
* @return string|\Stringable
*/
protected static function formatMessage($message)
{
if (is_array($message)) {
$message = Utils::jsonEncode($message);
}
/** @phpstan-ignore-next-line */
return $message;
}
}