class PhobosDBCheckpoint::Middleware::Logger
Constants
- CONTENT_LENGTH
- HTTP_VERSION
- PATH_INFO
- QUERY_STRING
- RACK_LOGGER
- REQUEST_METHOD
- SINATRA_ERROR
Public Class Methods
new(app, options = {})
click to toggle source
# File lib/phobos_db_checkpoint/middleware/logger.rb, line 16 def initialize(app, options = {}) @app = app Phobos.configure(options.fetch(:config, 'config/phobos.yml')) Phobos.config.logger.file = options.fetch(:log_file, 'log/api.log') Phobos.configure_logger ActiveRecord::Base.logger = Phobos.logger end
Public Instance Methods
call(request_env)
click to toggle source
# File lib/phobos_db_checkpoint/middleware/logger.rb, line 24 def call(request_env) began_at = Time.now request_env[RACK_LOGGER] = Phobos.logger status, header, body = @app.call(request_env) header = Rack::Utils::HeaderHash.new(header) body = Rack::BodyProxy.new(body) do log(request_env, status, header, began_at) end [status, header, body] end
Private Instance Methods
extract_content_length(headers)
click to toggle source
# File lib/phobos_db_checkpoint/middleware/logger.rb, line 64 def extract_content_length(headers) (value = headers[CONTENT_LENGTH]) || return value.to_s == '0' ? nil : value end
extract_path(request_env)
click to toggle source
# File lib/phobos_db_checkpoint/middleware/logger.rb, line 60 def extract_path(request_env) "#{request_env[PATH_INFO]}#{request_env[QUERY_STRING].empty? ? '' : "?#{request_env[QUERY_STRING]}"} #{request_env[HTTP_VERSION]}" end
log(request_env, status, header, began_at)
click to toggle source
# File lib/phobos_db_checkpoint/middleware/logger.rb, line 37 def log(request_env, status, header, began_at) error = request_env[SINATRA_ERROR] message = { remote_address: request_env['HTTP_X_FORWARDED_FOR'] || request_env['REMOTE_ADDR'], remote_user: request_env['REMOTE_USER'], request_method: request_env[REQUEST_METHOD], path: extract_path(request_env), status: status.to_s[0..3], content_length: extract_content_length(header), request_time: "#{Time.now - began_at}s" } if error Phobos.logger.error(message.merge( exception_class: error.class.to_s, exception_message: error.message, backtrace: error.backtrace )) else Phobos.logger.info(message) end end