Sha256: de7425ab60df0d07cc75281c3d67b76aa1192062f58ef196b536e3a6feae0aec

Contents?: true

Size: 843 Bytes

Versions: 38

Compression:

Stored size: 843 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, "#{Roqua.appname}.#{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.name, message: e.message}.merge(options))
      raise
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
roqua-support-0.1.27 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.26 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.25 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.24 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.23 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.22 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.21 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.20 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.19 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.18 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.17 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.16 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.15 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.14 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.13 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.12 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.11 lib/roqua/support/log_wrapper.rb
roqua-support-0.1.10 lib/roqua/support/log_wrapper.rb