From b6bd79ee52d6e37388489976fa09026384a10d2e Mon Sep 17 00:00:00 2001 From: Tim Connor Date: Sat, 27 Mar 2010 11:42:53 -0700 Subject: [PATCH] Allow use of logging panel in plain Rack apps and handle inconsistency 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 --- .../bug/panels/log_panel/logger_extension.rb | 29 ++++++++++++++----- spec/fixtures/sample_app.rb | 7 ++++- spec/rack/bug/panels/log_panel_spec.rb | 17 +++++++++++ spec/spec_helper.rb | 2 ++ 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/lib/rack/bug/panels/log_panel/logger_extension.rb b/lib/rack/bug/panels/log_panel/logger_extension.rb index 211098d..4a28fdc 100644 --- a/lib/rack/bug/panels/log_panel/logger_extension.rb +++ b/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 \ No newline at end of file diff --git a/spec/fixtures/sample_app.rb b/spec/fixtures/sample_app.rb index f5a2a99..9552411 100644 --- a/spec/fixtures/sample_app.rb +++ b/spec/fixtures/sample_app.rb @@ -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' @@ -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 diff --git a/spec/rack/bug/panels/log_panel_spec.rb b/spec/rack/bug/panels/log_panel_spec.rb index 4552ee8..f244832 100644 --- a/spec/rack/bug/panels/log_panel_spec.rb +++ b/spec/rack/bug/panels/log_panel_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dc0f822..be80084 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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__))