1515
1616use Flownative \Sentry \SentryClientTrait ;
1717use Neos \Flow \Log \Backend \FileBackend ;
18- use Sentry \Severity ;
18+ use Sentry \Breadcrumb ;
19+ use Sentry \SentrySdk ;
1920
2021class SentryFileBackend extends FileBackend
2122{
2223 use SentryClientTrait;
2324
24- private bool $ capturingMessage = false ;
25-
2625 /**
2726 * Appends the given message along with the additional information into the log.
2827 *
@@ -36,29 +35,50 @@ class SentryFileBackend extends FileBackend
3635 */
3736 public function append (string $ message , int $ severity = LOG_INFO , $ additionalData = null , ?string $ packageKey = null , ?string $ className = null , ?string $ methodName = null ): void
3837 {
39- if ($ this ->capturingMessage ) {
40- return ;
38+ try {
39+ SentrySdk::getCurrentHub ()->addBreadcrumb (
40+ new Breadcrumb (
41+ $ this ->getBreadcrumbLevel ($ severity ),
42+ $ this ->getBreadcrumbType ($ severity ),
43+ basename ($ this ->logFileUrl ),
44+ $ message ,
45+ ($ additionalData ?? []) + array_filter ([
46+ 'packageKey ' => $ packageKey , 'className ' => $ className , 'methodName ' => $ methodName
47+ ]),
48+ time ()
49+ )
50+ );
51+ } catch (\Throwable $ throwable ) {
52+ parent ::append (
53+ sprintf ('%s (%s) ' , $ throwable ->getMessage (), $ throwable ->getCode ()),
54+ LOG_WARNING ,
55+ null ,
56+ 'Flownative.Sentry ' ,
57+ __CLASS__ ,
58+ __METHOD__
59+ );
4160 }
4261
43- try {
44- $ this -> capturingMessage = true ;
62+ parent :: append ( $ message , $ severity , $ additionalData , $ packageKey , $ className , $ methodName );
63+ }
4564
46- $ sentryClient = self ::getSentryClient ();
47- if ($ severity <= LOG_NOTICE && $ sentryClient ) {
48- $ sentrySeverity = match ($ severity ) {
49- LOG_WARNING => Severity::warning (),
50- LOG_ERR => Severity::error (),
51- LOG_CRIT , LOG_ALERT , LOG_EMERG => Severity::fatal (),
52- default => Severity::info (),
53- };
65+ private function getBreadcrumbLevel (int $ severity ): string
66+ {
67+ return match ($ severity ) {
68+ LOG_EMERG , LOG_ALERT , LOG_CRIT => Breadcrumb::LEVEL_FATAL ,
69+ LOG_ERR => Breadcrumb::LEVEL_ERROR ,
70+ LOG_WARNING => Breadcrumb::LEVEL_WARNING ,
71+ LOG_NOTICE , LOG_INFO => Breadcrumb::LEVEL_INFO ,
72+ default => Breadcrumb::LEVEL_DEBUG ,
73+ };
74+ }
5475
55- $ sentryClient ->captureMessage ($ message , $ sentrySeverity , ['Additional Data ' => $ additionalData ]);
56- }
57- parent ::append ($ message , $ severity , $ additionalData , $ packageKey , $ className , $ methodName );
58- } catch (\Throwable $ throwable ) {
59- echo sprintf ('SentryFileBackend: %s (%s) ' , $ throwable ->getMessage (), $ throwable ->getCode ());
60- } finally {
61- $ this ->capturingMessage = false ;
76+ private function getBreadcrumbType (int $ severity ): string
77+ {
78+ if ($ severity >= LOG_ERR ) {
79+ return Breadcrumb::TYPE_ERROR ;
6280 }
81+
82+ return Breadcrumb::TYPE_DEFAULT ;
6383 }
6484}
0 commit comments