Skip to content

Commit

Permalink
Major release to support Laravel 10 and Monolog 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
theriddleofenigma committed Jan 27, 2024
1 parent af193f7 commit dbd6126
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 48 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ Brings up the option for sending the logs to google chat [Google Workspace forme
composer require theriddleofenigma/laravel-google-chat-log
```

For laravel 9.x or lower, please use v1.x
```shell
composer require theriddleofenigma/laravel-google-chat-log:^1.3
```

Add the following code to the channels array in `config/logging.php` in your laravel/lumen application.
```php
'google-chat' => [
Expand Down Expand Up @@ -54,10 +59,10 @@ use Illuminate\Http\Request;
class AppServiceProvider {
public function register() {}
public function boot() {
GoogleChatHandler::$additionalLogs = function (Request $request) {
GoogleChatHandler::$additionalLogs = function () {
return [
'tenant' => $request->user()?->tenant->name,
'request' => json_encode($request->toArray()),
'tenant' => request()->user()?->tenant->name,
'request' => json_encode(request()->toArray()),
];
};
}
Expand Down
10 changes: 4 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
"authors": [
{
"name": "Kumaravel",
"email": "[email protected]"
"email": "[email protected]"
}
],
"require": {
"php": "^7.3|^8.0",
"php": "^8.1",
"monolog/monolog": "^3.0",
"illuminate/support": "^10.0|^11.0",
"guzzlehttp/guzzle": "^5.3.3|^6.2.1|^7.0"
},
"require-dev": {
"monolog/monolog": "^2.0",
"illuminate/support": "^7.0|^8.0|^9.0"
},
"autoload": {
"psr-4": {
"Enigma\\": "src/"
Expand Down
77 changes: 38 additions & 39 deletions src/GoogleChatHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Exception;
use Illuminate\Support\Facades\Http;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Monolog\Level;
use Monolog\LogRecord;

class GoogleChatHandler extends AbstractProcessingHandler
Expand All @@ -24,7 +24,7 @@ class GoogleChatHandler extends AbstractProcessingHandler
*
* @throws \Exception
*/
protected function write(array $record): void
protected function write(LogRecord $record): void
{
foreach ($this->getWebhookUrl() as $url) {
Http::post($url, $this->getRequestBody($record));
Expand Down Expand Up @@ -57,19 +57,20 @@ protected function getWebhookUrl(): array
/**
* Get the request body content.
*
* @param array $record
* @param LogRecord $record
* @return array
* @throws Exception
*/
protected function getRequestBody(array $record): array
protected function getRequestBody(LogRecord $record): array
{
return [
'text' => substr($this->getNotifiableText($record['level'] ?? '') . $record['formatted'], 0, 4096),
'text' => substr($this->getNotifiableText($record->level) . $record->formatted, 0, 4096),
'cardsV2' => [
[
'cardId' => 'info-card-id',
'card' => [
'header' => [
'title' => "{$record['level_name']}: {$record['message']}",
'title' => "{$record->level->getName()}: {$record->message}",
'subtitle' => config('app.name'),
],
'sections' => [
Expand All @@ -78,8 +79,8 @@ protected function getRequestBody(array $record): array
'uncollapsibleWidgetsCount' => 3,
'widgets' => [
$this->cardWidget(ucwords(config('app.env') ?: 'NA') . ' [Env]', 'BOOKMARK'),
$this->cardWidget($this->getLevelContent($record), 'TICKET'),
$this->cardWidget($record['datetime'], 'CLOCK'),
$this->cardWidget($this->getLevelContent($record->level), 'TICKET'),
$this->cardWidget($record->datetime, 'CLOCK'),
$this->cardWidget(request()->url(), 'BUS'),
...$this->getCustomLogs(),
],
Expand All @@ -93,50 +94,48 @@ protected function getRequestBody(array $record): array
/**
* Get the card content.
*
* @param array $record
* @param Level $level
* @return string
*/
protected function getLevelContent(array $record): string
protected function getLevelContent(Level $level): string
{
$color = [
Logger::EMERGENCY => '#ff1100',
Logger::ALERT => '#ff1100',
Logger::CRITICAL => '#ff1100',
Logger::ERROR => '#ff1100',
Logger::WARNING => '#ffc400',
Logger::NOTICE => '#00aeff',
Logger::INFO => '#48d62f',
Logger::DEBUG => '#000000',
][$record['level']] ?? '#ff1100';

return "<font color='{$color}'>{$record['level_name']}</font>";
$color = match ($level) {
Level::Warning => '#ffc400',
Level::Notice => '#00aeff',
Level::Info => '#48d62f',
Level::Debug => '#000000',
// Default matches emergency, alert, critical and error.
default => '#ff1100',
};

return "<font color='{$color}'>{$level->getName()}</font>";
}

/**
* Get the text string for notifying the configured user id.
*
* @param $level
* @param Level $level
* @return string
*/
protected function getNotifiableText($level): string
protected function getNotifiableText(Level $level): string
{
$levelBasedUserIds = [
Logger::EMERGENCY => config('logging.channels.google-chat.notify_users.emergency'),
Logger::ALERT => config('logging.channels.google-chat.notify_users.alert'),
Logger::CRITICAL => config('logging.channels.google-chat.notify_users.critical'),
Logger::ERROR => config('logging.channels.google-chat.notify_users.error'),
Logger::WARNING => config('logging.channels.google-chat.notify_users.warning'),
Logger::NOTICE => config('logging.channels.google-chat.notify_users.notice'),
Logger::INFO => config('logging.channels.google-chat.notify_users.info'),
Logger::DEBUG => config('logging.channels.google-chat.notify_users.debug'),
][$level] ?? '';

$levelBasedUserIds = trim($levelBasedUserIds);
$levelBasedUserIds = match ($level) {
Level::Emergency => config('logging.channels.google-chat.notify_users.emergency'),
Level::Alert => config('logging.channels.google-chat.notify_users.alert'),
Level::Critical => config('logging.channels.google-chat.notify_users.critical'),
Level::Error => config('logging.channels.google-chat.notify_users.error'),
Level::Warning => config('logging.channels.google-chat.notify_users.warning'),
Level::Notice => config('logging.channels.google-chat.notify_users.notice'),
Level::Info => config('logging.channels.google-chat.notify_users.info'),
Level::Debug => config('logging.channels.google-chat.notify_users.debug'),
};

$levelBasedUserIds = trim($levelBasedUserIds ?? '');
if (($userIds = config('logging.channels.google-chat.notify_users.default')) && $levelBasedUserIds) {
$levelBasedUserIds = ",$levelBasedUserIds";
}

return $this->constructNotifiableText(trim($userIds) . $levelBasedUserIds);
return $this->constructNotifiableText(trim($userIds ?? '') . $levelBasedUserIds);
}

/**
Expand Down Expand Up @@ -196,7 +195,7 @@ public function getCustomLogs(): array
return [];
}

$additionalLogs = $additionalLogs(request());
$additionalLogs = $additionalLogs();
if (!is_array($additionalLogs)) {
throw new Exception('Data returned from the additional Log must be an array.');
}
Expand All @@ -215,7 +214,7 @@ public function getCustomLogs(): array
$key = ucwords(str_replace('_', ' ', $key));
$value = "<b>{$key}:</b> $value";
}
$logs[] = $this->cardWidget($value, 'CONFIRMATION_NUMBER_ICON');
$logs[] = $this->cardWidget($value, 'DESCRIPTION');
}

return $logs;
Expand Down

0 comments on commit dbd6126

Please sign in to comment.