class SharkOnLambda::Middleware::LambdaLogger

Attributes

logger[R]

Public Class Methods

new(app, logger: SharkOnLambda.logger) click to toggle source
Calls superclass method SharkOnLambda::Middleware::Base::new
# File lib/shark_on_lambda/middleware/lambda_logger.rb, line 8
def initialize(app, logger: SharkOnLambda.logger)
  super(app)
  @logger = logger
end

Public Instance Methods

call!(env) click to toggle source
# File lib/shark_on_lambda/middleware/lambda_logger.rb, line 13
def call!(env)
  start_time = Time.now
  response = app.call(env)
  duration = duration_in_ms(start_time, Time.now)

  if logger.info?
    log_request(env: env, response: response, duration: duration)
  end

  response
end

Private Instance Methods

body_size(body) click to toggle source
# File lib/shark_on_lambda/middleware/lambda_logger.rb, line 27
def body_size(body)
  size = 0
  body.each { |chunk| size += chunk.bytesize }
  size
end
duration_in_ms(start_time, end_time) click to toggle source
# File lib/shark_on_lambda/middleware/lambda_logger.rb, line 45
def duration_in_ms(start_time, end_time)
  duration = (end_time - start_time) * 1000
  duration.abs.floor(3)
end
log_request(env:, response:, duration:) click to toggle source
# File lib/shark_on_lambda/middleware/lambda_logger.rb, line 33
def log_request(env:, response:, duration:)
  log_object = {
    url: env['PATH_INFO'],
    method: env['REQUEST_METHOD'],
    params: env.fetch('action_dispatch.request.parameters', {}),
    status: response[0],
    length: body_size(response[2]),
    duration: "#{duration} ms"
  }
  logger.info log_object.to_json
end