3
3
namespace Logger ;
4
4
5
5
use Exception ;
6
+ use Illuminate \View \View ;
6
7
use Monolog \Formatter \FormatterInterface ;
7
8
use Monolog \Formatter \LineFormatter ;
8
9
use Monolog \Logger ;
9
10
use Monolog \Handler \AbstractProcessingHandler ;
11
+ use Monolog \LogRecord ;
10
12
11
13
/**
12
14
* Class TelegramHandler
@@ -16,7 +18,7 @@ class TelegramHandler extends AbstractProcessingHandler
16
18
{
17
19
/**
18
20
* Logger config
19
- *
21
+ *
20
22
* @var array
21
23
*/
22
24
private $ config ;
@@ -60,7 +62,7 @@ public function __construct(array $config)
60
62
parent ::__construct ($ level , true );
61
63
62
64
// define variables for making Telegram request
63
- $ this ->config = $ config ;
65
+ $ this ->config = $ config ;
64
66
$ this ->botToken = $ this ->getConfigValue ('token ' );
65
67
$ this ->chatId = $ this ->getConfigValue ('chat_id ' );
66
68
@@ -72,9 +74,9 @@ public function __construct(array $config)
72
74
/**
73
75
* @param array $record
74
76
*/
75
- public function write (array $ record ): void
77
+ public function write ($ record ): void
76
78
{
77
- if (!$ this ->botToken || !$ this ->chatId ) {
79
+ if (!$ this ->botToken || !$ this ->chatId ) {
78
80
throw new \InvalidArgumentException ('Bot token or chat id is not defined for Telegram logger ' );
79
81
}
80
82
@@ -99,31 +101,40 @@ protected function getDefaultFormatter(): FormatterInterface
99
101
}
100
102
101
103
/**
102
- * @param array $record
104
+ * @param $record
103
105
* @return string
104
106
*/
105
- private function formatText (array $ record ): string
107
+ private function formatText ($ record ): string
106
108
{
107
109
if ($ template = config ('telegram-logger.template ' )) {
110
+ if ($ record instanceof LogRecord) {
111
+ return view ($ template , array_merge ($ record ->toArray (), [
112
+ 'appName ' => $ this ->appName ,
113
+ 'appEnv ' => $ this ->appEnv ,
114
+ 'formatted ' => $ record ->formatted ,
115
+ ])
116
+ )->render ();
117
+ }
118
+
108
119
return view ($ template , array_merge ($ record , [
109
120
'appName ' => $ this ->appName ,
110
121
'appEnv ' => $ this ->appEnv ,
111
122
])
112
- );
123
+ )-> render () ;
113
124
}
114
125
115
126
return sprintf ("<b>%s</b> (%s) \n%s " , $ this ->appName , $ record ['level_name ' ], $ record ['formatted ' ]);
116
127
}
117
128
118
129
/**
119
- * @param string $text
130
+ * @param string $text
120
131
*/
121
132
private function sendMessage (string $ text ): void
122
133
{
123
134
$ httpQuery = http_build_query (array_merge (
124
135
[
125
- 'text ' => $ text ,
126
- 'chat_id ' => $ this ->chatId ,
136
+ 'text ' => $ text ,
137
+ 'chat_id ' => $ this ->chatId ,
127
138
'parse_mode ' => 'html ' ,
128
139
],
129
140
config ('telegram-logger.options ' , [])
@@ -137,7 +148,7 @@ private function sendMessage(string $text): void
137
148
$ context = stream_context_create ([
138
149
'http ' => [
139
150
'proxy ' => $ proxy ,
140
- ]
151
+ ],
141
152
]);
142
153
file_get_contents ($ url , false , $ context );
143
154
} else {
@@ -155,7 +166,7 @@ private function getConfigValue($key, $defaultConfigKey = null): ?string
155
166
if (isset ($ this ->config [$ key ])) {
156
167
return $ this ->config [$ key ];
157
168
}
158
-
169
+
159
170
return config ($ defaultConfigKey ?: "telegram-logger. $ key " );
160
171
}
161
172
}
0 commit comments