Skip to content

Commit

Permalink
Feat(logging): Allow disabling logging (#729)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhenrixon committed Jul 28, 2022
1 parent b67ec72 commit 3eeb85c
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Want to show me some ❤️ for the hard work I do on this gem? You can use the
- [raise](#raise)
- [reject](#reject)
- [replace](#replace)
- [Reschedule](#reschedule)
- [reschedule](#reschedule)
- [Custom Strategies](#custom-strategies)
- [3 Cleanup Dead Locks](#3-cleanup-dead-locks)
- [Debugging](#debugging)
Expand Down Expand Up @@ -610,6 +610,7 @@ This has been probably the most confusing part of this gem. People get really co
```ruby
SidekiqUniqueJobs.configure do |config|
config.enabled = !Rails.env.test?
config.logger_enabled = !Rails.env.test?
end
```

Expand Down Expand Up @@ -736,6 +737,7 @@ Configure SidekiqUniqueJobs in an initializer or the sidekiq initializer on appl
```ruby
SidekiqUniqueJobs.configure do |config|
config.logger = Sidekiq.logger # default, change at your own discretion
config.logger_enabled = true # default, disable for test environments
config.debug_lua = false # Turn on when debugging
config.lock_info = false # Turn on when debugging
config.lock_ttl = 600 # Expire locks after 10 minutes
Expand Down
5 changes: 5 additions & 0 deletions lib/sidekiq_unique_jobs/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module SidekiqUniqueJobs
:enabled,
:lock_prefix,
:logger,
:logger_enabled,
:locks,
:strategies,
:debug_lua,
Expand Down Expand Up @@ -91,6 +92,9 @@ class Config < ThreadSafeConfig
# @return [nil]
LOCK_TTL = nil
#
# @return [true,false] by default false (don't disable logger)
LOGGER_ENABLED = true
#
# @return [true] by default the gem is enabled
ENABLED = true
#
Expand Down Expand Up @@ -180,6 +184,7 @@ def self.default # rubocop:disable Metrics/MethodLength
ENABLED,
PREFIX,
Sidekiq.logger,
LOGGER_ENABLED,
LOCKS,
STRATEGIES,
DEBUG_LUA,
Expand Down
14 changes: 14 additions & 0 deletions lib/sidekiq_unique_jobs/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def logger
# @yield [String, Exception] the message or exception to use for log message
#
def log_debug(message_or_exception = nil, item = nil, &block)
return unless logging?

message = build_message(message_or_exception, item)
logger.debug(message, &block)
nil
Expand All @@ -45,6 +47,8 @@ def log_debug(message_or_exception = nil, item = nil, &block)
# @yield [String, Exception] the message or exception to use for log message
#
def log_info(message_or_exception = nil, item = nil, &block)
return unless logging?

message = build_message(message_or_exception, item)
logger.info(message, &block)
nil
Expand All @@ -60,6 +64,8 @@ def log_info(message_or_exception = nil, item = nil, &block)
# @yield [String, Exception] the message or exception to use for log message
#
def log_warn(message_or_exception = nil, item = nil, &block)
return unless logging?

message = build_message(message_or_exception, item)
logger.warn(message, &block)
nil
Expand All @@ -75,6 +81,8 @@ def log_warn(message_or_exception = nil, item = nil, &block)
# @yield [String, Exception] the message or exception to use for log message
#
def log_error(message_or_exception = nil, item = nil, &block)
return unless logging?

message = build_message(message_or_exception, item)
logger.error(message, &block)
nil
Expand All @@ -90,6 +98,8 @@ def log_error(message_or_exception = nil, item = nil, &block)
# @yield [String, Exception] the message or exception to use for log message
#
def log_fatal(message_or_exception = nil, item = nil, &block)
return unless logging?

message = build_message(message_or_exception, item)
logger.fatal(message, &block)

Expand Down Expand Up @@ -218,5 +228,9 @@ def fake_logger_context(_context)

yield
end

def logging?
SidekiqUniqueJobs.logging?
end
end
end
10 changes: 10 additions & 0 deletions lib/sidekiq_unique_jobs/sidekiq_unique_jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ def logger=(other)
config.logger = other
end

#
# Check if logging is enabled
#
#
# @return [true, false]
#
def logging?
config.logger_enabled
end

#
# Temporarily use another configuration and reset to the old config after yielding
#
Expand Down
45 changes: 45 additions & 0 deletions spec/sidekiq_unique_jobs/logging_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
.and(ending_with("(queue=#{queue} class=#{worker} jid=#{jid} lock_digest=#{digest})")),
)
end

context "when logging is disabled" do
it "logs nothing" do
SidekiqUniqueJobs.use_config(logger_enabled: false) do
log_debug(message, item)
expect(logger).not_to have_received(level)
end
end
end
end

describe "#log_info" do
Expand All @@ -45,6 +54,15 @@
.and(ending_with("(queue=#{queue} class=#{worker} jid=#{jid} lock_digest=#{digest})")),
)
end

context "when logging is disabled" do
it "logs nothing" do
SidekiqUniqueJobs.use_config(logger_enabled: false) do
log_info(message, item)
expect(logger).not_to have_received(level)
end
end
end
end

describe "#log_warn" do
Expand All @@ -57,6 +75,15 @@
.and(ending_with("(queue=#{queue} class=#{worker} jid=#{jid} lock_digest=#{digest})")),
)
end

context "when logging is disabled" do
it "logs nothing" do
SidekiqUniqueJobs.use_config(logger_enabled: false) do
log_warn(message, item)
expect(logger).not_to have_received(level)
end
end
end
end

describe "#log_error" do
Expand All @@ -69,6 +96,15 @@
.and(ending_with("(queue=#{queue} class=#{worker} jid=#{jid} lock_digest=#{digest})")),
)
end

context "when logging is disabled" do
it "logs nothing" do
SidekiqUniqueJobs.use_config(logger_enabled: false) do
log_error(message, item)
expect(logger).not_to have_received(level)
end
end
end
end

describe "#log_fatal" do
Expand All @@ -81,6 +117,15 @@
.and(ending_with("(queue=#{queue} class=#{worker} jid=#{jid} lock_digest=#{digest})")),
)
end

context "when logging is disabled" do
it "logs nothing" do
SidekiqUniqueJobs.use_config(logger_enabled: false) do
log_fatal(message, item)
expect(logger).not_to have_received(level)
end
end
end
end

describe "#logging_context" do
Expand Down

0 comments on commit 3eeb85c

Please sign in to comment.