Class: Tumugi::Plugin::Webhook::Logger

Inherits:
Faraday::Response::Middleware
  • Object
show all
Defined in:
lib/tumugi/plugin/webhook/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, logger) ⇒ Logger

Returns a new instance of Logger.



10
11
12
13
# File 'lib/tumugi/plugin/webhook/logger.rb', line 10

def initialize(app, logger)
  super(app)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/tumugi/plugin/webhook/logger.rb', line 8

def logger
  @logger
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
# File 'lib/tumugi/plugin/webhook/logger.rb', line 15

def call(env)
  logger.info { "#{env[:method].upcase} #{env[:url]}" }
  log_request(env)
  super
end

#log(headers, body) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/tumugi/plugin/webhook/logger.rb', line 35

def log(headers, body)
  logger.debug {
    JSON.generate(
      headers: headers,
      body: body
    )
  }
end

#log_request(env) ⇒ Object



27
28
29
# File 'lib/tumugi/plugin/webhook/logger.rb', line 27

def log_request(env)
  log(env[:request_headers], env[:body])
end

#log_response(env) ⇒ Object



31
32
33
# File 'lib/tumugi/plugin/webhook/logger.rb', line 31

def log_response(env)
  log(env[:response_headers], env[:body])
end

#log_response_status(status, &block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/tumugi/plugin/webhook/logger.rb', line 44

def log_response_status(status, &block)
  case status
  when 200..399
    logger.info(&block)
  else
    logger.error(&block)
  end
end

#on_complete(env) ⇒ Object



21
22
23
24
25
# File 'lib/tumugi/plugin/webhook/logger.rb', line 21

def on_complete(env)
  status = env[:status]
  log_response_status(status) { "HTTP #{status}" }
  log_response(env)
end