diff --git a/src/Monolog/Handler/SlackHandler.php b/src/Monolog/Handler/SlackHandler.php index 016f5019..5a3cdea2 100644 --- a/src/Monolog/Handler/SlackHandler.php +++ b/src/Monolog/Handler/SlackHandler.php @@ -40,13 +40,20 @@ class SlackHandler extends SocketHandler private $username; /** - * @param string $token Slack API token - * @param string $channel Slack channel (encoded ID or name) - * @param string $username Name of a bot - * @param 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 + * Whether the message should be added to Slack as attachment (plain text otherwise) + * @var bool */ - public function __construct($token, $channel, $username = 'Monolog', $level = Logger::CRITICAL, $bubble = true) + private $useAttachment; + + /** + * @param string $token Slack API token + * @param string $channel Slack channel (encoded ID or name) + * @param string $username Name of a bot + * @param 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 bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise) + */ + public function __construct($token, $channel, $username = 'Monolog', $level = Logger::CRITICAL, $bubble = true, $useAttachment = true) { if (!extension_loaded('openssl')) { throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler'); @@ -57,6 +64,7 @@ class SlackHandler extends SocketHandler $this->token = $token; $this->channel = $channel; $this->username = $username; + $this->useAttachment = $useAttachment; } /** @@ -84,7 +92,12 @@ class SlackHandler extends SocketHandler 'token' => $this->token, 'channel' => $this->channel, 'username' => $this->username, - 'attachments' => json_encode( + 'text' => '', + 'attachments' => array() + ); + + if ($this->useAttachment) { + $dataArray['attachments'] = json_encode( array( array( 'fallback' => $record['message'], @@ -103,8 +116,10 @@ class SlackHandler extends SocketHandler ) ) ) - ) - ); + ); + } else { + $dataArray['text'] = $record['message']; + } return http_build_query($dataArray); } diff --git a/tests/Monolog/Handler/SlackHandlerTest.php b/tests/Monolog/Handler/SlackHandlerTest.php index 58fb6248..90b7f664 100644 --- a/tests/Monolog/Handler/SlackHandlerTest.php +++ b/tests/Monolog/Handler/SlackHandlerTest.php @@ -54,7 +54,7 @@ class SlackHandlerTest extends TestCase fseek($this->res, 0); $content = fread($this->res, 1024); - $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&attachments=.*$/', $content); + $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content); } /** @@ -70,6 +70,16 @@ class SlackHandlerTest extends TestCase $this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content); } + public function testWriteContentWithPlainTextMessage() + { + $this->createHandler('myToken', 'channel1', 'Monolog', false); + $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1')); + fseek($this->res, 0); + $content = fread($this->res, 1024); + + $this->assertRegexp('/text=test1/', $content); + } + public function provideLevelColors() { return array( @@ -84,9 +94,9 @@ class SlackHandlerTest extends TestCase ); } - private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog') + private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true) { - $constructorArgs = array($token, $channel, $username, Logger::DEBUG); + $constructorArgs = array($token, $channel, $username, Logger::DEBUG, true, $useAttachment); $this->res = fopen('php://memory', 'a'); $this->handler = $this->getMock( '\Monolog\Handler\SlackHandler',