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
12namespace Monolog\Handler;
13
14use Monolog\Logger;
15
16/**
17 * Inspired on LogEntriesHandler.
18 *
19 * @author Robert Kaufmann III <rok3@rok3.me>
20 * @author Gabriel Machado <gabriel.ms1@hotmail.com>
21 */
22class InsightOpsHandler extends SocketHandler
23{
24    /**
25     * @var string
26     */
27    protected $logToken;
28
29    /**
30     * @param string     $token  Log token supplied by InsightOps
31     * @param string     $region Region where InsightOps account is hosted. Could be 'us' or 'eu'.
32     * @param bool       $useSSL Whether or not SSL encryption should be used
33     *
34     * @throws MissingExtensionException If SSL encryption is set to true and OpenSSL is missing
35     */
36    public function __construct(
37        string $token,
38        string $region = 'us',
39        bool $useSSL = true,
40        $level = Logger::DEBUG,
41        bool $bubble = true,
42        bool $persistent = false,
43        float $timeout = 0.0,
44        float $writingTimeout = 10.0,
45        ?float $connectionTimeout = null,
46        ?int $chunkSize = null
47    ) {
48        if ($useSSL && !extension_loaded('openssl')) {
49            throw new MissingExtensionException('The OpenSSL PHP plugin is required to use SSL encrypted connection for InsightOpsHandler');
50        }
51
52        $endpoint = $useSSL
53            ? 'ssl://' . $region . '.data.logs.insight.rapid7.com:443'
54            : $region . '.data.logs.insight.rapid7.com:80';
55
56        parent::__construct(
57            $endpoint,
58            $level,
59            $bubble,
60            $persistent,
61            $timeout,
62            $writingTimeout,
63            $connectionTimeout,
64            $chunkSize
65        );
66        $this->logToken = $token;
67    }
68
69    /**
70     * {@inheritDoc}
71     */
72    protected function generateDataStream(array $record): string
73    {
74        return $this->logToken . ' ' . $record['formatted'];
75    }
76}
77