public
Last active

  • Download Gist
superlogger.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
module Rack
class Superlogger
module LogProcessor
def self.find(type)
case type
when Class
type
when String, Symbol
const_get type.to_s.capitalize
else
raise ArgumentError, "Unexpected type class #{type.class}"
end
end
class Base
attr_reader :logger
def initialize(options)
@options = options
end
def process(env)
raise "Not implemented"
end
end
 
class Templated < Base
def initialize(options)
@logger = options.delete(:logger) or raise ArgumentError, "You must specify a logger"
@template = options.delete(:template) or raise ArgumentError, "You must specify a template"
super options
end
def process(env)
request = Rack::Request.new(env)
message = @template.dup
 
Rack::Superlogger::REQUEST_METHODS.each do |method_name|
env["rack.superlogger.data"][method_name.to_sym] = request.send(method_name.to_sym) if message.include?(":#{method_name}")
end
 
env["rack.superlogger.data"].each do |k, v|
message.gsub! ":#{k}", v.to_s
end
 
@logger.info message
end
end
end
REQUEST_METHODS = Rack::Request.public_instance_methods(false).
reject { |method_name| method_name =~ /[=\[]|content_length/ }.freeze
 
def initialize(app, options)
@app, @processor = app, LogProcessor::find(options.delete(:type)).new(options)
end
 
def call(env)
env["rack.superlogger.data"], env["rack.superlogger.raw_logger"] = {}, @processor.logger
before = Time.now.to_f
status, headers, body = @app.call(env)
duration = ((Time.now.to_f - before.to_f) * 1000).floor
env["rack.superlogger.data"][:duration] = duration.to_s
env["rack.superlogger.data"][:status] = status.to_s
env["rack.superlogger.data"][:content_length] = headers["Content-length"]
@processor.process env
[status, headers, body]
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.