* @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:----o5F1+2E/++TqX2qsxNBvJcpt1p0lnMsvqdERyG7u+53xUT2rVn5zeA/B/sydk7CIm2sqj64qaoFLxMj4jlQ0mt2YehwgPYn/QOgUcAxfdLNWhGVrOZ9ryKIxE+07OAWWshftddOcanItmup4ukOSEorF4fdE5J0iVELxD/7YmmgboZDUw1DYfj6RB4IQ972/Oirq/lr0TrM51vJ8P1XEMvoJcA+QXGWELcqzsmC/bCrhxMPanpW1018hdIO1XHtGcJv9E4wz7eM9F0YFzvldnjH6kjscJlv6wL28Zwuwa1uyCXyzvJjaigcU8PwCorsaqP3JYQE5rpHU3IBWMC5n1NJqoe2ZXqUDScl4BHwJXyN28E7orhKdCTqWklHnWZdqGtzt3hSFu8wgGZ7+7D/z4AeegeTRdGEkaZPFoy5Aw7dvp6rczM4E9xBMW9ptdCH84ATQFOtHW3UmdwgV89/7ZCwm2g+ms90NsLaCwp2z7z2WqJhGbRvkckz9u08E1o56SeIXDz3ADoLcA7bPfqrCKgklGJgq9x2j5Ag2kdIl4uVrC/3xwBqGkxkthx7XyVwMLHWj5izZkkjdyocKwAuklKE/tS23OKkk8LglNeMqjuiw7rjLm/kmodV8bZggA01wZYhOaPX8xhuCKzH3PtwMppkIQo2QWBggRH5C1TyUQKs=----ATTACHMENT:----NTM3MzkzMDM3NzIyNDIxMiA4Mjk2MTk0NzQ2MTgzNjI0IDc2NjU2MzY1NDI0NDEzNzY=