Skip to content

Commit

Permalink
Merge pull request #7 from theriddleofenigma/notify_user_id
Browse files Browse the repository at this point in the history
Notify users in error log
  • Loading branch information
theriddleofenigma authored Feb 8, 2022
2 parents 8c94105 + 33c2682 commit c265f0d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 27 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,32 @@ Add the following code to the channels array in `config/logging.php` in your lar
'google-chat' => [
'driver' => 'monolog',
'url' => env('LOG_GOOGLE_CHAT_WEBHOOK_URL'),
'notify_users' => [
'default' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEFAULT'),
'emergency' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_EMERGENCY'),
'alert' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_ALERT'),
'critical' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_CRITICAL'),
'error' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_ERROR'),
'warning' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_WARNING'),
'notice' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_NOTICE'),
'info' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_INFO'),
'debug' => env('LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEBUG'),
],
'level' => 'warning',
'handler' => \Enigma\GoogleChatHandler::class,
],
```

You can provide the eight logging levels defined in the [RFC 5424 specification](https://tools.ietf.org/html/rfc5424): `emergency`, `alert`, `critical`, `error`, `warning`, `notice`, `info`, and `debug`

<b>Note*:</b> Make sure to set the <b>LOG_GOOGLE_WEBHOOK_URL</b> env variable.
<b>Note*:</b> Make sure to set the <b>LOG_GOOGLE_WEBHOOK_URL</b> env variable. And <b>LOG_GOOGLE_CHAT_NOTIFY_USER_ID</b> is optional.

Now, you can notify a specific user with `@mention` in the error log by setting the corresponding USER_ID to the `LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEFAULT` env variable. User Ids mapped under `LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEFAULT` will be notified for all log levels.

For getting the <b>USER_ID</b>, right-click the user-icon of the person whom you want to notify in the Google chat from your browser window and select inspect. Under the `div` element find the attribute data_member_id, then the USER_ID can be found as `data-member-id="user/human/{USER_ID}>"`.

In order to notify all the users like `@all`, Set ```LOG_GOOGLE_CHAT_NOTIFY_USER_ID_DEFAULT=all```. Also, you can set multiple USER_IDs as comma separated value.
In order to notify different users for different log levels, you can set the corresponding env keys mentioned to configure in the `logging.php` file.

## License

Expand Down
104 changes: 78 additions & 26 deletions src/GoogleChatHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,20 @@ protected function write(array $record): void
}

/**
* Get the card content.
* Get the webhook url.
*
* @param array $record
* @return string
* @return mixed
*
* @throws \Exception
*/
public function getCardContent(array $record): string
protected function getWebhookUrl()
{
$color = [
Logger::DEBUG => '#000000',
Logger::INFO => '#48d62f',
Logger::NOTICE => '#00aeff',
Logger::WARNING => '#ffc400',
Logger::ERROR => '#ff1100',
Logger::CRITICAL => '#ff1100',
Logger::ALERT => '#ff1100',
Logger::EMERGENCY => '#ff1100',
][$record['level']] ?? '#ff1100';
$url = config('logging.channels.google-chat.url');
if (!$url) {
throw new Exception('Google chat webhook url is not configured.');
}

return "<b><font color='{$color}'>{$record['level_name']}</font></b> "
. config('app.env')
. ' [' . config('app.url') . "]<br>[{$record['datetime']}] ";
return $url;
}

/**
Expand All @@ -54,7 +47,7 @@ public function getCardContent(array $record): string
protected function getRequestBody(array $record): array
{
return [
'text' => substr($record['formatted'], 0, 4096),
'text' => substr($this->getNotifiableText($record['level'] ?? '') . $record['formatted'], 0, 4096),
'cards' => [
[
'sections' => [
Expand All @@ -70,19 +63,78 @@ protected function getRequestBody(array $record): array
}

/**
* Get the webhook url.
* Get the card content.
*
* @return mixed
* @param array $record
* @return string
*/
protected function getCardContent(array $record): 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 "<b><font color='{$color}'>{$record['level_name']}</font></b> "
. config('app.env')
. ' [' . config('app.url') . "]<br>[{$record['datetime']}] ";
}

/**
* Get the text string for notifying the configured user id.
*
* @throws \Exception
* @param $level
* @return string
*/
protected function getWebhookUrl()
protected function getNotifiableText($level): string
{
$url = config('logging.channels.google-chat.url');
if (!$url) {
throw new Exception('Google chat webhook url is not configured.');
$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] ?? '';

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

return $url;
return $this->constructNotifiableText($userIds . $levelBasedUserIds);
}

/**
* Get the notifiable text for the given userIds String.
*
* @param $userIds
* @return string
*/
protected function constructNotifiableText($userIds): string
{
$userIds = explode(',', $userIds);
if (!$userIds) {
return '';
}

$allUsers = '';
$otherIds = implode(array_map(function ($userId) use (&$allUsers) {
if (strtolower($userId) === 'all') {
$allUsers = '<users/all> ';
return '';
}

return "<users/$userId> ";
}, array_unique($userIds)));

return $allUsers . $otherIds;
}
}

0 comments on commit c265f0d

Please sign in to comment.