|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* 日志 |
|
4
|
|
|
* |
|
5
|
|
|
* @author mybsdc <[email protected]> |
|
6
|
|
|
* @date 2019/3/3 |
|
7
|
|
|
* @time 12:01 |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Luolongfei\Lib; |
|
11
|
|
|
|
|
12
|
|
|
use Monolog\Logger; |
|
13
|
|
|
use Monolog\Handler\StreamHandler; |
|
14
|
|
|
use Bramus\Monolog\Formatter\ColoredLineFormatter; |
|
15
|
|
|
|
|
16
|
|
|
class Log |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Logger |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $loggerInstance; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* 由于php不能在类外使用已实例化的对象来访问静态属性,但可以在类外访问类里的静态方法,故定义此方法实现类外访问静态属性 |
|
25
|
|
|
* |
|
26
|
|
|
* 注意,info等方法不写日志,error方法才写日志到指定目录 |
|
27
|
|
|
* |
|
28
|
|
|
* @return Logger |
|
29
|
|
|
* @throws \Exception |
|
30
|
|
|
*/ |
|
31
|
|
|
public static function logger() |
|
32
|
|
|
{ |
|
33
|
|
|
if (!self::$loggerInstance instanceof Logger) { |
|
|
|
|
|
|
34
|
|
|
$handler = new StreamHandler( |
|
35
|
|
|
config('debug') ? 'php://stdout' : sprintf('%s/logs/%s.log', ROOT_PATH, date('Y-m/d')), |
|
|
|
|
|
|
36
|
|
|
config('debug') ? Logger::DEBUG : Logger::INFO |
|
37
|
|
|
); |
|
38
|
|
|
if (config('debug')) { |
|
39
|
|
|
$handler->setFormatter(new ColoredLineFormatter(null, "[%datetime%] %channel%.%level_name%: %message%\n")); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$logger = new Logger('pusher'); |
|
43
|
|
|
$logger->pushHandler($handler); |
|
44
|
|
|
|
|
45
|
|
|
self::$loggerInstance = $logger; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return self::$loggerInstance; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param $message |
|
53
|
|
|
* @param array $context |
|
54
|
|
|
* |
|
55
|
|
|
* @return bool |
|
56
|
|
|
* @throws \Exception |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function debug($message, array $context = []) |
|
59
|
|
|
{ |
|
60
|
|
|
return self::logger()->addDebug($message, $context); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $message |
|
65
|
|
|
* @param array $context |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
* @throws \Exception |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function info($message, array $context = []) |
|
71
|
|
|
{ |
|
72
|
|
|
return self::logger()->addInfo($message, $context); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $message |
|
77
|
|
|
* @param array $context |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
* @throws \Exception |
|
81
|
|
|
*/ |
|
82
|
|
|
public static function notice($message, array $context = []) |
|
83
|
|
|
{ |
|
84
|
|
|
return self::logger()->addNotice($message, $context); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $message |
|
89
|
|
|
* @param array $context |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
* @throws \Exception |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function warning($message, array $context = []) |
|
95
|
|
|
{ |
|
96
|
|
|
return self::logger()->addWarning($message, $context); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $message |
|
101
|
|
|
* @param array $context |
|
102
|
|
|
* |
|
103
|
|
|
* @return bool |
|
104
|
|
|
* @throws \Exception |
|
105
|
|
|
*/ |
|
106
|
|
|
public static function error($message, array $context = []) |
|
107
|
|
|
{ |
|
108
|
|
|
return self::logger()->addError($message, $context); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param $message |
|
113
|
|
|
* @param array $context |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool |
|
116
|
|
|
* @throws \Exception |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function alert($message, array $context = []) |
|
119
|
|
|
{ |
|
120
|
|
|
return self::logger()->addAlert($message, $context); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param $message |
|
125
|
|
|
* @param array $context |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool |
|
128
|
|
|
* @throws \Exception |
|
129
|
|
|
*/ |
|
130
|
|
|
public static function emergency($message, array $context = []) |
|
131
|
|
|
{ |
|
132
|
|
|
return self::logger()->addEmergency($message, $context); |
|
133
|
|
|
} |
|
134
|
|
|
} |