Skip to content

Commit 91dde74

Browse files
committed
add tags
1 parent b1a6089 commit 91dde74

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,19 @@ Open config/logging.php and change the file
3030
'telegram' => [
3131
'driver' => 'monolog',
3232
'level' => 'debug',
33+
3334
'handler' => TheCoder\MonologTelegram\TelegramBotHandler::class,
34-
'formatter' => TheCoder\MonologTelegram\TelegramFormatter::class,
3535
'handler_with' => [
3636
'token' => env('LOG_TELEGRAM_BOT_TOKEN'),
3737
'chat_id' => env('LOG_TELEGRAM_CHAT_ID'),
3838
'bot_api' => env('LOG_TELEGRAM_BOT_API', 'https://api.telegram.org/bot'),
3939
'proxy' => env('LOG_TELEGRAM_BOT_PROXY', null),
4040
],
41+
42+
'formatter' => TheCoder\MonologTelegram\TelegramFormatter::class,
43+
'formatter_with' => [
44+
'tags' => env('LOG_TELEGRAM_TAGS', null),
45+
],
4146
],
4247
]
4348

src/TelegramFormatter.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,30 @@ class TelegramFormatter implements FormatterInterface
3333
*/
3434
private $separator;
3535

36+
/**
37+
* @var string
38+
*/
39+
private $tags;
40+
3641
/**
3742
* Formatter constructor
3843
*
3944
* @param bool $html Format as HTML or not
4045
* @param string $format The format of the message
4146
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format
4247
* @param string $separator Record separator used when sending batch of logs in one message
48+
* @param string $tags Tags to be added to the message
4349
*/
44-
public function __construct($html = true, $format = null, $dateFormat = null, $separator = '-')
50+
public function __construct($html = true, $format = null, $dateFormat = null, $separator = '-', $tags = '')
4551
{
4652
$this->html = $html;
4753
$this->format = $format ?: self::MESSAGE_FORMAT;
4854
$this->dateFormat = $dateFormat ?: self::DATE_FORMAT;
4955
$this->separator = $separator;
56+
if (is_null($tags)) {
57+
$tags = '';
58+
}
59+
$this->tags = explode(',', $tags);
5060
}
5161

5262
/**
@@ -104,6 +114,7 @@ private function getMessageForException($exception)
104114
. '<b>Message:</b> ' . $exception->getMessage() . PHP_EOL
105115
. '<b>Exception:</b> ' . get_class($exception) . PHP_EOL
106116
. '<b>Code:</b> ' . $code . PHP_EOL
117+
. '<b>Tags:</b> ' . $this->getTags() . PHP_EOL
107118
. '<b>File:</b> ' . $exception->getFile() . PHP_EOL
108119
. '<b>Line:</b> ' . $exception->getLine() . PHP_EOL
109120
. '<b>Url:</b> ' . urldecode($request->url()) . PHP_EOL
@@ -140,6 +151,18 @@ private function getMessageForException($exception)
140151
return $message;
141152
}
142153

154+
private function getTags()
155+
{
156+
$message = '';
157+
foreach ($this->tags as $tag) {
158+
if (!empty($tag)) {
159+
$message .= '#' . $tag . ' ';
160+
}
161+
}
162+
163+
return $message;
164+
}
165+
143166
private function getMessageForLog($record)
144167
{
145168
$message = $this->format;

0 commit comments

Comments
 (0)