Skip to content

Conversation

@Eazybright
Copy link
Contributor

@Eazybright Eazybright commented Jun 18, 2025

Description

Describe your changes:
This is a fun way of adding integration for stdlib Logger so that any usage of any logger instance would result in sending logs to Sentry too. issue

Sample Config

require "sentry-ruby"

# Initialize Sentry with experimental logging feature enabled
Sentry.init do |config|
  config.dsn = "YOUR_SENTRY_DSN"
  config.environment = "development"

  # Set this to enable structured logging via Sentry.logger
  config.enable_logs = true

  # set this to send any instance of stdlib logger to sentry
  config.enabled_patches = [:logger]
end

logger = Logger.new(STDOUT)
logger.info('send_stdlib_log') { "send_stdlib_log_enable_logs: Testing 789..." }
logger.warn("ruby logger warn  - - 9")

Sample Result on the UI

Screenshot 2025-06-18 at 11 34 29 PM

@Eazybright Eazybright changed the title implement stblib logger support implement stdlib logger support Jun 18, 2025
Copy link
Member

@sl0thentr0py sl0thentr0py left a comment

Choose a reason for hiding this comment

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

left a small comment, will leave a proper review to @solnic


def add(severity, message = nil, progname = nil, &block)

return unless Sentry.initialized? && Sentry.get_current_hub
Copy link
Member

Choose a reason for hiding this comment

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

we need a super here and below since the original logger should not be affected

Copy link
Collaborator

@solnic solnic left a comment

Choose a reason for hiding this comment

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

Thank you for the PR! I've left some initial comments. Please also ensure that rubocop passes against the files you added and changed because there are issues with formatting 🙂

self.traces_sampler = nil
self.enable_tracing = nil
self.enable_logs = false
self.send_stdlib_logs = false
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not needed, there's a config option called enabled_patches that should be used instead. The Logger patch should be registered as :logger and then the users will be able to configure it to be enabled by default.

You can see how :http patch is done for reference.

return unless Sentry.initialized? && Sentry.get_current_hub

config = Sentry.configuration
return unless config.enable_logs && config.send_stdlib_logs
Copy link
Collaborator

Choose a reason for hiding this comment

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

This guard won't be needed when you add patching based on our enabled_patches option like I mentioned in the other comment. When registering your patch via register_patch you'll be able to do something like:

Sentry.register_patch(:logger, Sentry::StdLibLogger, ::Logger) do |config|
  if config.enable_logs
    ::Logger.prepend(Sentry::StdLibLogger)
  else
    Sentry.sdk_logger.warn(":logger patch enabled but `enable_logs` is turned off - skipping applying patch")
  end
end

if !message.nil? && message != Sentry::Logger::PROGNAME && method = SEVERITY_MAP[severity]
Sentry.logger.send(method, message)
end
end
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this method should always call super and logs to Sentry as an additional side effect.

@sl0thentr0py wouldn't you agree?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh wait nevermind @sl0thentr0py - I just noticed that you pointed it out as well above, sorry for the noise!

@Eazybright
Copy link
Contributor Author

Thank you for the PR! I've left some initial comments. Please also ensure that rubocop passes against the files you added and changed because there are issues with formatting 🙂

all comments have been addressed and PR updated. @solnic @sl0thentr0py

@solnic solnic mentioned this pull request Jun 24, 2025
@solnic
Copy link
Collaborator

solnic commented Jun 24, 2025

Thanks for the PR and for following up on the reviews! 🙇🏻

I've opened a new PR that builds on top of yours because of time constraints, as we need to finish this feature this week and we may need more back and worth in the review process.

See #2657 for more progress!

Gonna close this one because of this, I hope that's OK! All your commits are in my branch of course :)

@solnic solnic closed this Jun 24, 2025
@codecov
Copy link

codecov bot commented Jun 24, 2025

Codecov Report

Attention: Patch coverage is 89.47368% with 2 lines in your changes missing coverage. Please review.

Project coverage is 97.44%. Comparing base (32ca39e) to head (d20494e).

Files with missing lines Patch % Lines
sentry-ruby/lib/sentry/std_lib_logger.rb 88.88% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2653      +/-   ##
==========================================
+ Coverage   96.98%   97.44%   +0.45%     
==========================================
  Files         133      134       +1     
  Lines        5147     5167      +20     
==========================================
+ Hits         4992     5035      +43     
+ Misses        155      132      -23     
Components Coverage Δ
sentry-ruby 97.68% <89.47%> (+0.21%) ⬆️
sentry-rails 96.70% <ø> (+1.79%) ⬆️
sentry-sidekiq 96.57% <ø> (+1.14%) ⬆️
sentry-resque 94.44% <ø> (ø)
sentry-delayed_job 94.68% <ø> (ø)
sentry-opentelemetry 99.31% <ø> (ø)
Files with missing lines Coverage Δ
sentry-ruby/lib/sentry-ruby.rb 99.14% <100.00%> (+<0.01%) ⬆️
sentry-ruby/lib/sentry/std_lib_logger.rb 88.88% <88.88%> (ø)

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Eazybright
Copy link
Contributor Author

Thanks for the PR and for following up on the reviews! 🙇🏻

I've opened a new PR that builds on top of yours because of time constraints, as we need to finish this feature this week and we may need more back and worth in the review process.

See #2657 for more progress!

Gonna close this one because of this, I hope that's OK! All your commits are in my branch of course :)

Totally fine!

@solnic
Copy link
Collaborator

solnic commented Jun 26, 2025

@Eazybright thanks again for helping with this. I just merged my PR with your work included. We're gonna ship it in the next release 🙂

@Eazybright
Copy link
Contributor Author

@Eazybright thanks again for helping with this. I just merged my PR with your work included. We're gonna ship it in the next release 🙂

Great work @solnic

Thanks for the review, i learnt from it 🙏

@sl0thentr0py
Copy link
Member

thanks for the contribution @Eazybright! if you want a free Sentry t-shirt, send me an email on neel dot shah at sentry.io and we'll ship it to you :)

@Eazybright
Copy link
Contributor Author

thanks for the contribution @Eazybright! if you want a free Sentry t-shirt, send me an email on neel dot shah at sentry.io and we'll ship it to you :)

Will do, thanks!

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.

3 participants