FingersCrossed
1 year ago
ChromePhpHandler.php
1 year ago
ConsoleHandler.php
1 year ago
ElasticsearchLogstashHandler.php
1 year ago
FirePHPHandler.php
1 year ago
MailerHandler.php
1 year ago
NotifierHandler.php
1 year ago
ServerLogHandler.php
2 months ago
SwiftMailerHandler.php
1 year ago
ServerLogHandler.php
130 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Bridge\Monolog\Handler; |
| 12 | |
| 13 | use Matomo\Dependencies\Monolog\Formatter\FormatterInterface; |
| 14 | use Matomo\Dependencies\Monolog\Handler\AbstractProcessingHandler; |
| 15 | use Matomo\Dependencies\Monolog\Handler\FormattableHandlerTrait; |
| 16 | use Matomo\Dependencies\Monolog\Logger; |
| 17 | use Matomo\Dependencies\Symfony\Bridge\Monolog\Formatter\VarDumperFormatter; |
| 18 | if (trait_exists(FormattableHandlerTrait::class)) { |
| 19 | class ServerLogHandler extends AbstractProcessingHandler |
| 20 | { |
| 21 | use ServerLogHandlerTrait; |
| 22 | /** |
| 23 | * {@inheritdoc} |
| 24 | */ |
| 25 | protected function getDefaultFormatter() : FormatterInterface |
| 26 | { |
| 27 | return new VarDumperFormatter(); |
| 28 | } |
| 29 | } |
| 30 | } else { |
| 31 | class ServerLogHandler extends AbstractProcessingHandler |
| 32 | { |
| 33 | use ServerLogHandlerTrait; |
| 34 | /** |
| 35 | * {@inheritdoc} |
| 36 | */ |
| 37 | protected function getDefaultFormatter() |
| 38 | { |
| 39 | return new VarDumperFormatter(); |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | /** |
| 44 | * @author Grégoire Pineau <lyrixx@lyrixx.info> |
| 45 | */ |
| 46 | trait ServerLogHandlerTrait |
| 47 | { |
| 48 | private $host; |
| 49 | private $context; |
| 50 | private $socket; |
| 51 | /** |
| 52 | * @param string|int $level The minimum logging level at which this handler will be triggered |
| 53 | */ |
| 54 | public function __construct(string $host, $level = Logger::DEBUG, bool $bubble = \true, array $context = []) |
| 55 | { |
| 56 | parent::__construct($level, $bubble); |
| 57 | if (!str_contains($host, '://')) { |
| 58 | $host = 'tcp://' . $host; |
| 59 | } |
| 60 | $this->host = $host; |
| 61 | $this->context = stream_context_create($context); |
| 62 | } |
| 63 | /** |
| 64 | * {@inheritdoc} |
| 65 | */ |
| 66 | public function handle(array $record) : bool |
| 67 | { |
| 68 | if (!$this->isHandling($record)) { |
| 69 | return \false; |
| 70 | } |
| 71 | set_error_handler(self::class . '::nullErrorHandler'); |
| 72 | try { |
| 73 | if (!($this->socket = $this->socket ?: $this->createSocket())) { |
| 74 | return \false === $this->bubble; |
| 75 | } |
| 76 | } finally { |
| 77 | restore_error_handler(); |
| 78 | } |
| 79 | return parent::handle($record); |
| 80 | } |
| 81 | protected function write(array $record) : void |
| 82 | { |
| 83 | $recordFormatted = $this->formatRecord($record); |
| 84 | set_error_handler(self::class . '::nullErrorHandler'); |
| 85 | try { |
| 86 | if (-1 === stream_socket_sendto($this->socket, $recordFormatted)) { |
| 87 | stream_socket_shutdown($this->socket, \STREAM_SHUT_RDWR); |
| 88 | // Let's retry: the persistent connection might just be stale |
| 89 | if ($this->socket = $this->createSocket()) { |
| 90 | stream_socket_sendto($this->socket, $recordFormatted); |
| 91 | } |
| 92 | } |
| 93 | } finally { |
| 94 | restore_error_handler(); |
| 95 | } |
| 96 | } |
| 97 | /** |
| 98 | * {@inheritdoc} |
| 99 | */ |
| 100 | protected function getDefaultFormatter() : FormatterInterface |
| 101 | { |
| 102 | return new VarDumperFormatter(); |
| 103 | } |
| 104 | private static function nullErrorHandler() |
| 105 | { |
| 106 | } |
| 107 | private function createSocket() |
| 108 | { |
| 109 | $socket = stream_socket_client($this->host, $errno, $errstr, 0, \STREAM_CLIENT_CONNECT | \STREAM_CLIENT_ASYNC_CONNECT | \STREAM_CLIENT_PERSISTENT, $this->context); |
| 110 | if ($socket) { |
| 111 | stream_set_blocking($socket, \false); |
| 112 | } |
| 113 | return $socket; |
| 114 | } |
| 115 | private function formatRecord(array $record) : string |
| 116 | { |
| 117 | $recordFormatted = $record['formatted']; |
| 118 | foreach (['log_uuid', 'uuid', 'uid'] as $key) { |
| 119 | if (isset($record['extra'][$key])) { |
| 120 | $recordFormatted['log_id'] = $record['extra'][$key]; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | if (isset($recordFormatted['datetime']) && $recordFormatted['datetime'] instanceof \DateTimeInterface) { |
| 125 | $recordFormatted['datetime'] = $recordFormatted['datetime']->format('Y-m-d\\TH:i:s.uP'); |
| 126 | } |
| 127 | return base64_encode(serialize($recordFormatted)) . "\n"; |
| 128 | } |
| 129 | } |
| 130 |