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
|
<?php
class Logger {
const DEBUG = 0;
const INFO = 1;
const WARNING = 2;
const ERROR = 3;
const FATAL = 4;
protected static array $levelColors = [
self::INFO => 34,
self::WARNING => 33,
self::ERROR => 31,
self::FATAL => 91,
];
protected static array $levelEmojis = [
self::INFO => 'ℹ️',
self::WARNING => '⚠️',
self::ERROR => '‼️',
self::FATAL => '⚡️'
];
protected string $domain;
public function __construct(string $domain) {
$this->domain = $domain;
}
protected function stderr(string $message, $color = null) {
$fmt = "[%s] %s";
if (is_int($color))
$fmt = "\033[{$color}m$fmt\033[0m";
$fmt .= "\n";
$message = strip_tags($message);
fprintf(STDERR, $fmt, $this->domain, $message);
}
protected function telegram(string $message) {
global $config;
$url = 'https://api.telegram.org/bot'.$config['telegram_token'].'/sendMessage';
$query_content = http_build_query([
'chat_id' => $config['telegram_chat_id'],
'text' => $message,
'parse_mode' => 'html'
]);
$ctx = stream_context_create([
'http' => [
'header' => [
'Content-type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($query_content)
],
'method' => 'POST',
'content' => $query_content
]
]);
$fp = @fopen($url, 'r', false, $ctx);
if ($fp === false) {
$this->stderr("fopen failed");
return;
}
$result = stream_get_contents($fp);
fclose($fp);
$result = json_decode($result, true);
if (!$result['ok'])
$this->stderr("telegram did not OK");
}
protected function report(int $level, string $message) {
global $config;
if ($config['verbose'])
$this->stderr($message, self::$levelColors[$level] ?? null);
if ($level != self::DEBUG && ($config['telegram_enabled'] ?? 1) == 1)
$this->telegram(self::$levelEmojis[$level].' '.$this->domain.': '.$message);
}
public function debug(string $message) {
$this->report(self::DEBUG, $message);
}
public function info(string $message) {
$this->report(self::INFO, $message);
}
public function warn(string $message) {
$this->report(self::WARNING, $message);
}
public function error(string $message) {
$this->report(self::ERROR, $message);
}
public function fatal(string $message) {
$this->report(self::FATAL, $message);
}
}
|