class Racknga::Middleware::Log

This is a middleware that puts access logs to groonga database. It may useful for OLAP (OnLine Analytical Processing).

Usage:

use Racnkga::Middleware::Log, :database_path => "var/log/db"
run YourApplication

@see Racknga::LogDatabase

Constants

LOGGER

Public Class Methods

new(application, options={}) click to toggle source

@option options [String] :database_path the database path to be stored caches.

# File lib/racknga/middleware/log.rb, line 36
def initialize(application, options={})
  @application = application
  @options = Utils.normalize_options(options || {})
  database_path = @options[:database_path]
  raise ArgumentError, ":database_path is missing" if database_path.nil?
  @database = LogDatabase.new(database_path)
  @logger = Logger.new(@database)
end

Public Instance Methods

call(environment) click to toggle source

For Rack.

# File lib/racknga/middleware/log.rb, line 46
def call(environment)
  environment[LOGGER] = @logger

  start_time = Time.now
  status, headers, body = @application.call(environment)
  end_time = Time.now

  request = Rack::Request.new(environment)
  log(start_time, end_time, request, status, headers, body)

  [status, headers, body]
end
close_database() click to toggle source

close the cache database.

# File lib/racknga/middleware/log.rb, line 65
def close_database
  @database.close_database
end
ensure_database() click to toggle source

ensures creating cache database.

# File lib/racknga/middleware/log.rb, line 60
def ensure_database
  @database.ensure_database
end

Private Instance Methods

log(start_time, end_time, request, status, headers, body) click to toggle source
# File lib/racknga/middleware/log.rb, line 70
def log(start_time, end_time, request, status, headers, body)
  request_time = end_time - start_time
  runtime = headers["X-Runtime"]
  runtime_in_float = nil
  runtime_in_float = runtime.to_f if runtime
  length = headers["Content-Length"] || "-"
  length = "-" if length == "0"
  format = "%s - %s [%s] \"%s %s %s\" %s %s \"%s\" \"%s\" %s %0.8f"
  message = format % [request.ip || "-",
                      request.env["REMOTE_USER"] || "-",
                      end_time.dup.utc.strftime("%d/%b/%Y:%H:%M:%S %z"),
                      request.request_method,
                      request.fullpath,
                      request.env["SERVER_PROTOCOL"] || "-",
                      status.to_s[0..3],
                      length,
                      request.env["HTTP_REFERER"] || "-",
                      request.user_agent || "-",
                      runtime || "-",
                      request_time]
  @logger.log("access",
              request.fullpath,
              :message => message,
              :user_agent => request.user_agent,
              :runtime => runtime_in_float,
              :request_time => request_time)
end