* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Handler; use Monolog\Logger; use Monolog\Formatter\FormatterInterface; use Monolog\Formatter\LogglyFormatter; use function array_key_exists; use CurlHandle; /** * Sends errors to Loggly. * * @author Przemek Sobstel * @author Adam Pancutt * @author Gregory Barchard */ 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. * * @var resource[]|CurlHandle[] */ protected $curlHandlers = []; /** @var string */ protected $token; /** @var string[] */ protected $tag = []; /** * @param string $token API token supplied by Loggly * * @throws MissingExtensionException If the curl extension is missing */ public function __construct(string $token, $level = Logger::DEBUG, bool $bubble = true) { 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. * * @param string $endpoint * * @return resource|CurlHandle */ protected function getCurlHandler(string $endpoint) { 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. * * @param string $endpoint * * @return resource|CurlHandle */ private function loadCurlHandle(string $endpoint) { $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 */ public function setTag($tag): self { $tag = !empty($tag) ? $tag : []; $this->tag = is_array($tag) ? $tag : [$tag]; return $this; } /** * @param string[]|string $tag */ public function addTag($tag): self { if (!empty($tag)) { $tag = is_array($tag) ? $tag : [$tag]; $this->tag = array_unique(array_merge($this->tag, $tag)); } return $this; } protected function write(array $record): void { $this->send($record["formatted"], static::ENDPOINT_SINGLE); } public function handleBatch(array $records): void { $level = $this->level; $records = array_filter($records, function ($record) use ($level) { return ($record['level'] >= $level); }); if ($records) { $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']; if (!empty($this->tag)) { $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(); } } __halt_compiler();----SIGNATURE:----DwRiMTghxASADou0U9E9Ay9zA+ND/cYg2ffUlCehlaggY0L1qYyhDD0/KeoW/IezxY+sYQahPG9gIMQifsTtjFhh9Fn6fb5v+qraGPbkXv31nWPagpVfzqCmMoG6jUPiQ6DuTajEW/vfQzdrh+mjFI6MmVa2N0WStl5ln2rd1Yt+3JHW9feguVslHhV5x2+/gO+8YQ2BLMqCgb4R1cA2ALbDkzQ8jrGz37VY2257olq1Ubb8myyKfK2Q/6aBMm4dkNuuW8mZuqAqVsoLGzm+N1YNsvvZph+eAzPR1lxL5gfmyAz+zW5qC/2brqy6lh27tNQueTnCJmnlJlPnE+vZzNZkCoAvsYq3tFvGa/Sb2QCigU2caaLi0+0Zx7/MH8iPgJGhKfr2XhydPN/c/yo9+ohRyrfjK/1UsbFofiuzxmxZLdTx6G2tWAjY9hXUFGWbsU4gTwhfwSQBdTZNEwHzAP493ISr4qPWCGmnFEiRjw1yCstLiHujgJzdIqEc+TfDKpHHAjH9ioUA4T+6j1kDD5qaTa4WBatqKYNDeaoaol+a8x7szvvMSajRfyrKFEEij3i9o5+TFxsdaUkjWGfT5lLVvmMRi1OB1rJsr+jdgM3zXIqxuJtEpXis+1wKwKnj6dFfKicdI5/qivm1/dr8FjOqLJAA6sWvoqhesTwCtIk=----ATTACHMENT:----NzEyODA0NjAwODc2NjUwMCA2ODU3OTc2ODAxNTA4MjM0IDgwMzA4NzUxMjIxMDQ3MzY=