2012-05-15 22:44:35 -04:00
|
|
|
require 'time'
|
|
|
|
require 'logger'
|
|
|
|
|
|
|
|
module Sidekiq
|
|
|
|
module Logging
|
|
|
|
|
|
|
|
class Pretty < Logger::Formatter
|
|
|
|
# Provide a call() method that returns the formatted message.
|
|
|
|
def call(severity, time, program_name, message)
|
|
|
|
"#{time.utc.iso8601} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{context} #{severity}: #{message}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def context
|
|
|
|
c = Thread.current[:sidekiq_context]
|
|
|
|
c ? " #{c}" : ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.with_context(msg)
|
|
|
|
begin
|
|
|
|
Thread.current[:sidekiq_context] = msg
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
Thread.current[:sidekiq_context] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-12 14:27:27 -05:00
|
|
|
def self.initialize_logger(log_target = STDOUT)
|
2013-06-18 16:44:19 -04:00
|
|
|
oldlogger = @logger
|
2012-12-12 14:27:27 -05:00
|
|
|
@logger = Logger.new(log_target)
|
|
|
|
@logger.level = Logger::INFO
|
|
|
|
@logger.formatter = Pretty.new
|
2013-06-18 16:44:19 -04:00
|
|
|
oldlogger.close if oldlogger && !$TESTING # don't want to close testing's STDOUT logging
|
2012-12-12 14:27:27 -05:00
|
|
|
@logger
|
2012-12-11 06:32:22 -05:00
|
|
|
end
|
|
|
|
|
2012-05-15 22:44:35 -04:00
|
|
|
def self.logger
|
2012-12-12 14:27:27 -05:00
|
|
|
@logger || initialize_logger
|
2012-05-15 22:44:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.logger=(log)
|
|
|
|
@logger = (log ? log : Logger.new('/dev/null'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def logger
|
|
|
|
Sidekiq::Logging.logger
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|