Skip to content

Commit

Permalink
Allow use of logging panel in plain Rack apps and handle inconsistenc…
Browse files Browse the repository at this point in the history
…y between AS::BufferedLogger and Logger

use amc-style to be consistent with other panels and for flexibility on swapping the logger out with a new instance, such as in tests.  Do it without actual amc, though, to avoid AS dependency on non-Rails specific panel
  • Loading branch information
knzconnor committed Oct 22, 2010
1 parent af36df1 commit b6bd79e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
29 changes: 21 additions & 8 deletions lib/rack/bug/panels/log_panel/logger_extension.rb
@@ -1,11 +1,24 @@
if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
module LoggerExtension
def add(*args, &block)
logged_message = super
Rack::Bug::LogPanel.record(logged_message, args[0])
return logged_message
end
module Rack::Bug::LoggerExtension
def self.included(target)
target.send :alias_method, :add_without_rack_bug, :add
target.send :alias_method, :add, :add_with_rack_bug
end

def add_with_rack_bug(*args, &block)
logger_return = add_without_rack_bug(*args, &block)
logged_message = logger_return
logged_message = args[1] || args[2] unless logged_message.is_a?(String)
Rack::Bug::LogPanel.record(logged_message, args[0])
return logger_return
end
end

if defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
logger = Rails.logger
elsif defined?(LOGGER)
logger = LOGGER
end

Rails.logger.extend LoggerExtensions
if logger
logger.class.send :include, Rack::Bug::LoggerExtension
end
7 changes: 6 additions & 1 deletion spec/fixtures/sample_app.rb
Expand Up @@ -2,6 +2,11 @@
require "rack/bug"

require "sinatra/base"
require 'logger'

RAILS_ENV = "development" unless defined?(RAILS_ENV)
log_to = RAILS_ENV == "test" ? StringIO.new : STDOUT
LOGGER = Logger.new(log_to)

class SampleApp < Sinatra::Base
use Rack::Bug#, :intercept_redirects => true, :password => 'secret'
Expand All @@ -23,7 +28,7 @@ class SampleApp < Sinatra::Base
if params[:content_type]
headers["Content-Type"] = params[:content_type]
end

LOGGER.error "I am a log message"
<<-HTML
<html>
<head>
Expand Down
17 changes: 17 additions & 0 deletions spec/rack/bug/panels/log_panel_spec.rb
Expand Up @@ -22,5 +22,22 @@ class Rack::Bug
response.should contain("DEBUG")
end
end

describe "Extended Logger" do
it "does still return true/false for #add if class Logger" do
#AS::BufferedLogger returns the added string, Logger returns true/false
LOGGER.add(0, "foo").should == true
end
end


describe "With no logger defined" do
it "does not err out" do
logger = LOGGER
Object.send :remove_const, :LOGGER
lambda{ load("rack/bug/panels/log_panel/logger_extension.rb") }.should_not raise_error
::LOGGER = logger
end
end
end
end
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -3,6 +3,8 @@
require "webrat"
require "rack/test"

RAILS_ENV = "test"

$LOAD_PATH.unshift File.dirname(File.dirname(__FILE__)) + '/lib'
$LOAD_PATH.unshift File.dirname(File.dirname(__FILE__))

Expand Down

0 comments on commit b6bd79e

Please sign in to comment.