1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 20:57:36 +02:00

Add colors to attachments based on level

This commit is contained in:
gkedzierski
2014-06-15 15:55:24 +02:00
parent 20844b94ff
commit 83ebdc1f32
2 changed files with 53 additions and 4 deletions

View File

@@ -88,6 +88,7 @@ class SlackHandler extends SocketHandler
array( array(
array( array(
'fallback' => $record['message'], 'fallback' => $record['message'],
'color' => $this->getAttachmentColor($record['level']),
'fields' => array( 'fields' => array(
array( array(
'title' => 'Message', 'title' => 'Message',
@@ -135,4 +136,25 @@ class SlackHandler extends SocketHandler
parent::write($record); parent::write($record);
$this->closeSocket(); $this->closeSocket();
} }
/**
* Returned a Slack message attachment color associated with
* provided level.
*
* @param int $level
* @return string
*/
protected function getAttachmentColor($level)
{
switch (true) {
case $level >= Logger::ERROR:
return 'danger';
case $level >= Logger::WARNING:
return 'warning';
case $level >= Logger::INFO:
return 'good';
default:
return '#e3e4e6';
}
}
} }

View File

@@ -45,16 +45,43 @@ class SlackHandlerTest extends TestCase
$content = fread($this->res, 1024); $content = fread($this->res, 1024);
$this->assertRegexp('/POST \/api\/chat.postMessage HTTP\/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content); $this->assertRegexp('/POST \/api\/chat.postMessage HTTP\/1.1\\r\\nHost: slack.com\\r\\nContent-Type: application\/x-www-form-urlencoded\\r\\nContent-Length: \d{2,4}\\r\\n\\r\\n/', $content);
}
return $content; public function testWriteContent()
{
$this->createHandler();
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&attachments=.*$/', $content);
} }
/** /**
* @depends testWriteHeader * @dataProvider provideLevelColors
*/ */
public function testWriteContent($content) public function testWriteContentWithColors($level, $expectedColor)
{ {
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&attachments=.*$/', $content); $this->createHandler();
$this->handler->handle($this->getRecord($level, 'test1'));
fseek($this->res, 0);
$content = fread($this->res, 1024);
$this->assertRegexp('/color%22%3A%22'.$expectedColor.'/', $content);
}
public function provideLevelColors()
{
return array(
array(Logger::DEBUG, '%23e3e4e6'), // escaped #e3e4e6
array(Logger::INFO, 'good'),
array(Logger::NOTICE, 'good'),
array(Logger::WARNING, 'warning'),
array(Logger::ERROR, 'danger'),
array(Logger::CRITICAL, 'danger'),
array(Logger::ALERT, 'danger'),
array(Logger::EMERGENCY,'danger'),
);
} }
private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog') private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog')