Skip to content

Instantly share code, notes, and snippets.

@jerodsanto
Forked from simonjefford/firebug_logger.rb
Created December 9, 2009 16:23
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jerodsanto/252575 to your computer and use it in GitHub Desktop.
Save jerodsanto/252575 to your computer and use it in GitHub Desktop.
Rack::FirebugLogger
Allows logging from your Rack-based app in Firebug (or the WebKit inspector)
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
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.
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"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment