Skip to content

Commit

Permalink
Implement #silence logger functionality
Browse files Browse the repository at this point in the history
Our current implementation of the `#silence` method does not
actually silence anything. If we are implementing the method, we
should actually implement its functionality.
  • Loading branch information
unflxw committed Dec 19, 2024
1 parent 2468e74 commit d08a1ce
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changesets/fix---silence--implementation-for-logger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
bump: patch
type: fix
---

Fix `#silence` implementation for `Appsignal::Logger`.
11 changes: 6 additions & 5 deletions lib/appsignal/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,16 +192,17 @@ def clear_tags!

# When using ActiveSupport::TaggedLogging without the broadcast feature,
# the passed logger is required to respond to the `silence` method.
# In our case it behaves as the broadcast feature of the Rails logger, but
# we don't have to check if the parent logger has the `silence` method defined
# as our logger directly inherits from Ruby base logger.
#
# Links:
# Reference links:
#
# - https://github.com/rails/rails/blob/e11ebc04cfbe41c06cdfb70ee5a9fdbbd98bb263/activesupport/lib/active_support/logger.rb#L60-L76
# - https://github.com/rails/rails/blob/e11ebc04cfbe41c06cdfb70ee5a9fdbbd98bb263/activesupport/lib/active_support/logger_silence.rb
def silence(_severity = ERROR, &block)
def silence(severity = ERROR, &block)
previous_level = @level
@level = severity
block.call(self)
ensure
@level = previous_level
end

private
Expand Down
37 changes: 37 additions & 0 deletions spec/lib/appsignal/logger_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,43 @@
expect(num).to eq(2)
expect(Appsignal::Extension).not_to receive(:log)
end

it "silences the logger up to, but not including, the given level" do
# Expect not to receive info
expect(Appsignal::Extension).not_to receive(:log)
.with("group", 3, 0, "Log message", instance_of(Appsignal::Extension::Data))

# Expect to receive warn
expect(Appsignal::Extension).to receive(:log)
.with("group", 5, 0, "Log message", instance_of(Appsignal::Extension::Data))

logger.silence(::Logger::WARN) do
logger.info("Log message")
logger.warn("Log message")
end
end

it "silences the logger to error level by default" do
# Expect not to receive debug, info or warn
[2, 3, 5].each do |severity|
expect(Appsignal::Extension).not_to receive(:log)
.with("group", severity, 0, "Log message", instance_of(Appsignal::Extension::Data))
end

# Expect to receive error and fatal
[6, 7].each do |severity|
expect(Appsignal::Extension).to receive(:log)
.with("group", severity, 0, "Log message", instance_of(Appsignal::Extension::Data))
end

logger.silence do
logger.debug("Log message")
logger.info("Log message")
logger.warn("Log message")
logger.error("Log message")
logger.fatal("Log message")
end
end
end

[
Expand Down

0 comments on commit d08a1ce

Please sign in to comment.