1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-10 07:06:40 +02:00

added FlowdockFormatter

This commit is contained in:
Dominik Liebler
2014-03-16 15:13:54 +01:00
parent c362f9a07a
commit 3976583606
2 changed files with 159 additions and 0 deletions

View File

@@ -0,0 +1,104 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
/**
* formats the record to be used in the FlowdockHandler
*
* @author Dominik Liebler <liebler.dominik@gmail.com>
*/
class FlowdockFormatter implements FormatterInterface
{
/**
* @var string
*/
private $source;
/**
* @var string
*/
private $sourceEmail;
/**
* @param string $source
* @param string $sourceEmail
*/
public function __construct($source, $sourceEmail)
{
$this->source = $source;
$this->sourceEmail = $sourceEmail;
}
/**
* {@inheritdoc}
*/
public function format(array $record)
{
$tags = array(
'#logs',
'#' . strtolower($record['level_name']),
'#' . $record['channel'],
);
foreach ($record['extra'] as $value) {
$tags[] = '#' . $value;
}
$subject = sprintf(
'in %s: %s - %s',
$this->source,
$record['level_name'],
$this->getShortMessage($record['message'])
);
$record['flowdock'] = array(
'source' => $this->source,
'from_address' => $this->sourceEmail,
'subject' => $subject,
'content' => $record['message'],
'tags' => $tags,
'project' => $this->source,
);
return $record;
}
/**
* {@inheritdoc}
*/
public function formatBatch(array $records)
{
$formatted = array();
foreach ($records as $record) {
$formatted[] = $this->format($record);
}
return $formatted;
}
/**
* @param string $message
*
* @return string
*/
public function getShortMessage($message)
{
$maxLength = 45;
if (strlen($message) > $maxLength) {
$message = substr($message, 0, $maxLength - 4) . ' ...';
}
return $message;
}
}

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
use Monolog\Logger;
use Monolog\TestCase;
class FlowdockFormatterTest extends TestCase
{
/**
* @covers Monolog\Formatter\FlowdockFormatter::format
*/
public function testFormat()
{
$formatter = new FlowdockFormatter('test_source', 'source@test.com');
$record = $this->getRecord();
$expected = array(
'source' => 'test_source',
'from_address' => 'source@test.com',
'subject' => 'in test_source: WARNING - test',
'content' => 'test',
'tags' => array('#logs', '#warning', '#test'),
'project' => 'test_source',
);
$formatted = $formatter->format($record);
$this->assertEquals($expected, $formatted['flowdock']);
}
/**
* @ covers Monolog\Formatter\FlowdockFormatter::formatBatch
*/
public function testFormatBatch()
{
$formatter = new FlowdockFormatter('test_source', 'source@test.com');
$records = array(
$this->getRecord(Logger::WARNING),
$this->getRecord(Logger::DEBUG),
);
$formatted = $formatter->formatBatch($records);
$this->assertArrayHasKey('flowdock', $formatted[0]);
$this->assertArrayHasKey('flowdock', $formatted[1]);
}
}