Skip to content

Commit eeaeb2a

Browse files
solnicEazybright
andauthored
Logger support (#2657)
* implement stblib logger support * update changelog * use existing patch to register stdlib logger * remove unused code * fix rubocop lint issues * Isolate sentry logger and std lib logger specs * Call super just once * Update CHANGELOG * Add SimpleCov command name for SentryLogger specs * Fix logger patch warning message configuration --------- Co-authored-by: Kolawole Ezekiel <[email protected]>
1 parent 32ca39e commit eeaeb2a

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
## Unreleased
22

3+
### Feature
4+
5+
- Support for `:logger` patch which enables sending logs to Sentry when `enabled_logs` is set to true ([#2657](https://github.com/getsentry/sentry-ruby/pull/2657))
6+
7+
Here's a sample config:
8+
9+
```ruby
10+
Sentry.init do |config|
11+
# ... your setup ...
12+
config.enable_logs = true
13+
config.enabled_patches = [:logger]
14+
end
15+
```
16+
17+
### Bug Fixes
318
- Skip creating `LogEventBuffer` if logging is not enabled ([#2652](https://github.com/getsentry/sentry-ruby/pull/2652))
419

520
## 5.25.0

sentry-ruby/lib/sentry-ruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,4 @@ def dependency_installed?(name)
690690
require "sentry/graphql"
691691
require "sentry/faraday"
692692
require "sentry/excon"
693+
require "sentry/std_lib_logger"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
module Sentry
4+
# Ruby Logger support Add commentMore actions
5+
# intercepts any logger instance and send the log to Sentry too.
6+
module StdLibLogger
7+
SEVERITY_MAP = {
8+
0 => :debug,
9+
1 => :info,
10+
2 => :warn,
11+
3 => :error,
12+
4 => :fatal
13+
}.freeze
14+
15+
def add(severity, message = nil, progname = nil, &block)
16+
result = super
17+
18+
return unless Sentry.initialized? && Sentry.get_current_hub
19+
20+
# exclude sentry SDK logs -- to prevent recursive log action,
21+
# do not process internal logs again
22+
if message.nil? && progname != Sentry::Logger::PROGNAME
23+
24+
# handle different nature of Ruby Logger class:
25+
# inspo from Sentry::Breadcrumb::SentryLogger
26+
if block_given?
27+
message = yield
28+
else
29+
message = progname
30+
end
31+
32+
message = message.to_s.strip
33+
34+
if !message.nil? && message != Sentry::Logger::PROGNAME && method = SEVERITY_MAP[severity]
35+
Sentry.logger.send(method, message)
36+
end
37+
end
38+
39+
result
40+
end
41+
end
42+
end
43+
44+
Sentry.register_patch(:logger) do |config|
45+
if config.enable_logs
46+
::Logger.prepend(Sentry::StdLibLogger)
47+
else
48+
config.sdk_logger.warn(":logger patch enabled but `enable_logs` is turned off - skipping applying patch")
49+
end
50+
end

sentry-ruby/spec/sentry/breadcrumb/sentry_logger_spec.rb renamed to sentry-ruby/spec/isolated/sentry_logger_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
# frozen_string_literal: true
22

3+
SimpleCov.command_name "SentryLogger"
4+
35
RSpec.describe "Sentry::Breadcrumbs::SentryLogger" do
46
before do
57
perform_basic_setup do |config|
68
config.breadcrumbs_logger = [:sentry_logger]
9+
config.enable_logs = true
10+
config.max_log_events = 1
11+
config.enabled_patches = [:logger]
712
end
813
end
914

@@ -100,4 +105,22 @@
100105
end
101106
end
102107
end
108+
109+
it "does not conflict with :logger patch" do
110+
logger = ::Logger.new(nil)
111+
112+
logger.info("Hello World")
113+
114+
expect(sentry_logs).to_not be_empty
115+
116+
log_event = sentry_logs.last
117+
118+
expect(log_event[:level]).to eql("info")
119+
expect(log_event[:body]).to eql("Hello World")
120+
121+
breadcrumb = breadcrumbs.peek
122+
123+
expect(breadcrumb.level).to eq("info")
124+
expect(breadcrumb.message).to eq("Hello World")
125+
end
103126
end
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# frozen_string_literal: true
2+
3+
SimpleCov.command_name "StdLibLogger"
4+
5+
RSpec.describe Sentry::StdLibLogger do
6+
let(:logger) { ::Logger.new($stdout) }
7+
8+
context "when logger patch is enabled but enable_logs is turned off" do
9+
it "logs a warning message" do
10+
string_io = StringIO.new
11+
12+
perform_basic_setup do |config|
13+
config.enable_logs = false
14+
config.enabled_patches = [:logger]
15+
config.sdk_logger = ::Logger.new(string_io)
16+
end
17+
18+
expect(string_io.string).to include("WARN -- : :logger patch enabled but `enable_logs` is turned off - skipping applying patch")
19+
end
20+
end
21+
22+
context "when enable_logs is set to true but logger patch is not enabled" do
23+
before do
24+
perform_basic_setup do |config|
25+
config.enable_logs = true
26+
end
27+
end
28+
29+
it "does not send log using stdlib logger" do
30+
expect {
31+
logger.send(:info, "Hello World")
32+
}.to output(/Hello World/).to_stdout
33+
34+
expect(sentry_logs).to be_empty
35+
end
36+
end
37+
38+
context "when enable_logs is set to true and logger patch is set" do
39+
before do
40+
perform_basic_setup do |config|
41+
config.max_log_events = 1
42+
config.enable_logs = true
43+
config.enabled_patches = [:redis, :puma, :http, :logger]
44+
end
45+
end
46+
47+
["info", "warn", "error", "fatal"].each do |level|
48+
describe "##{level}" do
49+
it "send logs using stdlib logger" do
50+
expect {
51+
logger.send(level, "Hello World")
52+
}.to output(/Hello World/).to_stdout
53+
54+
expect(sentry_logs).to_not be_empty
55+
56+
log_event = sentry_logs.last
57+
58+
expect(log_event[:level]).to eql(level)
59+
expect(log_event[:body]).to eql("Hello World")
60+
end
61+
end
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)