Skip to content

Commit 196c874

Browse files
authored
Merge pull request #38 from flownative/task/event-breadcrumbs
Add log entries as breadcrumbs for Sentry events
2 parents 905036c + 6e60fb5 commit 196c874

File tree

3 files changed

+86
-24
lines changed

3 files changed

+86
-24
lines changed

Classes/Log/SentryFileBackend.php

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515

1616
use Flownative\Sentry\SentryClientTrait;
1717
use Neos\Flow\Log\Backend\FileBackend;
18-
use Sentry\Severity;
18+
use Sentry\Breadcrumb;
19+
use Sentry\SentrySdk;
1920

2021
class 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
}

Configuration/Settings.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ Flownative:
1010
Neos:
1111
Flow:
1212
log:
13-
systemLogger:
14-
backend: Flownative\Sentry\Log\SentryFileBackend
13+
psr3:
14+
Neos\Flow\Log\PsrLoggerFactory:
15+
systemLogger:
16+
default:
17+
class: 'Flownative\Sentry\Log\SentryFileBackend'
1518
throwables:
1619
storageClass: 'Flownative\Sentry\Log\SentryStorage'
1720
optionsByImplementation:

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,45 @@ data" section in Sentry.
8888
Note that the array must only contain values of simple types, such as
8989
strings, booleans or integers.
9090

91+
## Logging integration
92+
93+
### Breadcrumb handler
94+
95+
This package configures a logging backend to add messages as breadcrumbs to
96+
be sent to Sentry when an exception happens. This provides more information
97+
on what happened before an exception.
98+
99+
If you want to include the security and query logging into this handling,
100+
feel free to add this to the Flow settings:
101+
102+
```yaml
103+
Neos:
104+
Flow:
105+
log:
106+
psr3:
107+
Neos\Flow\Log\PsrLoggerFactory:
108+
securityLogger:
109+
default:
110+
class: 'Flownative\Sentry\Log\SentryFileBackend'
111+
sqlLogger:
112+
default:
113+
class: 'Flownative\Sentry\Log\SentryFileBackend'
114+
```
115+
116+
For more information on breadcrumbs see the Sentry documentation at
117+
https://docs.sentry.io/platforms/php/enriching-events/breadcrumbs/
118+
119+
### Monolog
120+
121+
In case you want to store all log messages in Sentry, one way is to configure
122+
Flow to use monolog for logging and then add the `Sentry\Monolog\Handler` to
123+
the setup.
124+
125+
Keep in mind that the breadcrumb handler provided by this package might be
126+
disabled when doing this, depending on your configuration. Sentry provides
127+
a monolog integration for that purpose, see `Sentry\Monolog\BreadcrumbHandler`
128+
and https://docs.sentry.io/platforms/php/integrations/monolog/.
129+
91130
## Testing the Client
92131

93132
This package provides a command controller which allows you to log a

0 commit comments

Comments
 (0)