Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Emoji support for the Slack Handler #413

Merged
merged 4 commits into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/Monolog/Handler/SlackHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class SlackHandler extends SocketHandler
* @var string
*/
private $username;

/**
* Emoji icon name
* @var string
*/
private $iconEmoji;

/**
* Whether the message should be added to Slack as attachment (plain text otherwise)
Expand All @@ -53,7 +59,7 @@ class SlackHandler extends SocketHandler
* @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', $useAttachment = true, $level = Logger::CRITICAL, $bubble = true)
public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $level = Logger::CRITICAL, $bubble = true, $iconEmoji = null)
{
if (!extension_loaded('openssl')) {
throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
Expand All @@ -64,6 +70,7 @@ public function __construct($token, $channel, $username = 'Monolog', $useAttachm
$this->token = $token;
$this->channel = $channel;
$this->username = $username;
$this->iconEmoji = $iconEmoji;
$this->useAttachment = $useAttachment;
}

Expand Down Expand Up @@ -120,7 +127,11 @@ private function buildContent($record)
} else {
$dataArray['text'] = $record['message'];
}


if ($this->iconEmoji !== null) {
$dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
}

return http_build_query($dataArray);
}

Expand Down
14 changes: 12 additions & 2 deletions tests/Monolog/Handler/SlackHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public function testWriteContent()

$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
}

public function testWriteContentWithEmoji()
{
$this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
fseek($this->res, 0);
$content = fread($this->res, 1024);

$this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
}

/**
* @dataProvider provideLevelColors
Expand Down Expand Up @@ -94,9 +104,9 @@ public function provideLevelColors()
);
}

private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true)
private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null)
{
$constructorArgs = array($token, $channel, $username, $useAttachment, Logger::DEBUG, true);
$constructorArgs = array($token, $channel, $username, $useAttachment, Logger::DEBUG, true, $iconEmoji);
$this->res = fopen('php://memory', 'a');
$this->handler = $this->getMock(
'\Monolog\Handler\SlackHandler',
Expand Down