3
3
namespace Logger ;
4
4
5
5
use Exception ;
6
+ use Monolog \Formatter \FormatterInterface ;
7
+ use Monolog \Formatter \LineFormatter ;
6
8
use Monolog \Logger ;
7
9
use Monolog \Handler \AbstractProcessingHandler ;
8
10
@@ -40,6 +42,13 @@ class TelegramHandler extends AbstractProcessingHandler
40
42
*/
41
43
private $ appEnv ;
42
44
45
+ /**
46
+ * Blade template reference to be used by Logs
47
+ *
48
+ * @string
49
+ */
50
+ private $ template ;
51
+
43
52
/**
44
53
* TelegramHandler constructor.
45
54
* @param int $level
@@ -53,6 +62,7 @@ public function __construct($level)
53
62
// define variables for making Telegram request
54
63
$ this ->botToken = config ('telegram-logger.token ' );
55
64
$ this ->chatId = config ('telegram-logger.chat_id ' );
65
+ $ this ->template = config ('telegram-logger.template ' );
56
66
57
67
// define variables for text message
58
68
$ this ->appName = config ('app.name ' );
@@ -73,7 +83,7 @@ public function write(array $record): void
73
83
file_get_contents (
74
84
'https://api.telegram.org/bot ' . $ this ->botToken . '/sendMessage? '
75
85
. http_build_query ([
76
- 'text ' => $ this ->formatText ($ record[ ' formatted ' ], $ record [ ' level_name ' ] ),
86
+ 'text ' => $ this ->formatText ($ record ),
77
87
'chat_id ' => $ this ->chatId ,
78
88
'parse_mode ' => 'html '
79
89
])
@@ -84,12 +94,24 @@ public function write(array $record): void
84
94
}
85
95
86
96
/**
87
- * @param string $text
88
- * @param string $level
97
+ * {@inheritDoc}
98
+ */
99
+ protected function getDefaultFormatter (): FormatterInterface
100
+ {
101
+ return new LineFormatter ("%message% %context% %extra% \n" );
102
+ }
103
+
104
+ /**
105
+ * @param array $record
89
106
* @return string
90
107
*/
91
- private function formatText (string $ text , string $ level ): string
108
+ private function formatText (array $ record ): string
92
109
{
93
- return '<b> ' . $ this ->appName . '</b> ( ' . $ level . ') ' . PHP_EOL . 'Env: ' . $ this ->appEnv . PHP_EOL . $ text ;
110
+
111
+ return view ($ this ->template , array_merge ($ record ,[
112
+ 'appName ' => $ this ->appName ,
113
+ 'appEnv ' => $ this ->appEnv ,
114
+ ])
115
+ );
94
116
}
95
117
}
0 commit comments