-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR started in order to support monolog v2 but it ended up containing more stuff.
- Loading branch information
Showing
17 changed files
with
146 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
language: php | ||
|
||
php: | ||
- 7.1 | ||
- 7.2 | ||
- 7.3 | ||
- 7.4 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
Copyright 2018 George Bonos | ||
# The MIT License (MIT) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
Copyright (c) 2018 George Mponos <[email protected]> | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
> Permission is hereby granted, free of charge, to any person obtaining a copy | ||
> of this software and associated documentation files (the "Software"), to deal | ||
> in the Software without restriction, including without limitation the rights | ||
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
> copies of the Software, and to permit persons to whom the Software is | ||
> furnished to do so, subject to the following conditions: | ||
> | ||
> The above copyright notice and this permission notice shall be included in | ||
> all copies or substantial portions of the Software. | ||
> | ||
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
> THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
* | ||
* @author George Mponos <[email protected]> | ||
*/ | ||
class SlackLineFormatter extends NormalizerFormatter implements SlackFormatterInterface | ||
final class SlackLineFormatter extends NormalizerFormatter implements SlackFormatterInterface | ||
{ | ||
/** | ||
* Username to use as display for the webhook | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
* | ||
* @author George Mponos <[email protected]> | ||
*/ | ||
class SlackLongAttachmentFormatter extends AbstractSlackAttachmentFormatter | ||
final class SlackLongAttachmentFormatter extends AbstractSlackAttachmentFormatter | ||
{ | ||
/** | ||
* @param array $record | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,12 +11,8 @@ | |
* | ||
* @author George Mponos <[email protected]> | ||
*/ | ||
class SlackShortAttachmentFormatter extends AbstractSlackAttachmentFormatter | ||
final class SlackShortAttachmentFormatter extends AbstractSlackAttachmentFormatter | ||
{ | ||
/** | ||
* @param array $record | ||
* @return array | ||
*/ | ||
protected function formatFields(array $record): array | ||
{ | ||
$value = $this->truncateStringIfNeeded($this->toJson($record, true)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,91 +2,61 @@ | |
|
||
namespace Webthink\MonologSlack\Handler; | ||
|
||
use GuzzleHttp\Psr7\Request; | ||
use GuzzleHttp\RequestOptions; | ||
use Monolog\Formatter\FormatterInterface; | ||
use Monolog\Handler\AbstractProcessingHandler; | ||
use Monolog\Handler\HandlerInterface; | ||
use Monolog\Logger; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Webthink\MonologSlack\Formatter\SlackFormatterInterface; | ||
use Webthink\MonologSlack\Formatter\SlackLineFormatter; | ||
use Webthink\MonologSlack\Utility\ClientInterface; | ||
|
||
/** | ||
* Sends notifications through a Slack Webhook. | ||
* | ||
* @author George Mponos <[email protected]> | ||
*/ | ||
class SlackWebhookHandler extends AbstractProcessingHandler | ||
final class SlackWebhookHandler extends AbstractProcessingHandler | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $webhook; | ||
|
||
/** | ||
* @var string|null | ||
* @var ClientInterface | ||
*/ | ||
private $username; | ||
private $client; | ||
|
||
/** | ||
* @var string|null | ||
* @var string | ||
*/ | ||
private $useCustomEmoji; | ||
private $webhook; | ||
|
||
/** | ||
* @var \Webthink\MonologSlack\Utility\ClientInterface|\Psr\Http\Client\ClientInterface | ||
* @var RequestFactoryInterface | ||
*/ | ||
private $client; | ||
private $requestFactory; | ||
|
||
/** | ||
* @param string $webhook The slack webhook URL | ||
* @param string|null $username Display name that will be used on the slack message | ||
* @param string|null $useCustomEmoji The custom emoji you want to use. Set null if you do not wish to use a custom one. | ||
* @param int|string $level The minimum logging level at which this handler will be triggered | ||
* @param ClientInterface $client | ||
* @param RequestFactoryInterface $requestFactory | ||
* @param string $webhook Slack Webhook string | ||
* @param string|int $level The minimum logging level at which this handler will be triggered | ||
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not | ||
* @param \Webthink\MonologSlack\Utility\ClientInterface|\Psr\Http\Client\ClientInterface|null $client | ||
*/ | ||
public function __construct( | ||
ClientInterface $client, | ||
RequestFactoryInterface $requestFactory, | ||
string $webhook, | ||
?string $username = null, | ||
?string $useCustomEmoji = null, | ||
$level = Logger::ERROR, | ||
bool $bubble = true, | ||
$client = null | ||
bool $bubble = true | ||
) { | ||
parent::__construct($level, $bubble); | ||
|
||
$this->webhook = $webhook; | ||
if (!$username !== null) { | ||
@trigger_error('Argument $username is deprecated and will be remove on 2.x version. Instead pass your custom formatter.', E_USER_DEPRECATED); | ||
} | ||
|
||
if (!$useCustomEmoji !== null) { | ||
@trigger_error('Argument $useCustomEmoji is deprecated and will be remove on 2.x version. Instead pass your custom formatter.', E_USER_DEPRECATED); | ||
} | ||
|
||
$this->username = $username; | ||
$this->useCustomEmoji = $useCustomEmoji; | ||
|
||
if ($client === null) { | ||
$client = \Http\Adapter\Guzzle6\Client::createWithConfig([ | ||
RequestOptions::TIMEOUT => 1, | ||
RequestOptions::CONNECT_TIMEOUT => 1, | ||
RequestOptions::HTTP_ERRORS => false, | ||
]); | ||
} | ||
|
||
if ($client instanceof ClientInterface) { | ||
@trigger_error('Using the custom HTTP Client implementation is deprecated and will be removed on 2.x. Use a PSR-18 HTTP Client instead.', E_USER_DEPRECATED); | ||
} | ||
|
||
$this->client = $client; | ||
$this->requestFactory = $requestFactory; | ||
$this->webhook = $webhook; | ||
} | ||
|
||
/** | ||
* @param FormatterInterface $formatter | ||
* @return self | ||
* @return HandlerInterface | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function setFormatter(FormatterInterface $formatter): HandlerInterface | ||
|
@@ -98,32 +68,26 @@ public function setFormatter(FormatterInterface $formatter): HandlerInterface | |
return parent::setFormatter($formatter); | ||
} | ||
|
||
/** | ||
* @param array $record | ||
* @return void | ||
*/ | ||
protected function write(array $record): void | ||
{ | ||
if ($this->client instanceof \Psr\Http\Client\ClientInterface) { | ||
$body = json_encode($record['formatted']); | ||
if ($body === false) { | ||
throw new \InvalidArgumentException('Could not format record to json'); | ||
}; | ||
$body = json_encode($record['formatted']); | ||
if ($body === false) { | ||
throw new \InvalidArgumentException('Could not format record to json'); | ||
}; | ||
|
||
$this->client->sendRequest( | ||
new Request('POST', $this->webhook, ['Content-Type' => ['application/json']], $body) | ||
); | ||
return; | ||
} | ||
$request = $this->requestFactory->createRequest('POST', $this->webhook); | ||
$request = $request->withHeader('Content-Type', ['application/json']); | ||
$request->getBody()->write($body); | ||
$request->getBody()->rewind(); | ||
|
||
$this->client->send($this->webhook, $record['formatted']); | ||
$this->client->sendRequest($request); | ||
} | ||
|
||
/** | ||
* @return SlackLineFormatter | ||
*/ | ||
protected function getDefaultFormatter(): FormatterInterface | ||
{ | ||
return new SlackLineFormatter($this->username, $this->useCustomEmoji); | ||
return new SlackLineFormatter(); | ||
} | ||
} |
Oops, something went wrong.