A skeleton web application configured to use Sinatra and ActiveRecord

Logger: for 300 and 400 (but not 404) errors, log body

We're likely sending a string from sinatra's halt, so log it

+12 -5
+12 -5
lib/logging.rb
··· 30 status, headers, body = @app.call(env) 31 headers["X-Request-Id"] = request.uuid 32 body = Rack::BodyProxy.new(body) { 33 - log(env, request, status, headers, began_at) 34 } 35 [status, headers, body] 36 end 37 38 private 39 # Log the request to the configured logger. 40 - def log(env, request, status, headers, began_at) 41 logger = @logger || env[RACK_ERRORS] 42 43 # "text/html" -> "html", "application/ld+json; profile..." -> "ld+json" ··· 55 "output=#{headers["Content-Length"]}", 56 "format=#{output_format}", 57 "status=#{status}", 58 - "duration=#{sprintf("%0.2f", Time.now.to_f - began_at)}", 59 ] 60 61 - if (300..399).include?(status) 62 msg << "location=#{headers["Location"]}" 63 end 64 65 - msg << "params=#{App.filter_parameters(request.params).inspect}" 66 67 request.log_extras.each do |k,v| 68 msg << "#{k}=#{v}"
··· 30 status, headers, body = @app.call(env) 31 headers["X-Request-Id"] = request.uuid 32 body = Rack::BodyProxy.new(body) { 33 + log(env, request, status, headers, began_at, body.try(:first)) 34 } 35 [status, headers, body] 36 end 37 38 private 39 # Log the request to the configured logger. 40 + def log(env, request, status, headers, began_at, body = nil) 41 logger = @logger || env[RACK_ERRORS] 42 43 # "text/html" -> "html", "application/ld+json; profile..." -> "ld+json" ··· 55 "output=#{headers["Content-Length"]}", 56 "format=#{output_format}", 57 "status=#{status}", 58 ] 59 60 + case status 61 + when 300..399 62 msg << "location=#{headers["Location"]}" 63 + when 400..403, 405..499 64 + if body 65 + msg << "error=\"#{body}\"" 66 + end 67 end 68 69 + msg += [ 70 + "duration=#{sprintf("%0.2f", Time.now.to_f - began_at)}", 71 + "params=#{App.filter_parameters(request.params).inspect}", 72 + ] 73 74 request.log_extras.each do |k,v| 75 msg << "#{k}=#{v}"