Skip to content

Commit

Permalink
Added support for adding multiple webhook urls.
Browse files Browse the repository at this point in the history
  • Loading branch information
theriddleofenigma committed May 19, 2022
1 parent 07fed8a commit 4928c38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ Add the following code to the channels array in `config/logging.php` in your lar

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. And <b>LOG_GOOGLE_CHAT_NOTIFY_USER_ID</b> is optional.
<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.
Here, you can set multiple google chat webhook url as comma separated value for the <b>LOG_GOOGLE_WEBHOOK_URL</b> env variable.

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.

Expand Down
18 changes: 13 additions & 5 deletions src/GoogleChatHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,32 @@ class GoogleChatHandler extends AbstractProcessingHandler
*/
protected function write(array $record): void
{
Http::post($this->getWebhookUrl(), $this->getRequestBody($record));
foreach ($this->getWebhookUrl() as $url) {
Http::post($url, $this->getRequestBody($record));
}
}

/**
* Get the webhook url.
*
* @return mixed
* @return array
*
* @throws \Exception
*/
protected function getWebhookUrl()
protected function getWebhookUrl(): array
{
$url = config('logging.channels.google-chat.url');
if (!$url) {
throw new Exception('Google chat webhook url is not configured.');
}

return $url;
if (is_array($url)) {
return $url;
}

return array_map(function ($each) {
return trim($each);
}, explode(',', $url));
}

/**
Expand Down Expand Up @@ -134,7 +142,7 @@ protected function constructNotifiableText($userIds): string

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

return $allUsers . $otherIds;
Expand Down

0 comments on commit 4928c38

Please sign in to comment.