-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathlogger.php
72 lines (56 loc) · 1.92 KB
/
logger.php
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
<?php
namespace YellowTree\GeoipDetect;
/**
* This API is experimental and WILL change in future versions. Beware!
*/
class Logger {
const CATEGORY_CRON = 'cron';
const CATEGORY_UPDATE = 'update';
protected static $ignoreErrorCodes = [
'http_304',
];
protected static $ignoreErrorMessages = [
'It has not changed since the last update.'
];
public static function init() {
$translated = [];
foreach (self::$ignoreErrorMessages as $msg) {
$tr = __($msg, 'geoip-detect');
if ($tr !== $msg) {
$translated[] = $tr;
}
}
self::$ignoreErrorMessages = array_merge(self::$ignoreErrorMessages, $translated);
}
// Other errors to log: lookup, API
public static function logIfError($str, $category = '', $data = []) {
if (is_wp_error($str)) {
$code = $str->get_error_code();
if ($code && in_array($code, self::$ignoreErrorCodes)) {
return;
}
$str = $str->get_error_message();
}
if (is_string($str)) {
if (in_array($str, self::$ignoreErrorMessages)) {
return;
}
self::log($str, $category, $data);
}
}
public static function log($str, $category = '', $data = []) {
$str = sanitize_text_field($str);
// For now, only log the last error
$time = geoip_detect_format_localtime();
$str = '[' . $time . '] ' . $str;
// ToDo implement $data
update_option('geoip-detect-logger-last-error' . $category, $str);
}
public static function get_last_error_msg($category = '') {
return get_option('geoip-detect-logger-last-error' . $category);
}
public static function reset_last_error_msg($category = '') {
update_option('geoip-detect-logger-last-error' . $category, false);
}
}
Logger::init();