mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-24 18:16:10 +02:00
Add plain text message support
This commit is contained in:
@@ -40,13 +40,20 @@ class SlackHandler extends SocketHandler
|
|||||||
private $username;
|
private $username;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $token Slack API token
|
* Whether the message should be added to Slack as attachment (plain text otherwise)
|
||||||
* @param string $channel Slack channel (encoded ID or name)
|
* @var bool
|
||||||
* @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
|
|
||||||
*/
|
*/
|
||||||
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')) {
|
if (!extension_loaded('openssl')) {
|
||||||
throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
|
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->token = $token;
|
||||||
$this->channel = $channel;
|
$this->channel = $channel;
|
||||||
$this->username = $username;
|
$this->username = $username;
|
||||||
|
$this->useAttachment = $useAttachment;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -84,7 +92,12 @@ class SlackHandler extends SocketHandler
|
|||||||
'token' => $this->token,
|
'token' => $this->token,
|
||||||
'channel' => $this->channel,
|
'channel' => $this->channel,
|
||||||
'username' => $this->username,
|
'username' => $this->username,
|
||||||
'attachments' => json_encode(
|
'text' => '',
|
||||||
|
'attachments' => array()
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($this->useAttachment) {
|
||||||
|
$dataArray['attachments'] = json_encode(
|
||||||
array(
|
array(
|
||||||
array(
|
array(
|
||||||
'fallback' => $record['message'],
|
'fallback' => $record['message'],
|
||||||
@@ -103,8 +116,10 @@ class SlackHandler extends SocketHandler
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
);
|
||||||
);
|
} else {
|
||||||
|
$dataArray['text'] = $record['message'];
|
||||||
|
}
|
||||||
|
|
||||||
return http_build_query($dataArray);
|
return http_build_query($dataArray);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ class SlackHandlerTest extends TestCase
|
|||||||
fseek($this->res, 0);
|
fseek($this->res, 0);
|
||||||
$content = fread($this->res, 1024);
|
$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);
|
$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()
|
public function provideLevelColors()
|
||||||
{
|
{
|
||||||
return array(
|
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->res = fopen('php://memory', 'a');
|
||||||
$this->handler = $this->getMock(
|
$this->handler = $this->getMock(
|
||||||
'\Monolog\Handler\SlackHandler',
|
'\Monolog\Handler\SlackHandler',
|
||||||
|
|||||||
Reference in New Issue
Block a user