mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-23 09:36:11 +02:00
Merge pull request #309 from apancutt/master
Added support for Loggly batch uploads
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user