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 * @author Robert Kaufmann III <rok3@rok3.me>
18 */
19class LogEntriesHandler extends SocketHandler
20{
21    /**
22     * @var string
23     */
24    protected $logToken;
25
26    /**
27     * @param string     $token  Log token supplied by LogEntries
28     * @param bool       $useSSL Whether or not SSL encryption should be used.
29     * @param string     $host   Custom hostname to send the data to if needed
30     *
31     * @throws MissingExtensionException If SSL encryption is set to true and OpenSSL is missing
32     */
33    public function __construct(
34        string $token,
35        bool $useSSL = true,
36        $level = Logger::DEBUG,
37        bool $bubble = true,
38        string $host = 'data.logentries.com',
39        bool $persistent = false,
40        float $timeout = 0.0,
41        float $writingTimeout = 10.0,
42        ?float $connectionTimeout = null,
43        ?int $chunkSize = null
44    ) {
45        if ($useSSL && !extension_loaded('openssl')) {
46            throw new MissingExtensionException('The OpenSSL PHP plugin is required to use SSL encrypted connection for LogEntriesHandler');
47        }
48
49        $endpoint = $useSSL ? 'ssl://' . $host . ':443' : $host . ':80';
50        parent::__construct(
51            $endpoint,
52            $level,
53            $bubble,
54            $persistent,
55            $timeout,
56            $writingTimeout,
57            $connectionTimeout,
58            $chunkSize
59        );
60        $this->logToken = $token;
61    }
62
63    /**
64     * {@inheritDoc}
65     */
66    protected function generateDataStream(array $record): string
67    {
68        return $this->logToken . ' ' . $record['formatted'];
69    }
70}
71