2022-10-17 19:25:03 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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\Handler;
|
|
|
|
|
2023-07-02 21:57:24 +00:00
|
|
|
use Monolog\Logger;
|
2022-10-17 19:25:03 +00:00
|
|
|
use Monolog\Formatter\FormatterInterface;
|
|
|
|
use Monolog\Formatter\LogglyFormatter;
|
|
|
|
use function array_key_exists;
|
|
|
|
use CurlHandle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends errors to Loggly.
|
|
|
|
*
|
|
|
|
* @author Przemek Sobstel <przemek@sobstel.org>
|
|
|
|
* @author Adam Pancutt <adam@pancutt.com>
|
|
|
|
* @author Gregory Barchard <gregory@barchard.net>
|
|
|
|
*/
|
|
|
|
class LogglyHandler extends AbstractProcessingHandler
|
|
|
|
{
|
|
|
|
protected const HOST = 'logs-01.loggly.com';
|
|
|
|
protected const ENDPOINT_SINGLE = 'inputs';
|
|
|
|
protected const ENDPOINT_BATCH = 'bulk';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Caches the curl handlers for every given endpoint.
|
|
|
|
*
|
2023-07-02 21:57:24 +00:00
|
|
|
* @var resource[]|CurlHandle[]
|
2022-10-17 19:25:03 +00:00
|
|
|
*/
|
2023-07-02 21:57:24 +00:00
|
|
|
protected $curlHandlers = [];
|
2022-10-17 19:25:03 +00:00
|
|
|
|
2023-07-02 21:57:24 +00:00
|
|
|
/** @var string */
|
|
|
|
protected $token;
|
2022-10-17 19:25:03 +00:00
|
|
|
|
|
|
|
/** @var string[] */
|
2023-07-02 21:57:24 +00:00
|
|
|
protected $tag = [];
|
2022-10-17 19:25:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $token API token supplied by Loggly
|
|
|
|
*
|
|
|
|
* @throws MissingExtensionException If the curl extension is missing
|
|
|
|
*/
|
2023-07-02 21:57:24 +00:00
|
|
|
public function __construct(string $token, $level = Logger::DEBUG, bool $bubble = true)
|
2022-10-17 19:25:03 +00:00
|
|
|
{
|
|
|
|
if (!extension_loaded('curl')) {
|
|
|
|
throw new MissingExtensionException('The curl extension is needed to use the LogglyHandler');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->token = $token;
|
|
|
|
|
|
|
|
parent::__construct($level, $bubble);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads and returns the shared curl handler for the given endpoint.
|
2023-07-02 21:57:24 +00:00
|
|
|
*
|
|
|
|
* @param string $endpoint
|
|
|
|
*
|
|
|
|
* @return resource|CurlHandle
|
2022-10-17 19:25:03 +00:00
|
|
|
*/
|
2023-07-02 21:57:24 +00:00
|
|
|
protected function getCurlHandler(string $endpoint)
|
2022-10-17 19:25:03 +00:00
|
|
|
{
|
|
|
|
if (!array_key_exists($endpoint, $this->curlHandlers)) {
|
|
|
|
$this->curlHandlers[$endpoint] = $this->loadCurlHandle($endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->curlHandlers[$endpoint];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Starts a fresh curl session for the given endpoint and returns its handler.
|
2023-07-02 21:57:24 +00:00
|
|
|
*
|
|
|
|
* @param string $endpoint
|
|
|
|
*
|
|
|
|
* @return resource|CurlHandle
|
2022-10-17 19:25:03 +00:00
|
|
|
*/
|
2023-07-02 21:57:24 +00:00
|
|
|
private function loadCurlHandle(string $endpoint)
|
2022-10-17 19:25:03 +00:00
|
|
|
{
|
|
|
|
$url = sprintf("https://%s/%s/%s/", static::HOST, $endpoint, $this->token);
|
|
|
|
|
|
|
|
$ch = curl_init();
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
|
|
|
return $ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[]|string $tag
|
|
|
|
*/
|
2023-07-02 21:57:24 +00:00
|
|
|
public function setTag($tag): self
|
2022-10-17 19:25:03 +00:00
|
|
|
{
|
2023-07-02 21:57:24 +00:00
|
|
|
$tag = !empty($tag) ? $tag : [];
|
|
|
|
$this->tag = is_array($tag) ? $tag : [$tag];
|
2022-10-17 19:25:03 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[]|string $tag
|
|
|
|
*/
|
2023-07-02 21:57:24 +00:00
|
|
|
public function addTag($tag): self
|
2022-10-17 19:25:03 +00:00
|
|
|
{
|
2023-07-02 21:57:24 +00:00
|
|
|
if (!empty($tag)) {
|
2022-10-17 19:25:03 +00:00
|
|
|
$tag = is_array($tag) ? $tag : [$tag];
|
|
|
|
$this->tag = array_unique(array_merge($this->tag, $tag));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2023-07-02 21:57:24 +00:00
|
|
|
protected function write(array $record): void
|
2022-10-17 19:25:03 +00:00
|
|
|
{
|
2023-07-02 21:57:24 +00:00
|
|
|
$this->send($record["formatted"], static::ENDPOINT_SINGLE);
|
2022-10-17 19:25:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function handleBatch(array $records): void
|
|
|
|
{
|
|
|
|
$level = $this->level;
|
|
|
|
|
|
|
|
$records = array_filter($records, function ($record) use ($level) {
|
2023-07-02 21:57:24 +00:00
|
|
|
return ($record['level'] >= $level);
|
2022-10-17 19:25:03 +00:00
|
|
|
});
|
|
|
|
|
2023-07-02 21:57:24 +00:00
|
|
|
if ($records) {
|
2022-10-17 19:25:03 +00:00
|
|
|
$this->send($this->getFormatter()->formatBatch($records), static::ENDPOINT_BATCH);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function send(string $data, string $endpoint): void
|
|
|
|
{
|
|
|
|
$ch = $this->getCurlHandler($endpoint);
|
|
|
|
|
|
|
|
$headers = ['Content-Type: application/json'];
|
|
|
|
|
2023-07-02 21:57:24 +00:00
|
|
|
if (!empty($this->tag)) {
|
2022-10-17 19:25:03 +00:00
|
|
|
$headers[] = 'X-LOGGLY-TAG: '.implode(',', $this->tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
|
|
|
|
|
|
|
Curl\Util::execute($ch, 5, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getDefaultFormatter(): FormatterInterface
|
|
|
|
{
|
|
|
|
return new LogglyFormatter();
|
|
|
|
}
|
|
|
|
}
|