class EcsLogging::Middleware

Public Class Methods

new(app, logdev) click to toggle source
# File lib/ecs_logging/middleware.rb, line 25
def initialize(app, logdev)
  @app = app
  @logger = Logger.new(logdev)
end

Public Instance Methods

call(env) click to toggle source
# File lib/ecs_logging/middleware.rb, line 30
def call(env)
  status, headers, body = @app.call(env)
  body = BodyProxy.new(body) { log(env, status, headers) }
  [status, headers, body]
end

Private Instance Methods

log(env, status, headers) click to toggle source
# File lib/ecs_logging/middleware.rb, line 38
def log(env, status, headers)
  req_method = env['REQUEST_METHOD']
  path = env['PATH_INFO']
  message = "#{req_method} #{path}"

  severity = status >= 500 ? Logger::ERROR : Logger::INFO

  extras = {
    client: { address: env["REMOTE_ADDR"] },
    http: { request: { method: req_method } },
    url: {
      domain: env['HTTP_HOST'],
      path: path,
      port: env['SERVER_PORT'],
      scheme: env['HTTPS'] == 'on' ? 'https' : 'http'
    }
  }

  if content_length = env["CONTENT_LENGTH"]
    extras[:http][:request][:'body.bytes'] = content_length
  end

  if user_agent = env['HTTP_USER_AGENT']
    extras[:user_agent] = { original: user_agent }
  end

  @logger.add(severity, message, **extras)
end