|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\Laravel\Logs; |
| 4 | + |
| 5 | +use Monolog\Level; |
| 6 | +use Sentry\Logs\LogLevel; |
| 7 | +use Monolog\Formatter\LineFormatter; |
| 8 | +use Monolog\Formatter\FormatterInterface; |
| 9 | +use Monolog\Handler\AbstractProcessingHandler; |
| 10 | +use Sentry\Monolog\CompatibilityProcessingHandlerTrait; |
| 11 | +use Throwable; |
| 12 | + |
| 13 | +class LogsHandler extends AbstractProcessingHandler |
| 14 | +{ |
| 15 | + use CompatibilityProcessingHandlerTrait; |
| 16 | + |
| 17 | + /** |
| 18 | + * @var FormatterInterface The formatter to use for the logs generated via handleBatch() |
| 19 | + */ |
| 20 | + protected $batchFormatter; |
| 21 | + |
| 22 | + /** |
| 23 | + * {@inheritdoc} |
| 24 | + */ |
| 25 | + public function handleBatch(array $records): void |
| 26 | + { |
| 27 | + $level = $this->level; |
| 28 | + |
| 29 | + // filter records based on their level |
| 30 | + $records = array_filter( |
| 31 | + $records, |
| 32 | + function ($record) use ($level) { |
| 33 | + return $record['level'] >= $level; |
| 34 | + } |
| 35 | + ); |
| 36 | + |
| 37 | + if (!$records) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // the record with the highest severity is the "main" one |
| 42 | + $record = array_reduce( |
| 43 | + $records, |
| 44 | + function ($highest, $record) { |
| 45 | + if ($highest === null || $record['level'] > $highest['level']) { |
| 46 | + return $record; |
| 47 | + } |
| 48 | + |
| 49 | + return $highest; |
| 50 | + } |
| 51 | + ); |
| 52 | + |
| 53 | + // the other ones are added as a context item |
| 54 | + $logs = []; |
| 55 | + foreach ($records as $r) { |
| 56 | + $logs[] = $this->processRecord($r); |
| 57 | + } |
| 58 | + |
| 59 | + if ($logs) { |
| 60 | + $record['context']['logs'] = (string)$this->getBatchFormatter()->formatBatch($logs); |
| 61 | + } |
| 62 | + |
| 63 | + $this->handle($record); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Sets the formatter for the logs generated by handleBatch(). |
| 68 | + * |
| 69 | + * @param FormatterInterface $formatter |
| 70 | + * |
| 71 | + * @return \Sentry\Laravel\SentryHandler |
| 72 | + */ |
| 73 | + public function setBatchFormatter(FormatterInterface $formatter): self |
| 74 | + { |
| 75 | + $this->batchFormatter = $formatter; |
| 76 | + |
| 77 | + return $this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Gets the formatter for the logs generated by handleBatch(). |
| 82 | + */ |
| 83 | + public function getBatchFormatter(): FormatterInterface |
| 84 | + { |
| 85 | + if (!$this->batchFormatter) { |
| 86 | + $this->batchFormatter = new LineFormatter(); |
| 87 | + } |
| 88 | + |
| 89 | + return $this->batchFormatter; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * {@inheritdoc} |
| 94 | + * @suppress PhanTypeMismatchArgument |
| 95 | + */ |
| 96 | + protected function doWrite($record): void |
| 97 | + { |
| 98 | + $exception = $record['context']['exception'] ?? null; |
| 99 | + |
| 100 | + if ($exception instanceof Throwable) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + \Sentry\logger()->aggregator()->add( |
| 105 | + $this->getLevelFromMonologLevel($record['level']), |
| 106 | + $record['message'], |
| 107 | + [], |
| 108 | + array_merge($record['context'], $record['extra']) |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + private function getLevelFromMonologLevel(int $level): LogLevel |
| 113 | + { |
| 114 | + switch ($level) { |
| 115 | + case Level::Debug: |
| 116 | + return LogLevel::debug(); |
| 117 | + case Level::Warning: |
| 118 | + return LogLevel::warn(); |
| 119 | + case Level::Error: |
| 120 | + return LogLevel::error(); |
| 121 | + case Level::Critical: |
| 122 | + case Level::Alert: |
| 123 | + case Level::Emergency: |
| 124 | + return LogLevel::fatal(); |
| 125 | + case Level::Info: |
| 126 | + case Level::Notice: |
| 127 | + default: |
| 128 | + return LogLevel::info(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments