Skip to content

Commit 2c65db0

Browse files
committed
Optimize error message
1 parent c24ba14 commit 2c65db0

File tree

3 files changed

+32
-8
lines changed

3 files changed

+32
-8
lines changed

src/Contracts/LoggerData.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,28 @@
66
interface LoggerData
77
{
88
/**
9-
* Get request URL.
9+
* Get the request URL.
1010
*
1111
* @return string
1212
*/
1313
public function requestUrl(): ?string;
1414

1515
/**
16-
* Get request method.
16+
* Get the request method.
1717
*
1818
* @return string|null
1919
*/
2020
public function requestMethod(): ?string;
2121

2222
/**
23-
* Get ip address.
23+
* Check the error is raised on command.
24+
*
25+
* @return bool
26+
*/
27+
public function isCommand(): bool;
28+
29+
/**
30+
* Get the ip address.
2431
*
2532
* @return string|null
2633
*/

src/Handlers/TelegramLogHandler.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace Thanhtaivtt\TelegramLogger\Handlers;
55

6-
use Monolog\Handler\AbstractProcessingHandler;
76
use Monolog\Logger;
8-
use Thanhtaivtt\TelegramLogger\LoggerData;
7+
use Monolog\Handler\AbstractProcessingHandler;
8+
use \Thanhtaivtt\TelegramLogger\Contracts\LoggerData;
99

1010
/**
1111
* Handler send logs to Telegram using Telegram Bot API.
@@ -41,7 +41,7 @@ class TelegramLogHandler extends AbstractProcessingHandler
4141
* @param LoggerData $loggerData
4242
* @param array $config
4343
*/
44-
public function __construct($loggerData, array $config)
44+
public function __construct(LoggerData $loggerData, array $config)
4545
{
4646
parent::__construct($config['level'] ?? Logger::DEBUG, $config['bubble'] ?? true);
4747
$this->loggerData = $loggerData;
@@ -73,7 +73,11 @@ protected function messageBuilder($rawMessage): string
7373
$message = "<b>🧨[Laravel Telegram Logger]</b> \r\n";
7474
$message .= "METHOD: <b>{$this->loggerData->requestMethod()}</b> \r\n";
7575
$message .= "URL: {$this->loggerData->requestUrl()} \r\n";
76-
$message .= "IP: {$this->loggerData->ipAddress()} \r\n";
76+
77+
if (!$this->loggerData->isCommand()) {
78+
$message .= "IP: {$this->loggerData->ipAddress()} \r\n";
79+
}
80+
7781
$message .= "<pre>{$rawMessage}</pre>";
7882

7983
return $message;
@@ -95,6 +99,7 @@ protected function send(string $message): void
9599
'text' => $message,
96100
'parse_mode' => 'HTML',
97101
'chat_id' => $this->config['chat_id'],
102+
'disable_web_page_preview' => $this->config['disable_web_page_preview'] ?? true,
98103
]));
99104

100105
$result = curl_exec($ch);

src/LoggerData.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,19 @@ public function requestUrl(): ?string
6161
*/
6262
public function requestMethod(): ?string
6363
{
64-
return $this->request->method() ?? self::DEFAULT_METHOD;
64+
if ($this->isCommand()) {
65+
return self::DEFAULT_METHOD;
66+
}
67+
68+
return $this->request->method();
69+
}
70+
71+
/**
72+
* @inheritDoc
73+
*/
74+
public function isCommand(): bool
75+
{
76+
return strpos(php_sapi_name(), 'cli') !== false;
6577
}
6678

6779
/**

0 commit comments

Comments
 (0)