File tree Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Expand file tree Collapse file tree 2 files changed +16
-6
lines changed Original file line number Diff line number Diff line change 1010use Sentry \Severity ;
1111use Throwable ;
1212
13+ use function Sentry \logger ;
14+
1315class LogsHandler extends AbstractProcessingHandler
1416{
1517 use CompatibilityProcessingHandlerTrait;
@@ -95,20 +97,22 @@ public function getBatchFormatter(): FormatterInterface
9597 */
9698 protected function doWrite ($ record ): void
9799 {
98- $ exception = $ record ['context ' ]['exception ' ] ?? null ;
100+ $ context = $ record ['context ' ];
101+ $ exception = $ context ['exception ' ] ?? null ;
99102
100103 if ($ exception instanceof Throwable) {
101- return ;
104+ // Unset the exception object from the log context
105+ unset($ context ['exception ' ]);
102106 }
103107
104- \ Sentry \ logger ()->aggregator ()->add (
108+ logger ()->aggregator ()->add (
105109 // This seems a little bit of a roundabout way to get the log level, but this is done for compatibility
106110 self ::getLogLevelFromSeverity (
107111 self ::getSeverityFromLevel ($ record ['level ' ])
108112 ),
109113 $ record ['message ' ],
110114 [],
111- array_merge ($ record [ ' context ' ] , $ record ['extra ' ])
115+ array_merge ($ context , $ record ['extra ' ])
112116 );
113117 }
114118
Original file line number Diff line number Diff line change @@ -77,15 +77,21 @@ public function testLogChannelGeneratesLogsOnlyForConfiguredLevel(): void
7777 $ this ->assertEquals ('Sentry Laravel error log message ' , $ log ->getBody ());
7878 }
7979
80- public function testLogChannelDoesntCaptureExceptions (): void
80+ public function testLogChannelCapturesExceptions (): void
8181 {
8282 $ logger = Log::channel ('sentry_logs ' );
8383
8484 $ logger ->error ('Sentry Laravel error log message ' , ['exception ' => new \Exception ('Test exception ' )]);
8585
8686 $ logs = $ this ->getAndFlushCapturedLogs ();
8787
88- $ this ->assertCount (0 , $ logs );
88+ $ this ->assertCount (1 , $ logs );
89+
90+ $ log = $ logs [0 ];
91+
92+ $ this ->assertEquals (LogLevel::error (), $ log ->getLevel ());
93+ $ this ->assertEquals ('Sentry Laravel error log message ' , $ log ->getBody ());
94+ $ this ->assertNull ($ log ->attributes ()->get ('exception ' ));
8995 }
9096
9197 public function testLogChannelAddsContextAsAttributes (): void
You can’t perform that action at this time.
0 commit comments