mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-02 11:20:31 +02:00
Added support for Loggly batch uploads
This commit is contained in:
36
src/Monolog/Formatter/LogglyFormatter.php
Normal file
36
src/Monolog/Formatter/LogglyFormatter.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
36
tests/Monolog/Formatter/LogglyFormatterTest.php
Normal file
36
tests/Monolog/Formatter/LogglyFormatterTest.php
Normal 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));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user