vendor/sentry/sentry/src/Monolog/Handler.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\Monolog;
  4. use Monolog\Handler\AbstractProcessingHandler;
  5. use Monolog\Logger;
  6. use Sentry\Severity;
  7. use Sentry\State\HubInterface;
  8. use Sentry\State\Scope;
  9. /**
  10.  * This Monolog handler logs every message to a Sentry's server using the given
  11.  * hub instance.
  12.  *
  13.  * @author Stefano Arlandini <sarlandini@alice.it>
  14.  */
  15. final class Handler extends AbstractProcessingHandler
  16. {
  17.     /**
  18.      * @var HubInterface
  19.      */
  20.     private $hub;
  21.     /**
  22.      * Constructor.
  23.      *
  24.      * @param HubInterface $hub    The hub to which errors are reported
  25.      * @param int|string   $level  The minimum logging level at which this
  26.      *                             handler will be triggered
  27.      * @param bool         $bubble Whether the messages that are handled can
  28.      *                             bubble up the stack or not
  29.      */
  30.     public function __construct(HubInterface $hub$level Logger::DEBUGbool $bubble true)
  31.     {
  32.         $this->hub $hub;
  33.         parent::__construct($level$bubble);
  34.     }
  35.     /**
  36.      * {@inheritdoc}
  37.      */
  38.     protected function write(array $record): void
  39.     {
  40.         $payload = [
  41.             'level' => self::getSeverityFromLevel($record['level']),
  42.             'message' => $record['message'],
  43.             'logger' => 'monolog.' $record['channel'],
  44.         ];
  45.         if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Throwable) {
  46.             $payload['exception'] = $record['context']['exception'];
  47.         }
  48.         $this->hub->withScope(function (Scope $scope) use ($record$payload): void {
  49.             $scope->setExtra('monolog.channel'$record['channel']);
  50.             $scope->setExtra('monolog.level'$record['level_name']);
  51.             if (isset($record['context']['extra']) && \is_array($record['context']['extra'])) {
  52.                 foreach ($record['context']['extra'] as $key => $value) {
  53.                     $scope->setExtra((string) $key$value);
  54.                 }
  55.             }
  56.             if (isset($record['context']['tags']) && \is_array($record['context']['tags'])) {
  57.                 foreach ($record['context']['tags'] as $key => $value) {
  58.                     $scope->setTag($key$value);
  59.                 }
  60.             }
  61.             $this->hub->captureEvent($payload);
  62.         });
  63.     }
  64.     /**
  65.      * Translates the Monolog level into the Sentry severity.
  66.      *
  67.      * @param int $level The Monolog log level
  68.      */
  69.     private static function getSeverityFromLevel(int $level): Severity
  70.     {
  71.         switch ($level) {
  72.             case Logger::DEBUG:
  73.                 return Severity::debug();
  74.             case Logger::INFO:
  75.             case Logger::NOTICE:
  76.                 return Severity::info();
  77.             case Logger::WARNING:
  78.                 return Severity::warning();
  79.             case Logger::ERROR:
  80.                 return Severity::error();
  81.             case Logger::CRITICAL:
  82.             case Logger::ALERT:
  83.             case Logger::EMERGENCY:
  84.                 return Severity::fatal();
  85.             default:
  86.                 return Severity::info();
  87.         }
  88.     }
  89. }