1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Pluggable Log Formatter (#4093)

* Rename log_format to log_formatter

Rails provides configuration options with this name.

* Pluggable log formatter instance
This commit is contained in:
Andrew Babichev 2019-02-11 22:59:52 +01:00 committed by Mike Perham
parent e6fefdc5df
commit db64467fc1
5 changed files with 39 additions and 25 deletions

View file

@ -1,24 +1,30 @@
# Welcome to Sidekiq 6.0!
Sidekiq 6.0 contains some breaking changes which streamline proper operation
of Sidekiq. It also drops support for EOL versions of Ruby and Rails.
of Sidekiq. It also drops support for EOL versions of Ruby and Rails.
## What's New
This release has major breaking changes. Read and test carefully in production.
- Logging has been redesigned to allow pluggable formats and several
- Logging has been redesigned to allow pluggable formatters and several
formats ship with Sidekiq:
* default - your typical output on macOS
* json - a new format with key=value pairs for search indexing
* heroku - a Heroku-specific formatter
* json - a new format with key=value pairs for search indexing
Sidekiq will enable the best formatter for the detected environment but
you can override it by configuring the log format explicitly:
you can override it by configuring the log formatter explicitly:
```
Sidekiq.configure_server do |config|
config.log_format = :json # or nil for default
config.log_formatter = AcmeCorp::PlainLogFormatter.new
# Sidekiq::Logger::Formatters::Pretty.new (default)
# Sidekiq::Logger::Formatters::WhitoutTimestamp.new
# Sidekiq::Logger::Formatters::JSON.new
end
```
- **Remove the daemonization, logfile and pidfile command line arguments**.
I've [noted for years](https://www.mikeperham.com/2014/09/22/dont-daemonize-your-daemons/)
how modern services should be managed with a proper init system.

View file

@ -174,12 +174,21 @@ module Sidekiq
def self.load_json(string)
JSON.parse(string)
end
def self.dump_json(object)
JSON.generate(object)
end
class << self
attr_accessor :log_format
def self.log_formatter
@log_formatter ||= if ENV['DYNO']
Sidekiq::Logger::Formatters::WithoutTimestamp.new
else
Sidekiq::Logger::Formatters::Pretty.new
end
end
def self.log_formatter=(log_formatter)
@log_formatter = log_formatter
end
def self.logger

View file

@ -34,7 +34,9 @@ module Sidekiq
# test coverage of Sidekiq::CLI are welcomed.
def run
boot_system
print_banner if environment == 'development' && $stdout.tty? && Sidekiq.log_format == nil
if environment == 'development' && $stdout.tty? && Sidekiq.log_formatter.is_a?(Sidekiq::Logger::Formatters::Pretty)
print_banner
end
self_read, self_write = IO.pipe
sigs = %w(INT TERM TTIN TSTP)

View file

@ -9,14 +9,7 @@ module Sidekiq
def initialize(*args)
super
formatter_class = case Sidekiq.log_format
when :json
Formatters::JSON
else
ENV['DYNO'] ? Formatters::WithoutTimestamp : Formatters::Pretty
end
self.formatter = formatter_class.new
self.formatter = Sidekiq.log_formatter
end
def with_context(hash)

View file

@ -8,30 +8,34 @@ class TestLogger < Minitest::Test
@output = StringIO.new
@logger = Sidekiq::Logger.new(@output)
Sidekiq.log_formatter = nil
Thread.current[:sidekiq_context] = nil
Thread.current[:sidekiq_tid] = nil
end
def teardown
Sidekiq.log_formatter = nil
Thread.current[:sidekiq_context] = nil
Thread.current[:sidekiq_tid] = nil
end
def test_format_selection
assert_kind_of Sidekiq::Logger::Formatters::Pretty, Sidekiq::Logger.new(STDOUT).formatter
def test_default_log_formatter
assert_kind_of Sidekiq::Logger::Formatters::Pretty, Sidekiq::Logger.new(@output).formatter
end
def test_heroku_log_formatter
begin
ENV['DYNO'] = 'dyno identifier'
assert_kind_of Sidekiq::Logger::Formatters::WithoutTimestamp, Sidekiq::Logger.new(STDOUT).formatter
assert_kind_of Sidekiq::Logger::Formatters::WithoutTimestamp, Sidekiq::Logger.new(@output).formatter
ensure
ENV['DYNO'] = nil
end
end
begin
Sidekiq.log_format = :json
assert_kind_of Sidekiq::Logger::Formatters::JSON, Sidekiq::Logger.new(STDOUT).formatter
ensure
Sidekiq.log_format = nil
end
def test_json_log_formatter
Sidekiq.log_formatter = Sidekiq::Logger::Formatters::JSON.new
assert_kind_of Sidekiq::Logger::Formatters::JSON, Sidekiq::Logger.new(@output).formatter
end
def test_with_context