Permalink
Browse files

Allow use of logging panel in plain Rack apps and handle inconsistenc…

…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...
1 parent af36df1 commit b6bd79ee52d6e37388489976fa09026384a10d2e @knzconnor knzconnor committed Mar 27, 2010
View
29 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
View
7 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
<html>
<head>
View
17 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
View
2 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__))

0 comments on commit b6bd79e

Please sign in to comment.