1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Source\Logger; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Logger class |
7
|
|
|
*/ |
8
|
|
|
abstract class Log implements LoggerInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @return void |
12
|
|
|
*/ |
13
|
|
|
final public static function init(): void |
14
|
|
|
{ |
15
|
|
|
openlog('Redstore-Web', LOG_PERROR, LOG_USER); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* System is unusable. |
20
|
|
|
* |
21
|
|
|
* @param string $message |
22
|
|
|
* @param array $context |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
final public static function emergency($message, array $context = []): void |
26
|
|
|
{ |
27
|
|
|
syslog(LOG_EMERG, self::interpolate($message, $context)); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Action must be taken immediately. |
32
|
|
|
* |
33
|
|
|
* Example: Entire website down, database unavailable, etc. This should |
34
|
|
|
* trigger the SMS alerts and wake you up. |
35
|
|
|
* |
36
|
|
|
* @param string $message |
37
|
|
|
* @param array $context |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
|
|
final public static function alert($message, array $context = []): void |
41
|
|
|
{ |
42
|
|
|
syslog(LOG_ALERT, self::interpolate($message, $context)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Critical conditions. |
47
|
|
|
* |
48
|
|
|
* Example: Application component unavailable, unexpected exception. |
49
|
|
|
* |
50
|
|
|
* @param string $message |
51
|
|
|
* @param array $context |
52
|
|
|
* @return void |
53
|
|
|
*/ |
54
|
|
|
final public static function critical($message, array $context = []): void |
55
|
|
|
{ |
56
|
|
|
syslog(LOG_CRIT, self::interpolate($message, $context)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Runtime errors that do not require immediate action but should typically |
61
|
|
|
* be logged and monitored. |
62
|
|
|
* |
63
|
|
|
* @param string $message |
64
|
|
|
* @param array $context |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
final public static function error($message, array $context = []): void |
68
|
|
|
{ |
69
|
|
|
syslog(LOG_ERR, self::interpolate($message, $context)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Exceptional occurrences that are not errors. |
74
|
|
|
* |
75
|
|
|
* Example: Use of deprecated APIs, poor use of an API, undesirable things |
76
|
|
|
* that are not necessarily wrong. |
77
|
|
|
* |
78
|
|
|
* @param string $message |
79
|
|
|
* @param array $context |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
|
|
final public static function warning($message, array $context = []): void |
83
|
|
|
{ |
84
|
|
|
syslog(LOG_WARNING, self::interpolate($message, $context)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Normal but significant events. |
89
|
|
|
* |
90
|
|
|
* @param string $message |
91
|
|
|
* @param array $context |
92
|
|
|
* @return void |
93
|
|
|
*/ |
94
|
|
|
final public static function notice($message, array $context = []): void |
95
|
|
|
{ |
96
|
|
|
syslog(LOG_NOTICE, self::interpolate($message, $context)); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Interesting events. |
101
|
|
|
* |
102
|
|
|
* Example: User logs in, SQL logs. |
103
|
|
|
* |
104
|
|
|
* @param string $message |
105
|
|
|
* @param array $context |
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
final public static function info($message, array $context = []): void |
109
|
|
|
{ |
110
|
|
|
syslog(LOG_INFO, self::interpolate($message, $context)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Detailed debug information. |
115
|
|
|
* |
116
|
|
|
* @param string $message |
117
|
|
|
* @param array $context |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
final public static function debug($message, array $context = []): void |
121
|
|
|
{ |
122
|
|
|
syslog(LOG_DEBUG, self::interpolate($message, $context)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Logs with an arbitrary level. |
127
|
|
|
* |
128
|
|
|
* @param mixed $level |
129
|
|
|
* @param string $message |
130
|
|
|
* @param array $context |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
final public static function log(int $level, $message, array $context = []): void |
134
|
|
|
{ |
135
|
|
|
if ( |
136
|
|
|
!in_array($level, [ |
137
|
|
|
LOG_EMERG, |
138
|
|
|
LOG_ALERT, |
139
|
|
|
LOG_CRIT, |
140
|
|
|
LOG_ERR, |
141
|
|
|
LOG_WARNING, |
142
|
|
|
LOG_NOTICE, |
143
|
|
|
LOG_INFO, |
144
|
|
|
LOG_DEBUG |
145
|
|
|
]) |
146
|
|
|
) { |
147
|
|
|
$level = LOG_NOTICE; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
syslog($level, self::interpolate($message, $context)); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Interpolates context values into the message placeholders. |
155
|
|
|
*/ |
156
|
|
|
final private static function interpolate($message, array $context = []): string |
157
|
|
|
{ |
158
|
|
|
// build a replacement array with braces around the context keys |
159
|
|
|
$replace = []; |
160
|
|
|
|
161
|
|
|
// check that the value can be cast to string |
162
|
|
|
foreach ($context as $key => $val) { |
163
|
|
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
164
|
|
|
$replace['{' . $key . '}'] = $val; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
// interpolate replacement values into the message and return |
169
|
|
|
return strtr($message, $replace); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|