|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the TelegramBot package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Longman\TelegramBot; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Psr\Log\NullLogger; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class TelegramLog |
|
19
|
|
|
* |
|
20
|
|
|
* @method static void emergency(string $message, array $context = []) |
|
21
|
|
|
* @method static void alert(string $message, array $context = []) |
|
22
|
|
|
* @method static void critical(string $message, array $context = []) |
|
23
|
|
|
* @method static void error(string $message, array $context = []) |
|
24
|
|
|
* @method static void warning(string $message, array $context = []) |
|
25
|
|
|
* @method static void notice(string $message, array $context = []) |
|
26
|
|
|
* @method static void info(string $message, array $context = []) |
|
27
|
|
|
* @method static void debug(string $message, array $context = []) |
|
28
|
|
|
* @method static void update(string $message, array $context = []) |
|
29
|
|
|
*/ |
|
30
|
|
|
class TelegramLog |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* Logger instance |
|
34
|
|
|
* |
|
35
|
|
|
* @var LoggerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
protected static $logger; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Logger instance for update |
|
41
|
|
|
* |
|
42
|
|
|
* @var LoggerInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
protected static $update_logger; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Temporary stream handle for debug log |
|
48
|
|
|
* |
|
49
|
|
|
* @var resource|null |
|
50
|
|
|
*/ |
|
51
|
|
|
protected static $debug_log_temp_stream_handle; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Initialise logging. |
|
55
|
|
|
* |
|
56
|
|
|
* @param LoggerInterface|null $logger |
|
57
|
|
|
* @param LoggerInterface|null $update_logger |
|
58
|
|
|
*/ |
|
59
|
4 |
|
public static function initialize(LoggerInterface $logger = null, LoggerInterface $update_logger = null) |
|
60
|
|
|
{ |
|
61
|
4 |
|
self::$logger = $logger ?: new NullLogger(); |
|
62
|
4 |
|
self::$update_logger = $update_logger ?: new NullLogger(); |
|
63
|
4 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the stream handle of the temporary debug output |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed The stream if debug is active, else false |
|
69
|
|
|
*/ |
|
70
|
|
|
public static function getDebugLogTempStream() |
|
71
|
|
|
{ |
|
72
|
|
|
if ((self::$debug_log_temp_stream_handle === null) && $temp_stream_handle = fopen('php://temp', 'wb+')) { |
|
73
|
|
|
self::$debug_log_temp_stream_handle = $temp_stream_handle; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return self::$debug_log_temp_stream_handle; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Write the temporary debug stream to log and close the stream handle |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $message Message (with placeholder) to write to the debug log |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function endDebugLogTempStream($message = '%s') |
|
85
|
|
|
{ |
|
86
|
|
|
if (is_resource(self::$debug_log_temp_stream_handle)) { |
|
87
|
|
|
rewind(self::$debug_log_temp_stream_handle); |
|
88
|
|
|
self::debug(sprintf($message, stream_get_contents(self::$debug_log_temp_stream_handle))); |
|
89
|
|
|
fclose(self::$debug_log_temp_stream_handle); |
|
90
|
|
|
self::$debug_log_temp_stream_handle = null; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Handle any logging method call. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string $name |
|
98
|
|
|
* @param array $arguments |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public static function __callStatic($name, array $arguments) |
|
101
|
|
|
{ |
|
102
|
|
|
// Get the correct logger instance. |
|
103
|
6 |
|
$logger = null; |
|
104
|
6 |
|
if (in_array($name, ['emergency', 'alert', 'critical', 'error', 'warning', 'notice', 'info', 'debug',], true)) { |
|
105
|
5 |
|
$logger = self::$logger; |
|
106
|
2 |
|
} elseif ($name === 'update') { |
|
107
|
2 |
|
$logger = self::$update_logger; |
|
108
|
2 |
|
$name = 'info'; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Clearly we have no logging enabled. |
|
112
|
6 |
|
if ($logger === null) { |
|
113
|
2 |
|
return; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// Replace any placeholders from the passed context. |
|
117
|
4 |
|
if (count($arguments) >= 2) { |
|
118
|
3 |
|
$arguments[0] = self::interpolate($arguments[0], $arguments[1]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
4 |
|
call_user_func_array([$logger, $name], $arguments); |
|
122
|
4 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Interpolates context values into the message placeholders. |
|
126
|
|
|
* |
|
127
|
|
|
* @see https://www.php-fig.org/psr/psr-3/#12-message |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $message |
|
130
|
|
|
* @param array $context |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
3 |
|
protected static function interpolate($message, array $context = []) |
|
135
|
|
|
{ |
|
136
|
|
|
// Build a replacement array with braces around the context keys. |
|
137
|
3 |
|
$replace = []; |
|
138
|
3 |
|
foreach ($context as $key => $val) { |
|
139
|
|
|
// check that the value can be casted to string |
|
140
|
3 |
|
if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) { |
|
141
|
3 |
|
$replace["{{$key}}"] = $val; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// Interpolate replacement values into the message and return. |
|
146
|
3 |
|
return strtr($message, $replace); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|