|
@@ -12,16 +12,19 @@
|
|
|
namespace Monolog\Handler;
|
|
namespace Monolog\Handler;
|
|
|
|
|
|
|
|
use Monolog\Logger;
|
|
use Monolog\Logger;
|
|
|
-use Monolog\Formatter\JsonFormatter;
|
|
|
|
|
|
|
+use Monolog\Formatter\LogglyFormatter;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Sends errors to Loggly.
|
|
* Sends errors to Loggly.
|
|
|
*
|
|
*
|
|
|
* @author Przemek Sobstel <przemek@sobstel.org>
|
|
* @author Przemek Sobstel <przemek@sobstel.org>
|
|
|
|
|
+ * @author Adam Pancutt <adam@pancutt.com>
|
|
|
*/
|
|
*/
|
|
|
class LogglyHandler extends AbstractProcessingHandler
|
|
class LogglyHandler extends AbstractProcessingHandler
|
|
|
{
|
|
{
|
|
|
const HOST = 'logs-01.loggly.com';
|
|
const HOST = 'logs-01.loggly.com';
|
|
|
|
|
+ const ENDPOINT_SINGLE = 'inputs';
|
|
|
|
|
+ const ENDPOINT_BATCH = 'bulk';
|
|
|
|
|
|
|
|
protected $token;
|
|
protected $token;
|
|
|
|
|
|
|
@@ -45,17 +48,38 @@ class LogglyHandler extends AbstractProcessingHandler
|
|
|
|
|
|
|
|
protected function write(array $record)
|
|
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) {
|
|
if ($this->tag) {
|
|
|
- $url .= sprintf("tag/%s/", $this->tag);
|
|
|
|
|
|
|
+ $headers[] = "X-LOGGLY-TAG: {$this->tag}";
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
$ch = curl_init();
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
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_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
curl_exec($ch);
|
|
curl_exec($ch);
|
|
@@ -64,6 +88,6 @@ class LogglyHandler extends AbstractProcessingHandler
|
|
|
|
|
|
|
|
protected function getDefaultFormatter()
|
|
protected function getDefaultFormatter()
|
|
|
{
|
|
{
|
|
|
- return new JsonFormatter();
|
|
|
|
|
|
|
+ return new LogglyFormatter();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|