Sha256: 164dd44ed089e5a0c602d8b465abb762e7884771404d60b42348f0c96f52841d

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

require 'sidekiq'

module Ezlog
  module Sidekiq
    class JobLogger
      include LogContextHelper

      def initialize(logger = ::Sidekiq.logger)
        @logger = logger
      end

      def call(job_hash, _queue)
        within_log_context(JobContext.from_job_hash(job_hash)) do
          begin
            @logger.info "#{job_hash['class']} started"
            benchmark { yield }
            @logger.info message: "#{job_hash['class']} finished"
          rescue Exception
            @logger.info message: "#{job_hash['class']} failed"
            raise
          end
        end
      end

      def with_job_hash_context(_job_hash, &_block)
        yield
      end

      def prepare(job_hash, &_block)
        old_log_level = @logger.level
        @logger.level = job_hash['log_level'] || @logger.level
        yield
      ensure
        @logger.level = old_log_level
      end

      private

      def benchmark
        start_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
        yield
      ensure
        end_time = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
        add_to_log_context duration_sec: (end_time - start_time).round(3)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ezlog-0.10.3 lib/ezlog/sidekiq/job_logger.rb