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