Skip to content

Send Logback logs to Sentry as logs #4502

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 24, 2025
Merged

Send Logback logs to Sentry as logs #4502

merged 13 commits into from
Jun 24, 2025

Conversation

adinauer
Copy link
Member

📜 Description

Allow setting a minimumLevel to our Logback integration which causes logs >= that level to be sent to Sentry as logs.

You also have to enable the logs feature:

Either in logback.xml:

<appender name="sentry" class="io.sentry.logback.SentryAppender">
  <options>
    <!-- NOTE: Replace the test DSN below with YOUR OWN DSN to see the events from this app in your Sentry project/dashboard -->
    <dsn>https://502f25099c204a2fbf4cb16edc5975d1@o447951.ingest.sentry.io/5428563</dsn>
    <logs>
      <enabled>true</enabled>
    </logs>
  </options>
  <!-- Demonstrates how to modify the minimum values -->
  <!-- Default for Events is ERROR -->
  <minimumEventLevel>WARN</minimumEventLevel>
  <!-- Default for Breadcrumbs is INFO -->
  <minimumBreadcrumbLevel>DEBUG</minimumBreadcrumbLevel>
  <!-- Default for Breadcrumbs is INFO -->
  <minimumLevel>INFO</minimumLevel>
</appender>

Or in sentry.properties:

logs.enabled=true

Or when calling Sentry.init:

    Sentry.init(options -> {
      ...
      options.getLogs().setEnabled(true);
    });

Note: This is different from breadcrumbs (minimumBreadcrumbLevel) and error events (minimumEventLevel) we already had since Sentry Logs is a separate product feature. Breadcrumbs and error events still work.

💡 Motivation and Context

Closes #4404

💚 How did you test it?

📝 Checklist

  • I added tests to verify the changes.
  • No new PII added or SDK only sends newly added PII if sendDefaultPII is enabled.
  • I updated the docs if needed.
  • I updated the wizard if needed.
  • Review from the native team if needed.
  • No breaking change or entry added to the changelog.
  • No breaking change for hybrid SDKs or communicated to hybrid SDKs.

🔮 Next steps

@adinauer
Copy link
Member Author

There's currently a problem, where we don't flush logs before the application shuts down. Will open a separate PR to fix.

Copy link
Contributor

github-actions bot commented Jun 20, 2025

Messages
📖 Do not forget to update Sentry-docs with your feature once the pull request gets approved.

Generated by 🚫 dangerJS against 216d9c5

Copy link
Contributor

github-actions bot commented Jun 20, 2025

Performance metrics 🚀

  Plain With Sentry Diff
Startup time 403.68 ms 438.08 ms 34.40 ms
Size 1.58 MiB 2.09 MiB 518.93 KiB

Previous results on branch: feat/logback-logs

Startup times

Revision Plain With Sentry Diff
fd99165 456.53 ms 539.57 ms 83.04 ms
ce5393a 412.67 ms 443.19 ms 30.52 ms

App size

Revision Plain With Sentry Diff
fd99165 1.58 MiB 2.09 MiB 518.80 KiB
ce5393a 1.58 MiB 2.09 MiB 518.66 KiB

Comment on lines 84 to 85
final Hint hint = new Hint();
hint.set(SENTRY_SYNTHETIC_EXCEPTION, eventObject);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final Hint hint = new Hint();
hint.set(SENTRY_SYNTHETIC_EXCEPTION, eventObject);

I think the hint is not needed here, right?

// for the Android compatibility we must use old Java Date class
@SuppressWarnings("JdkObsolete")
protected void captureLog(@NotNull ILoggingEvent loggingEvent) {
final @NotNull SentryLogLevel sentryLevel = toSentryLogLevel(loggingEvent.getLevel());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's also check if logs is enabled here, so we don't format the string if not necessary?

Copy link
Member

@stefanosiano stefanosiano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok
In mobile we want to wait for priority queue for logs integrations to avoid log envelopes throwing away errors, especially when the app is offline, but also in case an app logs really a lot.
Not sure how much of it is applicable to backend, though

@adinauer
Copy link
Member Author

Just added code to treat unformatted message + params as PII if encoder is present.
I'll add the tests to a follow up PR since they require some test utils I added there.

Comment on lines +187 to +191
if (attributes.get("sentry.message.template") == null) {
attributes.put(
"sentry.message.template",
new SentryLogEventAttributeValue(SentryAttributeType.STRING, message));
}
Copy link
Member

@lcian lcian Jun 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: I don't think we should set this. If the message is already formatted, then this will just duplicate it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of logging integrations, we can just set the sentry.message.template attribute in the integration and the newly added if prevents replacing that attribute.

The duplicate content should compress nicely.

Copy link
Member

@lcian lcian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@adinauer adinauer merged commit b93e18d into main Jun 24, 2025
34 checks passed
@adinauer adinauer deleted the feat/logback-logs branch June 24, 2025 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integration for Sentry Logs through Logback
3 participants