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

72 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require "logger"
require "time"
module Sidekiq
module LogContext
def with_context(hash)
ctx.merge!(hash)
yield
ensure
hash.keys.each { |key| ctx.delete(key) }
end
def ctx
Thread.current[:sidekiq_context] ||= {}
end
end
class Logger < ::Logger
include LogContext
def initialize(*args)
super
self.formatter = Sidekiq.log_formatter
end
module Formatters
class Base < ::Logger::Formatter
def tid
Thread.current["sidekiq_tid"] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
end
def ctx
Thread.current[:sidekiq_context] ||= {}
end
def format_context
" " + ctx.compact.map { |k, v| "#{k}=#{v}" }.join(" ") if ctx.any?
end
end
class Pretty < Base
def call(severity, time, program_name, message)
"#{time.utc.iso8601(3)} pid=#{::Process.pid} tid=#{tid}#{format_context} #{severity}: #{message}\n"
end
end
class WithoutTimestamp < Pretty
def call(severity, time, program_name, message)
"pid=#{::Process.pid} tid=#{tid}#{format_context} #{severity}: #{message}\n"
end
end
class JSON < Base
def call(severity, time, program_name, message)
hash = {
ts: time.utc.iso8601(3),
pid: ::Process.pid,
tid: tid,
lvl: severity,
msg: message,
}
c = ctx
hash["ctx"] = c unless c.empty?
Sidekiq.dump_json(hash) << "\n"
end
end
end
end
end