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! # Welcome to Sidekiq 6.0!
Sidekiq 6.0 contains some breaking changes which streamline proper operation 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 ## What's New
This release has major breaking changes. Read and test carefully in production. 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: formats ship with Sidekiq:
* default - your typical output on macOS * default - your typical output on macOS
* json - a new format with key=value pairs for search indexing
* heroku - a Heroku-specific formatter * 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 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| 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 end
``` ```
- **Remove the daemonization, logfile and pidfile command line arguments**. - **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/) 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. how modern services should be managed with a proper init system.

View file

@ -174,12 +174,21 @@ module Sidekiq
def self.load_json(string) def self.load_json(string)
JSON.parse(string) JSON.parse(string)
end end
def self.dump_json(object) def self.dump_json(object)
JSON.generate(object) JSON.generate(object)
end end
class << self def self.log_formatter
attr_accessor :log_format @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 end
def self.logger def self.logger

View file

@ -34,7 +34,9 @@ module Sidekiq
# test coverage of Sidekiq::CLI are welcomed. # test coverage of Sidekiq::CLI are welcomed.
def run def run
boot_system 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 self_read, self_write = IO.pipe
sigs = %w(INT TERM TTIN TSTP) sigs = %w(INT TERM TTIN TSTP)

View file

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

View file

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