1 | <?php declare(strict_types=1);
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * This file is part of the Monolog package.
|
---|
5 | *
|
---|
6 | * (c) Jordi Boggiano <j.boggiano@seld.be>
|
---|
7 | *
|
---|
8 | * For the full copyright and license information, please view the LICENSE
|
---|
9 | * file that was distributed with this source code.
|
---|
10 | */
|
---|
11 |
|
---|
12 | namespace Monolog\Handler;
|
---|
13 |
|
---|
14 | use Monolog\Level;
|
---|
15 | use Monolog\Formatter\FormatterInterface;
|
---|
16 | use Monolog\Formatter\LogglyFormatter;
|
---|
17 | use CurlHandle;
|
---|
18 | use Monolog\LogRecord;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Sends errors to Loggly.
|
---|
22 | *
|
---|
23 | * @author Przemek Sobstel <przemek@sobstel.org>
|
---|
24 | * @author Adam Pancutt <adam@pancutt.com>
|
---|
25 | * @author Gregory Barchard <gregory@barchard.net>
|
---|
26 | */
|
---|
27 | class LogglyHandler extends AbstractProcessingHandler
|
---|
28 | {
|
---|
29 | protected const HOST = 'logs-01.loggly.com';
|
---|
30 | protected const ENDPOINT_SINGLE = 'inputs';
|
---|
31 | protected const ENDPOINT_BATCH = 'bulk';
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Caches the curl handlers for every given endpoint.
|
---|
35 | *
|
---|
36 | * @var CurlHandle[]
|
---|
37 | */
|
---|
38 | protected array $curlHandlers = [];
|
---|
39 |
|
---|
40 | protected string $token;
|
---|
41 |
|
---|
42 | /** @var string[] */
|
---|
43 | protected array $tag = [];
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * @param string $token API token supplied by Loggly
|
---|
47 | *
|
---|
48 | * @throws MissingExtensionException If the curl extension is missing
|
---|
49 | */
|
---|
50 | public function __construct(string $token, int|string|Level $level = Level::Debug, bool $bubble = true)
|
---|
51 | {
|
---|
52 | if (!\extension_loaded('curl')) {
|
---|
53 | throw new MissingExtensionException('The curl extension is needed to use the LogglyHandler');
|
---|
54 | }
|
---|
55 |
|
---|
56 | $this->token = $token;
|
---|
57 |
|
---|
58 | parent::__construct($level, $bubble);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Loads and returns the shared curl handler for the given endpoint.
|
---|
63 | */
|
---|
64 | protected function getCurlHandler(string $endpoint): CurlHandle
|
---|
65 | {
|
---|
66 | if (!\array_key_exists($endpoint, $this->curlHandlers)) {
|
---|
67 | $this->curlHandlers[$endpoint] = $this->loadCurlHandle($endpoint);
|
---|
68 | }
|
---|
69 |
|
---|
70 | return $this->curlHandlers[$endpoint];
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Starts a fresh curl session for the given endpoint and returns its handler.
|
---|
75 | */
|
---|
76 | private function loadCurlHandle(string $endpoint): CurlHandle
|
---|
77 | {
|
---|
78 | $url = sprintf("https://%s/%s/%s/", static::HOST, $endpoint, $this->token);
|
---|
79 |
|
---|
80 | $ch = curl_init();
|
---|
81 |
|
---|
82 | curl_setopt($ch, CURLOPT_URL, $url);
|
---|
83 | curl_setopt($ch, CURLOPT_POST, true);
|
---|
84 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
---|
85 |
|
---|
86 | return $ch;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * @param string[]|string $tag
|
---|
91 | * @return $this
|
---|
92 | */
|
---|
93 | public function setTag(string|array $tag): self
|
---|
94 | {
|
---|
95 | if ('' === $tag || [] === $tag) {
|
---|
96 | $this->tag = [];
|
---|
97 | } else {
|
---|
98 | $this->tag = \is_array($tag) ? $tag : [$tag];
|
---|
99 | }
|
---|
100 |
|
---|
101 | return $this;
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * @param string[]|string $tag
|
---|
106 | * @return $this
|
---|
107 | */
|
---|
108 | public function addTag(string|array $tag): self
|
---|
109 | {
|
---|
110 | if ('' !== $tag) {
|
---|
111 | $tag = \is_array($tag) ? $tag : [$tag];
|
---|
112 | $this->tag = array_unique(array_merge($this->tag, $tag));
|
---|
113 | }
|
---|
114 |
|
---|
115 | return $this;
|
---|
116 | }
|
---|
117 |
|
---|
118 | protected function write(LogRecord $record): void
|
---|
119 | {
|
---|
120 | $this->send($record->formatted, static::ENDPOINT_SINGLE);
|
---|
121 | }
|
---|
122 |
|
---|
123 | public function handleBatch(array $records): void
|
---|
124 | {
|
---|
125 | $level = $this->level;
|
---|
126 |
|
---|
127 | $records = array_filter($records, function ($record) use ($level) {
|
---|
128 | return ($record->level->value >= $level->value);
|
---|
129 | });
|
---|
130 |
|
---|
131 | if (\count($records) > 0) {
|
---|
132 | $this->send($this->getFormatter()->formatBatch($records), static::ENDPOINT_BATCH);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | protected function send(string $data, string $endpoint): void
|
---|
137 | {
|
---|
138 | $ch = $this->getCurlHandler($endpoint);
|
---|
139 |
|
---|
140 | $headers = ['Content-Type: application/json'];
|
---|
141 |
|
---|
142 | if (\count($this->tag) > 0) {
|
---|
143 | $headers[] = 'X-LOGGLY-TAG: '.implode(',', $this->tag);
|
---|
144 | }
|
---|
145 |
|
---|
146 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
---|
147 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
---|
148 |
|
---|
149 | Curl\Util::execute($ch, 5, false);
|
---|
150 | }
|
---|
151 |
|
---|
152 | protected function getDefaultFormatter(): FormatterInterface
|
---|
153 | {
|
---|
154 | return new LogglyFormatter();
|
---|
155 | }
|
---|
156 | }
|
---|