class Napa::Middleware::Logger

Public Class Methods

new(app) click to toggle source
# File lib/napa/middleware/logger.rb, line 8
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/napa/middleware/logger.rb, line 12
def call(env)
  # log the request
  Napa::Logger.logger.info format_request(env)

  # process the request
  status, headers, body = @app.call(env)

  # log the response
  Napa::Logger.logger.debug format_response(status, headers, body)

  # return the results
  [status, headers, body]
ensure
  # Clear the transaction id after each request
  Napa::LogTransaction.clear
end

Private Instance Methods

format_request(env) click to toggle source
# File lib/napa/middleware/logger.rb, line 31
def format_request(env)
  request = Rack::Request.new(env)
  params  = request.params

  begin
    params = JSON.parse(request.body.read) if env['CONTENT_TYPE'] == 'application/json'
  rescue
    # do nothing, params is already set
  end

  request_data = {
    method:           request.request_method,
    path:             request.path_info,
    query:            filtered_query_string(request.query_string),
    host:             Napa::Identity.hostname,
    pid:              Napa::Identity.pid,
    revision:         Napa::Identity.revision,
    params:           filtered_parameters(params),
    remote_ip:        request.ip
  }
  request_data[:user_id] = current_user.try(:id) if defined?(current_user)
  { request: request_data }
end
format_response(status, headers, body) click to toggle source
# File lib/napa/middleware/logger.rb, line 55
def format_response(status, headers, body)
  response_body = nil
  begin
    response_body = body.respond_to?(:body) ? body.body.map { |r| r } : nil
  rescue
    response_body = body.inspect
  end

  Napa::Logger.response(status, headers, response_body)
end