Class: Rack::LtsvLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/ltsv_logger.rb

Constant Summary collapse

DEFAULT_PARAMS_PROC =
Proc.new do |env, status, headers, body, began_at|
  now = Time.now
  reqtime = now.instance_eval { to_i + (usec/1000000.0) } - began_at
  {
    time: now.iso8601,
    pid: Process.pid,
    host: env["REMOTE_ADDR"] || "-",
    forwardedfor: env['HTTP_X_FORWARDED_FOR'] || "-",
    user: env["REMOTE_USER"] || "-",
    method: env["REQUEST_METHOD"],
    uri: env["PATH_INFO"],
    query: env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
    protocol: env["HTTP_VERSION"],
    status: ::Rack::LtsvLogger.extract_status(status),
    size: ::Rack::LtsvLogger.extract_content_length(headers),
    reqtime: "%0.6f" % reqtime,
  }
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, io = nil, **kwargs) ⇒ LtsvLogger

Returns a new instance of LtsvLogger.



24
25
26
27
28
29
# File 'lib/rack/ltsv_logger.rb', line 24

def initialize(app, io = nil, **kwargs)
  @app = app
  @io = io || $stdout
  @params_proc = kwargs[:params_proc] || DEFAULT_PARAMS_PROC
  @appends = kwargs.tap {|h| h.delete(:params_proc) } # old version compatibility
end

Class Method Details

.extract_content_length(headers) ⇒ Object



40
41
42
43
# File 'lib/rack/ltsv_logger.rb', line 40

def self.extract_content_length(headers)
  value = headers && headers['Content-Length'] or return '-'
  value.to_s == '0' ? '-' : value
end

.extract_status(status) ⇒ Object



45
46
47
# File 'lib/rack/ltsv_logger.rb', line 45

def self.extract_status(status)
  status.nil? ? "500" : status.to_s[0..2]
end

Instance Method Details

#call(env) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/rack/ltsv_logger.rb', line 31

def call(env)
  began_at = Time.now.instance_eval { to_i + (usec/1000000.0) }
  status, headers, body = @app.call(env)
ensure
  params = @params_proc.call(env, status, headers, body, began_at)
  @appends.each {|key, proc| params[key] = proc.call(env) } # old version compatibility
  @io.write ltsv(params)
end