mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
3845832c20
* Check Config File Existence (#4054) * Check config file existence * Eager config file check * Parse expanded path to default sidekiq.yml config file in Rails app * Cleanup * Add minitest-around * Extract context from formatter * Add JSON logger formatter * Adjust job logger to handle elapsed time within context * Add tid test * Rename processor logger * Enforce global state reset in logging tests * Add warning about upcoming refactoring to Sidekiq::Logging * Replace around hook with explicit stub inside test It's implemented with fibers, which means Thread.current returns different values in JRuby. * Fix typo * Concise JSON formatter keys * Add logger_formatter option * Shift context from array of strings to hash Allows more flexibly format context in the different formatters. * Adjust warning message regarding context type change * Add "Formatter" suffix to classes * Fix CLI specs * Replace Sidekiq::Logging with Sidekiq::Logger * Namespace logger formatters * Remove rails 4 appraisal
70 lines
1.7 KiB
Ruby
70 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'logger'
|
|
require 'time'
|
|
|
|
module Sidekiq
|
|
class Logger < ::Logger
|
|
|
|
def initialize(*args)
|
|
super
|
|
|
|
formatter_class = case Sidekiq.logger_formatter
|
|
when :json
|
|
Formatters::JSON
|
|
else
|
|
ENV['DYNO'] ? Formatters::WithoutTimestamp : Formatters::Pretty
|
|
end
|
|
|
|
self.formatter = formatter_class.new
|
|
end
|
|
|
|
def tid
|
|
Thread.current['sidekiq_tid'] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
|
|
end
|
|
|
|
def context
|
|
Thread.current[:sidekiq_context] ||= {}
|
|
end
|
|
|
|
def with_context(hash)
|
|
context.merge!(hash)
|
|
yield
|
|
ensure
|
|
hash.keys.each { |key| context.delete(key) }
|
|
end
|
|
|
|
module Formatters
|
|
class Pretty < Logger::Formatter
|
|
def call(severity, time, program_name, message)
|
|
"#{time.utc.iso8601(3)} #{::Process.pid} TID-#{Sidekiq.logger.tid}#{format_context(Sidekiq.logger.context)} #{severity}: #{message}\n"
|
|
end
|
|
|
|
private
|
|
|
|
def format_context(context)
|
|
' ' + context.compact.map { |k, v| "#{k.upcase}=#{v}" }.join(' ') if context.any?
|
|
end
|
|
end
|
|
|
|
class WithoutTimestamp < Pretty
|
|
def call(severity, time, program_name, message)
|
|
"#{::Process.pid} TID-#{Sidekiq.logger.tid}#{format_context(Sidekiq.logger.context)} #{severity}: #{message}\n"
|
|
end
|
|
end
|
|
|
|
class JSON < Logger::Formatter
|
|
def call(severity, time, program_name, message)
|
|
Sidekiq.dump_json(
|
|
ts: time.utc.iso8601(3),
|
|
pid: ::Process.pid,
|
|
tid: Sidekiq.logger.tid,
|
|
ctx: Sidekiq.logger.context,
|
|
sev: severity,
|
|
msg: message
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|