Sha256: 0c35f871f045ebfabb1383033b397b9d1649cd87a899bb5327486cd5c0487e72

Contents?: true

Size: 820 Bytes

Versions: 8

Compression:

Stored size: 820 Bytes

Contents

require 'json'

module Roqua
  class LogWrapper
    attr_reader :logger

    def initialize(logger)
      @logger = logger
    end

    def add(level, message, options = {})
      logger.send(level, "#{message} #{options.to_json}".strip)
    end

    [:fatal, :error, :warn, :info, :debug].each do |level|
      define_method(level) do |*args|
        add(level, *args)
      end
    end

    def lifecycle(message, options = {})
      started_at = Time.now.to_f
      info("#{message}:started", options)
      value = yield
      finished_at = Time.now.to_f
      duration = finished_at - started_at
      info("#{message}:finished", {duration: duration}.merge(options))
      value
    rescue => e
      error("#{message}:failed", {exception: e.class, message: e.message}.merge(options))
      raise
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
roqua-support-0.1.5 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.4 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.3 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.2.2 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.2.1 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.2 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.1 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.0 lib/roqua/support/log_wrapper.rb