| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Monolog; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Monolog\Formatter\FormatterInterface; |
| 7 |
use WCPOS\Vendor\Monolog\Formatter\LineFormatter; |
| 8 |
use WCPOS\Vendor\Monolog\Handler\HandlerInterface; |
| 9 |
use WCPOS\Vendor\Monolog\LogRecord; |
| 10 |
use WCPOS\Vendor\Sentry\Logs\LogLevel; |
| 11 |
use WCPOS\Vendor\Sentry\Logs\Logs; |
| 12 |
class LogsHandler implements HandlerInterface |
| 13 |
{ |
| 14 |
use CompatibilityLogLevelTrait; |
| 15 |
/** |
| 16 |
* The minimum logging level at which this handler will be triggered. |
| 17 |
* |
| 18 |
* @var LogLevel|\Monolog\Level|int |
| 19 |
*/ |
| 20 |
private $logLevel; |
| 21 |
/** |
| 22 |
* Whether the messages that are handled can bubble up the stack or not. |
| 23 |
* |
| 24 |
* @var bool |
| 25 |
*/ |
| 26 |
private $bubble; |
| 27 |
/** |
| 28 |
* Creates a new Monolog handler that converts Monolog logs to Sentry logs. |
| 29 |
* |
| 30 |
* @param LogLevel|\Monolog\Level|int|null $logLevel the minimum logging level at which this handler will be triggered and collects the logs |
| 31 |
* @param bool $bubble whether the messages that are handled can bubble up the stack or not |
| 32 |
*/ |
| 33 |
public function __construct($logLevel = null, bool $bubble = \true) |
| 34 |
{ |
| 35 |
$this->logLevel = $logLevel ?? LogLevel::debug(); |
| 36 |
$this->bubble = $bubble; |
| 37 |
} |
| 38 |
/** |
| 39 |
* @param array<string, mixed>|LogRecord $record |
| 40 |
*/ |
| 41 |
public function isHandling($record) : bool |
| 42 |
{ |
| 43 |
if ($this->logLevel instanceof LogLevel) { |
| 44 |
return self::getSentryLogLevelFromMonologLevel($record['level'])->getPriority() >= $this->logLevel->getPriority(); |
| 45 |
} elseif ($this->logLevel instanceof \WCPOS\Vendor\Monolog\Level) { |
| 46 |
return $record['level'] >= $this->logLevel->value; |
| 47 |
} |
| 48 |
return $record['level'] >= $this->logLevel; |
| 49 |
} |
| 50 |
/** |
| 51 |
* @param array<string, mixed>|LogRecord $record |
| 52 |
*/ |
| 53 |
public function handle($record) : bool |
| 54 |
{ |
| 55 |
if (!$this->isHandling($record)) { |
| 56 |
return \false; |
| 57 |
} |
| 58 |
// Do not collect logs for exceptions, they should be handled separately by `ExceptionToSentryIssueHandler` or `captureException` |
| 59 |
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) { |
| 60 |
return \false; |
| 61 |
} |
| 62 |
Logs::getInstance()->aggregator()->add(self::getSentryLogLevelFromMonologLevel($record['level']), $record['message'], [], $this->compileAttributes($record)); |
| 63 |
return $this->bubble === \false; |
| 64 |
} |
| 65 |
/** |
| 66 |
* @param array<array<string, mixed>|LogRecord> $records |
| 67 |
*/ |
| 68 |
public function handleBatch(array $records) : void |
| 69 |
{ |
| 70 |
foreach ($records as $record) { |
| 71 |
$this->handle($record); |
| 72 |
} |
| 73 |
} |
| 74 |
public function close() : void |
| 75 |
{ |
| 76 |
Logs::getInstance()->flush(); |
| 77 |
} |
| 78 |
/** |
| 79 |
* @param callable $callback |
| 80 |
*/ |
| 81 |
public function pushProcessor($callback) : void |
| 82 |
{ |
| 83 |
// noop, this handler does not support processors |
| 84 |
} |
| 85 |
/** |
| 86 |
* @return callable |
| 87 |
*/ |
| 88 |
public function popProcessor() |
| 89 |
{ |
| 90 |
// Since we do not support processors, we throw an exception if this method is called |
| 91 |
throw new \LogicException('You tried to pop from an empty processor stack.'); |
| 92 |
} |
| 93 |
public function setFormatter(FormatterInterface $formatter) : void |
| 94 |
{ |
| 95 |
// noop, this handler does not support formatters |
| 96 |
} |
| 97 |
public function getFormatter() : FormatterInterface |
| 98 |
{ |
| 99 |
// To adhere to the interface we need to return a formatter so we return a default one |
| 100 |
return new LineFormatter(); |
| 101 |
} |
| 102 |
public function __destruct() |
| 103 |
{ |
| 104 |
try { |
| 105 |
$this->close(); |
| 106 |
} catch (\Throwable $e) { |
| 107 |
// Just in case so that the destructor can never fail. |
| 108 |
} |
| 109 |
} |
| 110 |
/** |
| 111 |
* @param array<string,mixed>|LogRecord $record |
| 112 |
* |
| 113 |
* @return array<string,mixed> |
| 114 |
*/ |
| 115 |
protected function compileAttributes($record) : array |
| 116 |
{ |
| 117 |
return \array_merge($record['context'], $record['extra'], ['sentry.origin' => 'auto.log.monolog']); |
| 118 |
} |
| 119 |
} |
| 120 |
|