forked from Eleirbag89/TelegramBotPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TelegramErrorLogger.php
111 lines (104 loc) · 3.53 KB
/
TelegramErrorLogger.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
<?php
/**
* Telegram Error Logger Class.
*
* @author shakibonline <[email protected]>
*/
class TelegramErrorLogger
{
private static $self;
/// Log request and response parameters from/to Telegrsm API
/**
* Prints the list of parameters from/to Telegram's API endpoint
* \param $result the Telegram's response as array
* \param $content the request parameters as array.
*/
public static function log($result, $content, $use_rt = true)
{
try {
if ($result['ok'] === false) {
self::$self = new self();
$e = new \Exception();
$error = PHP_EOL;
$error .= '==========[Response]==========';
$error .= "\n";
foreach ($result as $key => $value) {
if ($value == false) {
$error .= $key.":\t\t\tFalse\n";
} else {
$error .= $key.":\t\t".$value."\n";
}
}
$array = '=========[Sent Data]==========';
$array .= "\n";
if ($use_rt == true) {
foreach ($content as $item) {
$array .= self::$self->rt($item).PHP_EOL.PHP_EOL;
}
} else {
foreach ($content as $key => $value) {
$array .= $key.":\t\t".$value."\n";
}
}
$backtrace = '============[Trace]===========';
$backtrace .= "\n";
$backtrace .= $e->getTraceAsString();
self::$self->_log_to_file($error.$array.$backtrace);
}
} catch (\Exception $e) {
echo $e->getMessage();
}
}
/// Write a string in the log file adding the current server time
/**
* Write a string in the log file TelegramErrorLogger.txt adding the current server time
* \param $error_text the text to append in the log.
*/
private function _log_to_file($error_text)
{
try {
$dir_name = 'logs';
if (!is_dir($dir_name)) {
mkdir($dir_name);
}
$fileName = $dir_name.'/'.__CLASS__.'-'.date('Y-m-d').'.txt';
$myFile = fopen($fileName, 'a+');
$date = '============[Date]============';
$date .= "\n";
$date .= '[ '.date('Y-m-d H:i:s e').' ] ';
fwrite($myFile, $date.$error_text."\n\n");
fclose($myFile);
} catch (\Exception $e) {
echo $e->getMessage();
}
}
private function rt($array, $title = null, $head = true)
{
$ref = 'ref';
$text = '';
if ($head) {
$text = "[$ref]";
$text .= "\n";
}
foreach ($array as $key => $value) {
if ($value instanceof CURLFile) {
$text .= $ref.'.'.$key.'= File'.PHP_EOL;
} elseif (is_array($value)) {
if ($title != null) {
$key = $title.'.'.$key;
}
$text .= self::rt($value, $key, false);
} else {
if (is_bool($value)) {
$value = ($value) ? 'true' : 'false';
}
if ($title != '') {
$text .= $ref.'.'.$title.'.'.$key.'= '.$value.PHP_EOL;
} else {
$text .= $ref.'.'.$key.'= '.$value.PHP_EOL;
}
}
}
return $text;
}
}