mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-06 05:07:36 +02:00
Allow formatting of message by setting a line formatter on SlackHandler, fixes #829
This commit is contained in:
@@ -144,19 +144,25 @@ class SlackHandler extends SocketHandler
|
|||||||
'attachments' => array(),
|
'attachments' => array(),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if ($this->formatter) {
|
||||||
|
$message = $this->formatter->format($record);
|
||||||
|
} else {
|
||||||
|
$message = $record['message'];
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->useAttachment) {
|
if ($this->useAttachment) {
|
||||||
$attachment = array(
|
$attachment = array(
|
||||||
'fallback' => $record['message'],
|
'fallback' => $message,
|
||||||
'color' => $this->getAttachmentColor($record['level']),
|
'color' => $this->getAttachmentColor($record['level']),
|
||||||
'fields' => array(),
|
'fields' => array(),
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($this->useShortAttachment) {
|
if ($this->useShortAttachment) {
|
||||||
$attachment['title'] = $record['level_name'];
|
$attachment['title'] = $record['level_name'];
|
||||||
$attachment['text'] = $record['message'];
|
$attachment['text'] = $message;
|
||||||
} else {
|
} else {
|
||||||
$attachment['title'] = 'Message';
|
$attachment['title'] = 'Message';
|
||||||
$attachment['text'] = $record['message'];
|
$attachment['text'] = $message;
|
||||||
$attachment['fields'][] = array(
|
$attachment['fields'][] = array(
|
||||||
'title' => 'Level',
|
'title' => 'Level',
|
||||||
'value' => $record['level_name'],
|
'value' => $record['level_name'],
|
||||||
@@ -206,7 +212,7 @@ class SlackHandler extends SocketHandler
|
|||||||
|
|
||||||
$dataArray['attachments'] = json_encode(array($attachment));
|
$dataArray['attachments'] = json_encode(array($attachment));
|
||||||
} else {
|
} else {
|
||||||
$dataArray['text'] = $record['message'];
|
$dataArray['text'] = $message;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->iconEmoji) {
|
if ($this->iconEmoji) {
|
||||||
|
@@ -13,6 +13,7 @@ namespace Monolog\Handler;
|
|||||||
|
|
||||||
use Monolog\TestCase;
|
use Monolog\TestCase;
|
||||||
use Monolog\Logger;
|
use Monolog\Logger;
|
||||||
|
use Monolog\Formatter\LineFormatter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Greg Kedzierski <greg@gregkedzierski.com>
|
* @author Greg Kedzierski <greg@gregkedzierski.com>
|
||||||
@@ -57,6 +58,23 @@ class SlackHandlerTest extends TestCase
|
|||||||
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
|
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testWriteContentUsesFormatterIfProvided()
|
||||||
|
{
|
||||||
|
$this->createHandler('myToken', 'channel1', 'Monolog', false);
|
||||||
|
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
|
||||||
|
fseek($this->res, 0);
|
||||||
|
$content = fread($this->res, 1024);
|
||||||
|
|
||||||
|
$this->createHandler('myToken', 'channel1', 'Monolog', false);
|
||||||
|
$this->handler->setFormatter(new LineFormatter('foo--%message%'));
|
||||||
|
$this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
|
||||||
|
fseek($this->res, 0);
|
||||||
|
$content2 = fread($this->res, 1024);
|
||||||
|
|
||||||
|
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
|
||||||
|
$this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
|
||||||
|
}
|
||||||
|
|
||||||
public function testWriteContentWithEmoji()
|
public function testWriteContentWithEmoji()
|
||||||
{
|
{
|
||||||
$this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
|
$this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
|
||||||
|
Reference in New Issue
Block a user