Created — forked from simonjefford/firebug_logger.rb

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Rack::FirebugLogger

View firebug_logger.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
module Rack
class FirebugLogger
def initialize(app, options = {})
@app = app
@options = options
end
 
def call(env)
status, headers, orig_response = @app.call(env)
return [status, headers, orig_response] unless (headers["Content-Type"] =~ /html/ && env['firebug.logs'])
new_response = Rack::Response.new([], status, headers)
js = generate_js(env['firebug.logs'])
if orig_response.body =~ /<body>/
orig_response.each do |line|
line.gsub!("</body>", js + "\n</body>")
new_response.write(line)
end
else
new_response.write(orig_response.body + js)
end
new_response.finish
end
 
private
 
def generate_js(logs)
js = ["<script type=\"text/javascript\">"]
start_group(js)
logs.each do |level, log|
level = sanitise_level(level)
log.gsub!('"', '\"')
js << "console.#{level.to_s}(\"#{log}\");"
end
end_group(js)
js << "</script>"
js.join("\n")
end
 
def start_group(js)
if @options[:group]
js << "console.group(\"#{@options[:group]}\");"
end
end
 
def sanitise_level(level)
[:info, :debug, :warn, :error].include?(level) ? level : :debug
end
 
def end_group(js)
if @options[:group]
js << "console.groupEnd();"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.