Class: Inferno::Utils::Middleware::RequestLogger
- Inherits:
-
Object
- Object
- Inferno::Utils::Middleware::RequestLogger
- Defined in:
- lib/inferno/utils/middleware/request_logger.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app) ⇒ RequestLogger
constructor
A new instance of RequestLogger.
- #log_request(env) ⇒ Object
- #log_response(response, start_time, end_time, exception = nil) ⇒ Object
- #logger ⇒ Object
- #verbose_logging? ⇒ Boolean
Constructor Details
#initialize(app) ⇒ RequestLogger
Returns a new instance of RequestLogger.
10 11 12 |
# File 'lib/inferno/utils/middleware/request_logger.rb', line 10 def initialize(app) @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
8 9 10 |
# File 'lib/inferno/utils/middleware/request_logger.rb', line 8 def app @app end |
Instance Method Details
#call(env) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/inferno/utils/middleware/request_logger.rb', line 22 def call(env) start = Time.now log_request(env) begin response = app.call(env) log_response(response, start, Time.now) rescue StandardError => e log_response([500, nil, nil], start, Time.now, e) raise e end env['inferno.response'] = response # rack.after_reply is handled by puma, which doesn't process requests # in unit tests, so we manually run them when in the test environment env['rack.after_reply']&.each(&:call) if ENV['APP_ENV'] == 'test' response end |
#log_request(env) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/inferno/utils/middleware/request_logger.rb', line 59 def log_request(env) method = env['REQUEST_METHOD'] scheme = env['rack.url_scheme'] host = env['HTTP_HOST'] path = env['REQUEST_URI'] query = env['rack.request.query_string'] body = env['rack.input'] body = if body.instance_of? Puma::NullIO nil else contents = body.read body.rewind contents end query_string = query.blank? ? '' : "?#{query}" logger.info("#{method} #{scheme}://#{host}#{path}#{query_string}") return unless body.present? if body.length > 100 && !verbose_logging? logger.info("#{body[0..100]}...") else logger.info(body) end end |
#log_response(response, start_time, end_time, exception = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/inferno/utils/middleware/request_logger.rb', line 42 def log_response(response, start_time, end_time, exception = nil) elapsed = end_time - start_time status, _response_headers, body = response if response status, = response if exception logger.info("#{status} in #{elapsed.in_milliseconds} ms") return unless body.present? body = body.join if body.is_a?(Array) if body.length > 100 && !verbose_logging? logger.info("#{body[0..100]}...") else logger.info(body) end end |
#logger ⇒ Object
18 19 20 |
# File 'lib/inferno/utils/middleware/request_logger.rb', line 18 def logger @logger ||= Application['logger'] end |
#verbose_logging? ⇒ Boolean
14 15 16 |
# File 'lib/inferno/utils/middleware/request_logger.rb', line 14 def verbose_logging? @verbose_logging ||= ENV['VERBOSE_REQUEST_LOGGING']&.downcase == true end |