Skip to content

Commit 4625cbe

Browse files
committed
telegram handler & logger;
1 parent e220ee6 commit 4625cbe

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "laravel-telegram-logging",
3+
"autoload": {
4+
"psr-0" : {
5+
"Logger\\" : "src"
6+
}
7+
},
8+
"require": {
9+
"php": "^7.1.3",
10+
"monolog/monolog": "^1.23"
11+
},
12+
"minimum-stability": "dev",
13+
"prefer-stable": true
14+
}

src/TelegramHandler.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace App\Logging;
4+
5+
use Exception;
6+
use Monolog\Logger;
7+
use Monolog\Handler\AbstractProcessingHandler;
8+
9+
/**
10+
* Class TelegramHandler
11+
* @package App\Logging
12+
*/
13+
class TelegramHandler extends AbstractProcessingHandler
14+
{
15+
/**
16+
* Bot API token
17+
*
18+
* @var string
19+
*/
20+
private $botToken;
21+
22+
/**
23+
* Chat id for bot
24+
*
25+
* @var int
26+
*/
27+
private $chatId;
28+
29+
/**
30+
* Application name
31+
*
32+
* @string
33+
*/
34+
private $appName;
35+
36+
/**
37+
* Application environment
38+
*
39+
* @string
40+
*/
41+
private $appEnv;
42+
43+
/**
44+
* TelegramHandler constructor.
45+
* @param int $level
46+
*/
47+
public function __construct($level = Logger::DEBUG)
48+
{
49+
parent::__construct($level, true);
50+
51+
// define variables for making Telegram request
52+
$this->botToken = env('TELEGRAM_LOGGER_BOT_TOKEN');
53+
$this->chatId = env('TELEGRAM_LOGGER_CHAT_ID');
54+
55+
// define variables for text message
56+
$this->appName = config('app.name');
57+
$this->appEnv = config('app.env');
58+
}
59+
60+
/**
61+
* @param array $record
62+
*/
63+
public function write(array $record)
64+
{
65+
if(!$this->botToken || !$this->chatId) {
66+
return;
67+
}
68+
69+
// trying to make request and send notification
70+
try {
71+
file_get_contents(
72+
'https://api.telegram.org/bot' . $this->botToken . '/sendMessage?'
73+
. http_build_query([
74+
'text' => $this->formatText($record['formatted'], $record['level_name']),
75+
'chat_id' => $this->chatId,
76+
'parse_mode' => 'html'
77+
])
78+
);
79+
} catch (Exception $exception) {
80+
81+
}
82+
}
83+
84+
/**
85+
* @param string $text
86+
* @param string $level
87+
* @return string
88+
*/
89+
private function formatText(string $text, string $level): string
90+
{
91+
return '<b>' . $this->appName . '</b> (' . $level . ')' . PHP_EOL . 'Env: ' . $this->appEnv . PHP_EOL . $text;
92+
}
93+
}

src/TelegramLogger.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Logging;
4+
5+
use Monolog\Logger;
6+
7+
/**
8+
* Class TelegramLogger
9+
* @package App\Logging
10+
*/
11+
class TelegramLogger
12+
{
13+
/**
14+
* Create a custom Monolog instance.
15+
*
16+
* @param array $config
17+
* @return \Monolog\Logger
18+
*/
19+
public function __invoke(array $config)
20+
{
21+
return new Logger(
22+
env('APP_NAME'),
23+
[
24+
new TelegramHandler()
25+
]
26+
);
27+
}
28+
}

0 commit comments

Comments
 (0)