Sha256: 0fb5bb4b2d56afb182b5547a64e738dd50435e13fcd37a51577edec21233a659

Contents?: true

Size: 1.61 KB

Versions: 63

Compression:

Stored size: 1.61 KB

Contents

module Katello
  module Logging
    def self.time(message, data: {}, logger: Rails.logger, level: :info)
      start = Time.now
      data[:success] = true

      begin
        yield(data)
      rescue => e
        data[:success] = false
        raise e
      ensure
        data[:duration] = ((Time.now - start) * 1000).truncate(2)
        data_string = data.map { |k, v| "#{k}=#{v}" }.join(' ')
        logger.send(level, "#{message} #{data_string}")
      end
    end

    class Timer
      def initialize(key = "default")
        @key = key
        Thread.current[:timers] ||= {}
        Thread.current[:timers][key] = self
        self.start
      end

      def start
        Rails.logger.info "Timer #{@key} already started; resetting start time" if @start_time
        Rails.logger.info "Timer #{@key} starting at #{Time.now}"
        @start_time = Time.now
        self
      end

      def stop
        fail ::StandardError, "Timer #{@key} is not started" unless @start_time
        duration = (Time.now - @start_time).truncate(2)
        @start_time = nil
        Rails.logger.info "Timer #{@key} stopping at #{Time.now}: #{duration} sec"
      end

      def log(msg = nil)
        duration = (Time.now - @start_time).truncate(2)
        Rails.logger.info ["Timer #{@key} running at #{Time.now}", msg, "#{duration} sec"].compact.join(': ')
      end

      def self.find_by_key(key)
        if Thread.current&.[](:timers)&.[](key)
          Thread.current[:timers][key]
        else
          Rails.logger.warn "Timer #{key} not found on current thread; creating a new timer"
          self.new(key)
        end
      end
    end
  end
end

Version data entries

63 entries across 63 versions & 1 rubygems

Version Path
katello-4.15.0.rc2 app/lib/katello/logging.rb
katello-4.15.0.rc1 app/lib/katello/logging.rb
katello-4.14.1 app/lib/katello/logging.rb
katello-4.14.0 app/lib/katello/logging.rb
katello-4.14.0.rc3 app/lib/katello/logging.rb
katello-4.14.0.rc2 app/lib/katello/logging.rb
katello-4.14.0.rc1.1 app/lib/katello/logging.rb
katello-4.14.0.rc1 app/lib/katello/logging.rb
katello-4.13.1 app/lib/katello/logging.rb
katello-4.13.0 app/lib/katello/logging.rb
katello-4.12.1 app/lib/katello/logging.rb
katello-4.13.0.rc1 app/lib/katello/logging.rb
katello-4.12.0 app/lib/katello/logging.rb
katello-4.12.0.rc3 app/lib/katello/logging.rb
katello-4.12.0.rc2 app/lib/katello/logging.rb
katello-4.12.0.rc1 app/lib/katello/logging.rb
katello-4.11.1 app/lib/katello/logging.rb
katello-4.11.0 app/lib/katello/logging.rb
katello-4.11.0.rc2 app/lib/katello/logging.rb
katello-4.11.0.rc1 app/lib/katello/logging.rb