Sha256: 9383e567aee8496cffd76f897bd3bd625bf378f19cf246ef5bfa818cc5c56527

Contents?: true

Size: 855 Bytes

Versions: 16

Compression:

Stored size: 855 Bytes

Contents

require 'benchmark'
require 'securerandom'
require 'logger'

module Attune
  class JsonLogger < Faraday::Middleware
    def initialize app, logger=nil
      super(app)
      @logger = logger || Logger.new(STDERR)
    end
    def call(env)
      time = (Time.now.to_f * 1000).to_i
      response = nil
      elapsed_time = Benchmark.realtime do
        response = @app.call(env)
      end
      log(
        ref: nil,
        v: 1,
        protocol: env[:url].scheme,
        host: env[:url].host,
        path: env[:url].path,
        t: time,
        r_id: SecureRandom.uuid,
        status: response.status,
        ua: env[:request_headers]['User-Agent'],
        method: env[:method],
        perf: {
          total: elapsed_time * 1000
        }
      )
      response
    end
    def log(data)
      @logger.info JSON.dump(data)
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
attune-1.0.21 lib/attune/json_logger.rb
attune-1.0.20 lib/attune/json_logger.rb
attune-1.0.19 lib/attune/json_logger.rb
attune-1.0.18 lib/attune/json_logger.rb
attune-1.0.17 lib/attune/json_logger.rb
attune-1.0.16 lib/attune/json_logger.rb
attune-1.0.15 lib/attune/json_logger.rb
attune-1.0.14 lib/attune/json_logger.rb
attune-1.0.13 lib/attune/json_logger.rb
attune-1.0.12 lib/attune/json_logger.rb
attune-1.0.11 lib/attune/json_logger.rb
attune-1.0.10 lib/attune/json_logger.rb
attune-1.0.9 lib/attune/json_logger.rb
attune-1.0.7 lib/attune/json_logger.rb
attune-1.0.6 lib/attune/json_logger.rb
attune-1.0.5 lib/attune/json_logger.rb