public
Last active — forked from simonjefford/firebug_logger.rb

Rack::FirebugLogger

  • Download Gist
About
1
Allows logging from your Rack-based app in Firebug (or the WebKit inspector)
LICENSE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Copyright (c) 2009 Simon Jefford
 
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
 
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
Usage
1 2 3 4 5 6 7 8
Currently takes one option when initialised, :group. This allows you to specify the group under which your logs appear - see "Nested grouping" at http://getfirebug.com/logging.html for an example.
 
To log, set env['firebug.logs'] to a list of pairs. The first element in each pair should be the log level: :info, :warn, :debug or :error. The second element should be the message.
 
For example
 
env['firebug.logs'] = [[:info, "beginning"]]
env['firebug.logs'] << [:error, "something went wrong"]
firebug_logger.rb
Ruby
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 56 57 58
class FirebugLogger
def initialize(app, options = {})
@app = app
@options = options
end
 
def call(env)
dup._call(env)
end
 
def _call(env)
status, headers, body = @app.call(env)
return [status, headers, body] if !(headers["Content-Type"] =~ /html/)
return [status, headers, body] unless env['firebug.logs']
response = Rack::Response.new([], status, headers)
js = generate_js(env['firebug.logs'])
body.each do |line|
line.gsub!("</body>", js)
response.write(line)
end
response.finish
end
 
private
 
def generate_js(logs)
js = ["<script>"]
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)
if [:info, :debug, :warn, :error].include?(level)
level
else
:debug
end
end
 
def end_group(js)
if @options[:group]
js << "console.groupEnd();"
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.