PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / vendor / prefixed / symfony / monolog-bridge / Handler / ServerLogHandler.php
matomo / app / vendor / prefixed / symfony / monolog-bridge / Handler Last commit date
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