Sha256: 4b27c4b274f0a43679ed65ac3925ddbedb58e2bd637eea23c2142d5e448f9275

Contents?: true

Size: 1.14 KB

Versions: 14

Compression:

Stored size: 1.14 KB

Contents

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

    def self.initialize_logger(log_target = STDOUT)
      oldlogger = @logger
      @logger = Logger.new(log_target)
      @logger.level = Logger::INFO
      @logger.formatter = Pretty.new
      oldlogger.close if oldlogger && !$TESTING # don't want to close testing's STDOUT logging
      @logger
    end

    def self.logger
      @logger || initialize_logger
    end

    def self.logger=(log)
      @logger = (log ? log : Logger.new('/dev/null'))
    end

    def logger
      Sidekiq::Logging.logger
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
sidekiq-2.17.4 lib/sidekiq/logging.rb
sidekiq-2.17.3 lib/sidekiq/logging.rb
sidekiq-2.17.2 lib/sidekiq/logging.rb
sidekiq-2.17.1 lib/sidekiq/logging.rb
sidekiq-2.17.0 lib/sidekiq/logging.rb
sidekiq-2.16.1 lib/sidekiq/logging.rb
sidekiq-2.16.0 lib/sidekiq/logging.rb
sidekiq-2.15.2 lib/sidekiq/logging.rb
sidekiq-2.15.1 lib/sidekiq/logging.rb
sidekiq-2.15.0 lib/sidekiq/logging.rb
sidekiq-2.14.1 lib/sidekiq/logging.rb
sidekiq-2.14.0 lib/sidekiq/logging.rb
sidekiq-2.13.1 lib/sidekiq/logging.rb
sidekiq-2.13.0 lib/sidekiq/logging.rb