Rails Middleware

Rails Middleware

  • app/middleware/dalta_logger.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#app/middleware/dalta_logger.rb
class DeltaLogger
def initialize(app, formatting_char = '=', log_level = "info")
@app = app
@formatting_char = formatting_char
@log_level = log_level
end
def call env
request_started_on = Time.now
@status, @headers, @response = @app.call(env)
request_ended_on = Time.now
Rails.logger.send(@log_level, @formatting_char * 50)
Rails.logger.send(@log_level, "Request delta time: #{request_ended_on - request_started_on} seconds.")
Rails.logger.send(@log_level, @formatting_char * 50)
[@status, @headers, @response]
end
end
  • config/application.rb
1
config.middleware.use "DeltaLogger", "*", :warn
restart server

原文

blog原文