-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.php
More file actions
277 lines (244 loc) · 8.46 KB
/
log.php
File metadata and controls
277 lines (244 loc) · 8.46 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
/* colors in terminal */
define('LDC_DEFAULT', "\033[0m");
define('LDC_DGRAYB', "\033[1;30m");
define('LDC_REDB', "\033[1;31m");
define('LDC_GREENB', "\033[1;32m");
define('LDC_YELLOWB', "\033[1;33m");
define('LDC_BLUEB', "\033[1;34m");
define('LDC_PURPLEB', "\033[1;35m");
define('LDC_CYANB', "\033[1;36m");
define('LDC_WHITEB', "\033[1;37m");
define('LDC_DGRAY', "\033[30m");
define('LDC_RED', "\033[31m");
define('LDC_GREEN', "\033[32m");
define('LDC_YELLOW', "\033[33m");
define('LDC_BLUE', "\033[34m");
define('LDC_PURPLE', "\033[35m");
define('LDC_CYAN', "\033[36m");
define('LDC_WHITE', "\033[37m");
define('LDC_BDGRAY', "\033[40m");
define('LDC_BRED', "\033[41m");
define('LDC_BGREEN', "\033[42m");
define('LDC_BYELLOW', "\033[43m");
define('LDC_BBLUE', "\033[44m");
define('LDC_BPURPLE', "\033[45m");
define('LDC_BCYAN', "\033[46m");
define('LDC_BWHITE', "\033[47m");
define('LOG_VERBOSE', LOG_DEBUG + 1);
function log_record(int $priority, string $message, array $context = [], $emit = true)
{
/* do not log debug messages if not in debug mode */
if (!cfg_debug() && $priority >= LOG_DEBUG) {
return;
}
/* translate possible references */
$message = tr($message, $context);
/* resolve default log level */
$levels = array(
LOG_EMERG => 'EMERGENCY',
LOG_ALERT => 'ALERT',
LOG_CRIT => 'CRITICAL',
LOG_ERR => 'ERROR',
LOG_WARNING => 'WARNING',
LOG_NOTICE => 'NOTICE',
LOG_INFO => 'INFO',
LOG_DEBUG => 'DEBUG',
LOG_VERBOSE => 'VERBOSE',
);
$priority_default = array_search(cfg(['log', 'level'], 'DEBUG'), $levels);
if ($priority_default === false) {
$priority_default = LOG_DEBUG;
}
$level_default = $levels[$priority_default];
/* check if this is a http request or console */
$address = 'console';
$port = 0;
if (isset($_SERVER['REMOTE_ADDR'])) {
$address = $_SERVER['REMOTE_ADDR'];
$port = $_SERVER['SERVER_PORT'];
}
$session_id = session_id();
if (empty($session_id)) {
$session_id = 'anonymous';
}
/* optional extra information (address, port, session id..) for message */
$message_extra = $address . ' ' . $port . ' ' . $session_id . ' ';
/* write to syslog, simplest one this is */
if (cfg(['log', 'syslog', 'enabled']) === true) {
$p = array_search(cfg(['log', 'syslog', 'level'], $level_default), $levels);
if ($p === false || $p >= $priority) {
$prefix = cfg(['log', 'syslog', 'prefix']);
@syslog($priority, $levels[$priority] . ' ' . $message_extra . (is_string($prefix) ? $prefix . ' ' : '') . $message);
}
}
/* write log to file */
if (cfg(['log', 'file', 'enabled']) !== false) {
$p = array_search(cfg(['log', 'file', 'level'], $level_default), $levels);
if ($p === false || $p >= $priority) {
static $f = null;
if ($f === null) {
$logfile = cfg(['log', 'file', 'file'], cfg(['path', 'log']) . '/kernel.log');
$f = @fopen($logfile, 'a');
if (!$f) {
error_log('failed to open log file: ' . $logfile);
}
}
if ($f) {
$colors = cfg(['log', 'file', 'colors'], true);
if ($colors) {
$color_by_priority = [
LOG_VERBOSE => LDC_PURPLE,
LOG_DEBUG => LDC_PURPLE,
LOG_INFO => LDC_BLUE,
LOG_NOTICE => LDC_CYAN,
LOG_WARNING => LDC_YELLOW,
LOG_ERR => LDC_RED,
LOG_CRIT => LDC_REDB,
LOG_ALERT => LDC_REDB,
LOG_EMERG => LDC_REDB,
];
@fwrite($f, date('Y-m-d H:i:s') . ' ' . $address . ' ' . $port . ' ' . $session_id . ' ' .
$color_by_priority[$priority] . $levels[$priority] . LDC_DEFAULT . ' ' . $message . "\n");
} else {
@fwrite($f, date('Y-m-d H:i:s') . ' ' . $address . ' ' . $port . ' ' . $session_id . ' ' .
$levels[$priority] . ' ' . $message . "\n");
}
}
}
}
/* echo to console if not a http request */
if ($address == 'console' && cfg(['log', 'console', 'enabled']) !== false) {
$p = array_search(cfg(['log', 'console', 'level'], $level_default), $levels);
if ($p === false || $p >= $priority) {
$colors = cfg(['log', 'console', 'colors'], true);
if ($colors) {
$color_by_priority = [
LOG_VERBOSE => LDC_PURPLE,
LOG_DEBUG => LDC_PURPLE,
LOG_INFO => LDC_BLUE,
LOG_NOTICE => LDC_CYAN,
LOG_WARNING => LDC_YELLOW,
LOG_ERR => LDC_RED,
LOG_CRIT => LDC_REDB,
LOG_ALERT => LDC_REDB,
LOG_EMERG => LDC_REDB,
];
echo $color_by_priority[$priority] . $levels[$priority] . LDC_DEFAULT . ' ' . $message . "\n";
} else {
echo $levels[$priority] . ' ' . $message . "\n";
}
}
}
/* usually always emit signal (note that $message will be the translated one here, not the original) */
if ($emit) {
emit();
}
}
function log_backtrace()
{
/* do not log debug messages if not in debug mode */
if (!cfg_debug() && $priority >= LOG_DEBUG) {
return;
}
$trace = debug_backtrace();
foreach ($trace as $index => $line) {
$context = [
'index' => $index,
'file' => isset($line['file']) ? $line['file'] : '?',
'line' => isset($line['line']) ? $line['line'] : '?',
'func' => isset($line['function']) ? (isset($line['class']) ? $line['class'] . $line['type'] : '') . $line['function'] : '?',
];
log_record(LOG_VERBOSE, 'backtrace #{index}: {file}:{line}:{func}', $context, false);
}
}
function log_verbose(string $message, array $context = [])
{
log_record(LOG_VERBOSE, $message, $context);
}
function log_debug(string $message, array $context = [])
{
log_record(LOG_DEBUG, $message, $context);
}
function log_info(string $message, array $context = [])
{
log_record(LOG_INFO, $message, $context);
}
function log_notice(string $message, array $context = [])
{
log_record(LOG_NOTICE, $message, $context);
}
function log_warning(string $message, array $context = [])
{
log_record(LOG_WARNING, $message, $context);
}
function log_error(string $message, array $context = [])
{
log_record(LOG_ERR, $message, $context);
}
function log_critical(string $message, array $context = [])
{
log_record(LOG_CRIT, $message, $context);
}
function log_alert(string $message, array $context = [])
{
log_record(LOG_ALERT, $message, $context);
}
function log_emergency(string $message, array $context = [])
{
log_record(LOG_EMERG, $message, $context);
}
function log_if_verbose($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_VERBOSE, $message, $context);
}
}
function log_if_debug($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_DEBUG, $message, $context);
}
}
function log_if_info($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_INFO, $message, $context);
}
}
function log_if_notice($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_NOTICE, $message, $context);
}
}
function log_if_warning($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_WARNING, $message, $context);
}
}
function log_if_error($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_ERR, $message, $context);
}
}
function log_if_critical($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_CRIT, $message, $context);
}
}
function log_if_alert($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_ALERT, $message, $context);
}
}
function log_if_emergency($condition, string $message, array $context = [])
{
if ($condition) {
log_record(LOG_EMERG, $message, $context);
}
}