Sha256: a9e28e6c0746904d91d09a44da249cd8c898eceebe86686fb821e3cdf3645d75

Contents?: true

Size: 1.28 KB

Versions: 3

Compression:

Stored size: 1.28 KB

Contents

module Jersey
  class BaseLogger
    attr_accessor :stream, :defaults

    def initialize(opts = {})
      @stream   = opts.fetch(:stream, $stdout)
      @defaults = opts.fetch(:defaults, {})
    end

    def <<(data)
      @defaults.merge!(data)
    end

    def reset!(key = nil)
      if key
        @defaults.delete(key)
        @defaults.delete(key.to_sym)
      else
        @defaults.clear
      end
    end

    def log(data, &block)
      log_to_stream(stream, @defaults.merge(data), &block)
    end

    private
    def log_to_stream(stream, data, &block)
      data.merge!(request_data || {})
      unless block
        str = unparse(data.merge(now: Time.now))
        stream.print(str + "\n")
      else
        data = data.dup
        start = Time.now
        log_to_stream(stream, data.merge(at: "start"))
        begin
          res = yield
          log_to_stream(stream, data.merge(
            at: "finish", elapsed: (Time.now - start).to_f))
          res
        rescue
          log_to_stream(stream, data.merge(
            at: "exception", elapsed: (Time.now - start).to_f))
          raise
        end
      end
    end

    def unparse
      raise "Need to subclass and implement #unparse"
    end

    def request_data
      RequestStore[:log] if defined? RequestStore
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
jersey-0.2.0 lib/jersey/logging/base_logger.rb
jersey-0.1.0 lib/jersey/logging/base_logger.rb
jersey-0.0.3 lib/jersey/logging/base_logger.rb