Skip to content

Instantly share code, notes, and snippets.

@simonjefford
Created October 14, 2009 13:20
Show Gist options
  • Save simonjefford/210069 to your computer and use it in GitHub Desktop.
Save simonjefford/210069 to your computer and use it in GitHub Desktop.

Revisions

  1. simonjefford revised this gist Nov 8, 2009. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions firebug_logger.rb
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,7 @@ def call(env)

    def _call(env)
    status, headers, body = @app.call(env)
    return [status, headers, body] unless (headers["Content-Type"] =~ /html/)
    return [status, headers, body] unless env['firebug.logs']
    return [status, headers, body] unless (headers["Content-Type"] =~ /html/ && env['firebug.logs'])
    response = Rack::Response.new([], status, headers)
    js = generate_js(env['firebug.logs'])
    body.each do |line|
    @@ -26,7 +25,7 @@ def _call(env)
    private

    def generate_js(logs)
    js = ["<script>"]
    js = ["<script type=\"text/javascript\">"]
    start_group(js)
    logs.each do |level, log|
    level = sanitise_level(level)
    @@ -58,4 +57,4 @@ def end_group(js)
    js << "console.groupEnd();"
    end
    end
    end
    end
  2. unknown revised this gist Oct 15, 2009. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions firebug_logger.rb
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ def call(env)

    def _call(env)
    status, headers, body = @app.call(env)
    return [status, headers, body] if !(headers["Content-Type"] =~ /html/)
    return [status, headers, body] unless (headers["Content-Type"] =~ /html/)
    return [status, headers, body] unless env['firebug.logs']
    response = Rack::Response.new([], status, headers)
    js = generate_js(env['firebug.logs'])
    @@ -58,4 +58,4 @@ def end_group(js)
    js << "console.groupEnd();"
    end
    end
    end
    end
  3. simonjefford revised this gist Oct 14, 2009. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion firebug_logger.rb
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ def call(env)

    def _call(env)
    status, headers, body = @app.call(env)
    return [status, headers, body] unless (headers["Content-Type"] =~ /html/)
    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'])
    @@ -35,6 +35,7 @@ def generate_js(logs)
    end
    end_group(js)
    js << "</script>"
    js << "</body>"
    js.join("\n")
    end

  4. simonjefford revised this gist Oct 14, 2009. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions firebug_logger.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # See also http://github.com/simonjefford/rack_firebug_logger
    # for this middleware + tests + a rails plugin
    class FirebugLogger
    def initialize(app, options = {})
    @app = app
  5. simonjefford revised this gist Oct 14, 2009. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion firebug_logger.rb
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ def call(env)

    def _call(env)
    status, headers, body = @app.call(env)
    return [status, headers, body] if !(headers["Content-Type"] =~ /html/)
    return [status, headers, body] unless (headers["Content-Type"] =~ /html/)
    return [status, headers, body] unless env['firebug.logs']
    response = Rack::Response.new([], status, headers)
    js = generate_js(env['firebug.logs'])
  6. simonjefford revised this gist Oct 14, 2009. 3 changed files with 0 additions and 35 deletions.
    1 change: 0 additions & 1 deletion About
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    Allows logging from your Rack-based app in Firebug (or the WebKit inspector)
    22 changes: 0 additions & 22 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -1,22 +0,0 @@
    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.
    12 changes: 0 additions & 12 deletions Usage
    Original file line number Diff line number Diff line change
    @@ -1,12 +0,0 @@
    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"]
  7. simonjefford revised this gist Oct 14, 2009. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions Usage
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,10 @@
    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.
    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.
    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

  8. simonjefford created this gist Oct 14, 2009.
    1 change: 1 addition & 0 deletions About
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Allows logging from your Rack-based app in Firebug (or the WebKit inspector)
    22 changes: 22 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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.
    8 changes: 8 additions & 0 deletions Usage
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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"]
    58 changes: 58 additions & 0 deletions firebug_logger.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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