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

Added support for Loggly batch uploads

This commit is contained in:
Adam Pancutt
2014-01-21 10:28:27 +00:00
parent 7f783c0ab0
commit e17aad1a99
3 changed files with 102 additions and 6 deletions

View File

@@ -0,0 +1,36 @@
<?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;
/**
* Extends the standard JsonFormatter to format messages compatible with Loggly.
*
* @author Adam Pancutt <adam@pancutt.com>
*/
class LogglyFormatter extends JsonFormatter
{
/**
* {@inheritdoc}
*/
public function formatBatch(array $records)
{
$instance = $this;
array_walk($records, function(&$value, $key) use ($instance) {
$value = $instance->format($value);
});
return implode("\n", $records);
}
}

View File

@@ -12,16 +12,19 @@
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Formatter\JsonFormatter;
use Monolog\Formatter\LogglyFormatter;
/**
* Sends errors to Loggly.
*
* @author Przemek Sobstel <przemek@sobstel.org>
* @author Adam Pancutt <adam@pancutt.com>
*/
class LogglyHandler extends AbstractProcessingHandler
{
const HOST = 'logs-01.loggly.com';
const ENDPOINT_SINGLE = 'inputs';
const ENDPOINT_BATCH = 'bulk';
protected $token;
@@ -45,17 +48,38 @@ class LogglyHandler extends AbstractProcessingHandler
protected function write(array $record)
{
$url = sprintf("http://%s/inputs/%s/", self::HOST, $this->token);
$this->send($record["formatted"], self::ENDPOINT_SINGLE);
}
public function handleBatch(array $records)
{
$level = $this->level;
$records = array_filter($records, function ($record) use ($level) {
return ($record['level'] >= $level);
});
if ($records) {
$this->send($this->getFormatter()->formatBatch($records), self::ENDPOINT_BATCH);
}
}
protected function send($data, $endpoint)
{
$url = sprintf("https://%s/%s/%s/", self::HOST, $endpoint, $this->token);
$headers = array('Content-Type: application/json');
if ($this->tag) {
$url .= sprintf("tag/%s/", $this->tag);
$headers[] = "X-LOGGLY-TAG: {$this->tag}";
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $record["formatted"]);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
@@ -64,6 +88,6 @@ class LogglyHandler extends AbstractProcessingHandler
protected function getDefaultFormatter()
{
return new JsonFormatter();
return new LogglyFormatter();
}
}

View File

@@ -0,0 +1,36 @@
<?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 LogglyFormatterTest extends TestCase
{
/**
* @covers Monolog\Formatter\LogglyFormatter::formatBatch
*/
public function testFormatBatch()
{
$formatter = new LogglyFormatter();
$records = $expected = array(
$this->getRecord(Logger::WARNING),
$this->getRecord(Logger::DEBUG),
);
array_walk($expected, function(&$value, $key) {
$value = json_encode($value);
});
$this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
}
}