-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.php
135 lines (106 loc) · 4 KB
/
log.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
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
<?php
/**
* InfoAppl Bulletin Board Bot
* ===================
* UWiClab, University of Urbino
* ===================
* Logging library.
*/
require_once('lib.php');
// Register teardown upfront
register_shutdown_function('Logger::logger_teardown');
class Logger {
const SEVERITY_DEBUG = 1;
const SEVERITY_INFO = 64;
const SEVERITY_WARNING = 128;
const SEVERITY_ERROR = 255;
private static $max_level = self::SEVERITY_DEBUG;
private static $messages = array();
private static $last_user_id = null;
public static function debug($message, $tag = '', $context = null) {
self::common(self::SEVERITY_DEBUG, $message, $tag, $context);
}
public static function info($message, $tag = '', $context = null, $transmit = false) {
self::common(self::SEVERITY_INFO, $message, $tag, $context);
if($transmit) {
self::notification($message);
}
}
public static function warning($message, $tag = '', $context = null) {
// Forward to default error logging (system log by default)
error_log('Warning: ' . $message);
self::common(self::SEVERITY_WARNING, $message, $tag, $context);
}
public static function error($message, $tag = '', $context = null) {
// Forward to default error logging (system log by default)
error_log('Error: ' . $message);
self::common(self::SEVERITY_ERROR, $message, $tag, $context);
}
public static function fatal($message, $tag = '', $context = null) {
self::error($message, $tag, $context);
die();
}
private static function severity_to_char($level) {
switch($level) {
case self::SEVERITY_DEBUG:
default:
return 'D';
case self::SEVERITY_INFO:
return 'I';
case self::SEVERITY_WARNING:
return 'W';
case self::SEVERITY_ERROR:
return 'E';
}
}
private static function common($level, $message, $tag = '', $context = null) {
echo '(' . self::severity_to_char($level) . ') ' . $message . PHP_EOL;
/*if(DEBUG_TO_BOT || $level > self::SEVERITY_DEBUG) {
self::$max_level = max(self::$max_level, $level);
self::$messages[] = $message;
}
if($context !== null) {
$from_id = $context->get_telegram_user_id();
self::$last_user_id = $from_id;
}
else {
$from_id = 'NULL';
}*/
// Write to database, if severity high enough or debug logging enabled
/*if(DEBUG_TO_DB || $level > self::SEVERITY_DEBUG) {
db_perform_action("INSERT INTO `log` VALUES(NOW(), '" . basename(db_escape($tag), '.php') . "', '" . db_escape($message) . "', {$level}, {$from_id})");
}*/
}
/**
* Sends out pending error (and warning) messages and resets the queue.
*/
public static function notify() {
/*if(self::$messages && sizeof(self::$messages) > 0 && self::$max_level >= self::SEVERITY_WARNING) {
$report = (self::$max_level === self::SEVERITY_ERROR) ? '🚨 *Error report*' : '⚠️ *Warning report*';
foreach(self::$messages as $m) {
$report .= "\n· " . escape_markdown($m);
}
if(self::$last_group_id && self::$last_user_id) {
$report .= "\n_Group #" . self::$last_group_id . " User #" . self::$last_user_id . "_";
}
telegram_send_message(CHAT_GROUP_DEBUG, $report, array(
'parse_mode' => 'Markdown'
));
}*/
Logger::$messages = array();
}
/**
* Sends an immediate informative notification.
* Use sparingly.
*/
public static function notification($message) {
//telegram_send_message(CHAT_GROUP_DEBUG, 'ℹ️ ' . $message);
}
/**
* Helper function that is called on PHP teardown.
* Sends pending messages.
*/
public static function logger_teardown() {
Logger::notify();
}
}