-
-
Notifications
You must be signed in to change notification settings - Fork 538
Logger support #2657
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
Logger support #2657
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4753964
implement stblib logger support
Eazybright 3adb8b5
update changelog
Eazybright 9a9738f
Merge branch 'master' into stdlib-logger-support
Eazybright 9beaafa
use existing patch to register stdlib logger
Eazybright c51dd30
Merge branch 'stdlib-logger-support' of github.com:Eazybright/sentry-…
Eazybright 6783dfa
remove unused code
Eazybright d20494e
fix rubocop lint issues
Eazybright 2e7bd10
Isolate sentry logger and std lib logger specs
solnic 0121c8c
Call super just once
solnic 87198c5
Update CHANGELOG
solnic 2d4ef24
Add SimpleCov command name for SentryLogger specs
solnic cdf0ad8
Fix logger patch warning message configuration
solnic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Sentry | ||
| # Ruby Logger support Add commentMore actions | ||
| # intercepts any logger instance and send the log to Sentry too. | ||
| module StdLibLogger | ||
| SEVERITY_MAP = { | ||
| 0 => :debug, | ||
| 1 => :info, | ||
| 2 => :warn, | ||
| 3 => :error, | ||
| 4 => :fatal | ||
| }.freeze | ||
|
|
||
| def add(severity, message = nil, progname = nil, &block) | ||
| result = super | ||
|
|
||
| return unless Sentry.initialized? && Sentry.get_current_hub | ||
|
|
||
| # exclude sentry SDK logs -- to prevent recursive log action, | ||
| # do not process internal logs again | ||
| if message.nil? && progname != Sentry::Logger::PROGNAME | ||
|
|
||
| # handle different nature of Ruby Logger class: | ||
| # inspo from Sentry::Breadcrumb::SentryLogger | ||
| if block_given? | ||
| message = yield | ||
| else | ||
| message = progname | ||
| end | ||
|
|
||
| message = message.to_s.strip | ||
|
|
||
| if !message.nil? && message != Sentry::Logger::PROGNAME && method = SEVERITY_MAP[severity] | ||
| Sentry.logger.send(method, message) | ||
| end | ||
| end | ||
|
|
||
| result | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Sentry.register_patch(:logger) do |config| | ||
| if config.enable_logs | ||
| ::Logger.prepend(Sentry::StdLibLogger) | ||
| else | ||
| config.sdk_logger.warn(":logger patch enabled but `enable_logs` is turned off - skipping applying patch") | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| SimpleCov.command_name "StdLibLogger" | ||
|
|
||
| RSpec.describe Sentry::StdLibLogger do | ||
| let(:logger) { ::Logger.new($stdout) } | ||
|
|
||
| context "when logger patch is enabled but enable_logs is turned off" do | ||
| it "logs a warning message" do | ||
| string_io = StringIO.new | ||
|
|
||
| perform_basic_setup do |config| | ||
| config.enable_logs = false | ||
| config.enabled_patches = [:logger] | ||
| config.sdk_logger = ::Logger.new(string_io) | ||
| end | ||
|
|
||
| expect(string_io.string).to include("WARN -- : :logger patch enabled but `enable_logs` is turned off - skipping applying patch") | ||
| end | ||
| end | ||
|
|
||
| context "when enable_logs is set to true but logger patch is not enabled" do | ||
| before do | ||
| perform_basic_setup do |config| | ||
| config.enable_logs = true | ||
| end | ||
| end | ||
|
|
||
| it "does not send log using stdlib logger" do | ||
| expect { | ||
| logger.send(:info, "Hello World") | ||
| }.to output(/Hello World/).to_stdout | ||
|
|
||
| expect(sentry_logs).to be_empty | ||
| end | ||
| end | ||
|
|
||
| context "when enable_logs is set to true and logger patch is set" do | ||
| before do | ||
| perform_basic_setup do |config| | ||
| config.max_log_events = 1 | ||
| config.enable_logs = true | ||
| config.enabled_patches = [:redis, :puma, :http, :logger] | ||
| end | ||
| end | ||
|
|
||
| ["info", "warn", "error", "fatal"].each do |level| | ||
| describe "##{level}" do | ||
| it "send logs using stdlib logger" do | ||
| expect { | ||
| logger.send(level, "Hello World") | ||
| }.to output(/Hello World/).to_stdout | ||
|
|
||
| expect(sentry_logs).to_not be_empty | ||
|
|
||
| log_event = sentry_logs.last | ||
|
|
||
| expect(log_event[:level]).to eql(level) | ||
| expect(log_event[:body]).to eql("Hello World") | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also needs a
SimpleCov.command_namesince it is now isolated