1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 21:26:43 +02:00

Add plain text message support

This commit is contained in:
gkedzierski
2014-06-15 16:35:40 +02:00
parent 83ebdc1f32
commit efd9a2065c
2 changed files with 37 additions and 12 deletions

View File

@@ -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);
}

View File

@@ -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',