From b491ecac31b45a1da9f24b10d300c76f0f77355f Mon Sep 17 00:00:00 2001 From: Benjamin Neff Date: Sun, 24 May 2015 19:34:04 +0200 Subject: [PATCH] wrap the sidekiq logger to add the context info again closes #5988 --- Changelog.md | 1 + app/workers/base.rb | 10 ++++++---- config/initializers/sidekiq.rb | 20 +++++++++++++++++++- config/logging.rb | 5 +++-- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Changelog.md b/Changelog.md index 0f8d3c4f3..5ca5ab15b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -15,6 +15,7 @@ * Refactored the Logger to add basic logrotating and more useful timestamps [#5975](https://github.com/diaspora/diaspora/pull/5975) * Gracefully handle mailer failures if a like is already deleted again [#5983](https://github.com/diaspora/diaspora/pull/5983) * Ensure posts have an author [#5986](https://github.com/diaspora/diaspora/pull/5986) +* Improve the logging messages of Sidekiq messages [#5988](https://github.com/diaspora/diaspora/pull/5988) ## Bug fixes * Disable auto follow back on aspect deletion [#5846](https://github.com/diaspora/diaspora/pull/5846) diff --git a/app/workers/base.rb b/app/workers/base.rb index 7a603b8ae..e33135007 100644 --- a/app/workers/base.rb +++ b/app/workers/base.rb @@ -8,10 +8,6 @@ module Workers sidekiq_options backtrace: (bt = AppConfig.environment.sidekiq.backtrace.get) && bt.to_i, retry: (rt = AppConfig.environment.sidekiq.retry.get) && rt.to_i - def logger - @logger ||= ::Logging::Logger[self] - end - # In the long term we need to eliminate the cause of these def suppress_annoying_errors(&block) yield @@ -40,5 +36,11 @@ module Workers "duplicate key in table 'posts'" ).any? {|index| e.message.include? index } end + + private + + def logger + @logger ||= ::Logging::Logger[self] + end end end diff --git a/config/initializers/sidekiq.rb b/config/initializers/sidekiq.rb index dbcba8bbb..9709cc27a 100644 --- a/config/initializers/sidekiq.rb +++ b/config/initializers/sidekiq.rb @@ -28,7 +28,25 @@ Sidekiq.configure_server do |config| # Make sure each Sidekiq process has its own sequence of UUIDs UUID.generator.next_sequence - Sidekiq.logger = Logging.logger[Sidekiq] + # wrap the logger to add the sidekiq job context to the log + class SidekiqLogger < SimpleDelegator + SPACE = " " + + # only info is used with context + def info(data=nil) + return false if Logger::Severity::INFO < level + data = yield if data.nil? && block_given? + __getobj__.info("#{context}#{data}") + end + + # from sidekiq/logging.rb + def context + c = Thread.current[:sidekiq_context] + "#{c.join(SPACE)}: " if c && c.any? + end + end + + Sidekiq::Logging.logger = SidekiqLogger.new(Logging.logger[Sidekiq]) end Sidekiq.configure_client do |config| diff --git a/config/logging.rb b/config/logging.rb index b58db314e..ae5d536fd 100644 --- a/config/logging.rb +++ b/config/logging.rb @@ -6,7 +6,8 @@ Logging::Rails.configure do |config| Logging.format_as :inspect # The default layout used by the appenders. - layout = Logging.layouts.pattern(pattern: "[%d] %-5l %c : %m\n") + pattern = "[%d] %-5l PID-%p TID-%t %c: %m\n" + layout = Logging.layouts.pattern(pattern: pattern) # Setup a color scheme called 'bright' than can be used to add color codes # to the pattern layout. Color schemes should only be used with appenders @@ -30,7 +31,7 @@ Logging::Rails.configure do |config| Logging.appenders.stdout("stdout", auto_flushing: true, layout: Logging.layouts.pattern( - pattern: "[%d] %-5l %c : %m\n", + pattern: pattern, color_scheme: "bright" ) ) if config.log_to.include? "stdout"