| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Monolog; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Monolog\Handler\AbstractProcessingHandler; |
| 7 |
use WCPOS\Vendor\Monolog\Logger; |
| 8 |
use WCPOS\Vendor\Monolog\LogRecord; |
| 9 |
use WCPOS\Vendor\Sentry\Event; |
| 10 |
use WCPOS\Vendor\Sentry\EventHint; |
| 11 |
use WCPOS\Vendor\Sentry\State\HubInterface; |
| 12 |
use WCPOS\Vendor\Sentry\State\Scope; |
| 13 |
/** |
| 14 |
* This Monolog handler logs every message to a Sentry's server using the given |
| 15 |
* hub instance. |
| 16 |
* |
| 17 |
* @deprecated since version 4.24. To be removed in version 5.0. Use {@see LogsHandler} |
| 18 |
* for Sentry logs, {@see ExceptionToSentryIssueHandler} to send Monolog exceptions |
| 19 |
* to Sentry issues, and {@see LogToSentryIssueHandler} |
| 20 |
* to send Monolog log messages to Sentry issues. |
| 21 |
* |
| 22 |
* @author Stefano Arlandini <[email protected]> |
| 23 |
*/ |
| 24 |
final class Handler extends AbstractProcessingHandler |
| 25 |
{ |
| 26 |
use CompatibilityProcessingHandlerTrait; |
| 27 |
private const CONTEXT_EXCEPTION_KEY = 'exception'; |
| 28 |
/** |
| 29 |
* @var HubInterface |
| 30 |
*/ |
| 31 |
private $hub; |
| 32 |
/** |
| 33 |
* @var bool |
| 34 |
*/ |
| 35 |
private $fillExtraContext; |
| 36 |
/** |
| 37 |
* {@inheritdoc} |
| 38 |
* |
| 39 |
* @param HubInterface $hub The hub to which errors are reported |
| 40 |
*/ |
| 41 |
public function __construct(HubInterface $hub, $level = Logger::DEBUG, bool $bubble = \true, bool $fillExtraContext = \false) |
| 42 |
{ |
| 43 |
parent::__construct($level, $bubble); |
| 44 |
$this->hub = $hub; |
| 45 |
$this->fillExtraContext = $fillExtraContext; |
| 46 |
} |
| 47 |
/** |
| 48 |
* @param array<string, mixed>|LogRecord $record |
| 49 |
*/ |
| 50 |
protected function doWrite($record) : void |
| 51 |
{ |
| 52 |
$event = Event::createEvent(); |
| 53 |
$event->setLevel(self::getSeverityFromLevel($record['level'])); |
| 54 |
$event->setMessage($record['message']); |
| 55 |
$event->setLogger(\sprintf('monolog.%s', $record['channel'])); |
| 56 |
$hint = new EventHint(); |
| 57 |
if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) { |
| 58 |
$hint->exception = $record['context']['exception']; |
| 59 |
} |
| 60 |
$this->hub->withScope(function (Scope $scope) use($record, $event, $hint) : void { |
| 61 |
$scope->setExtra('monolog.channel', $record['channel']); |
| 62 |
$scope->setExtra('monolog.level', $record['level_name']); |
| 63 |
$monologContextData = $this->getMonologContextData($record['context']); |
| 64 |
if ($monologContextData !== []) { |
| 65 |
$scope->setExtra('monolog.context', $monologContextData); |
| 66 |
} |
| 67 |
$monologExtraData = $this->getMonologExtraData($record['extra']); |
| 68 |
if ($monologExtraData !== []) { |
| 69 |
$scope->setExtra('monolog.extra', $monologExtraData); |
| 70 |
} |
| 71 |
$this->hub->captureEvent($event, $hint); |
| 72 |
}); |
| 73 |
} |
| 74 |
/** |
| 75 |
* @param mixed[] $context |
| 76 |
* |
| 77 |
* @return mixed[] |
| 78 |
*/ |
| 79 |
private function getMonologContextData(array $context) : array |
| 80 |
{ |
| 81 |
if (!$this->fillExtraContext) { |
| 82 |
return []; |
| 83 |
} |
| 84 |
$contextData = []; |
| 85 |
foreach ($context as $key => $value) { |
| 86 |
// We skip the `exception` field because it goes in its own context |
| 87 |
if ($key === self::CONTEXT_EXCEPTION_KEY) { |
| 88 |
continue; |
| 89 |
} |
| 90 |
$contextData[$key] = $value; |
| 91 |
} |
| 92 |
return $contextData; |
| 93 |
} |
| 94 |
/** |
| 95 |
* @param mixed[] $context |
| 96 |
* |
| 97 |
* @return mixed[] |
| 98 |
*/ |
| 99 |
private function getMonologExtraData(array $context) : array |
| 100 |
{ |
| 101 |
if (!$this->fillExtraContext) { |
| 102 |
return []; |
| 103 |
} |
| 104 |
$extraData = []; |
| 105 |
foreach ($context as $key => $value) { |
| 106 |
$extraData[$key] = $value; |
| 107 |
} |
| 108 |
return $extraData; |
| 109 |
} |
| 110 |
} |
| 111 |
|