Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor into gem layout

  • Loading branch information...
commit 920f8c2fe254c555faf8df6aed93330294f0c22f 1 parent 40e8174
Jonathan Dance wuputah authored
47 init.rb
View
@@ -1,40 +1,9 @@
-begin
-
- STDOUT.sync = true
-
- def Rails.heroku_stdout_logger
- logger = Logger.new(STDOUT)
- logger.level = Logger.const_get(([ENV['LOG_LEVEL'].to_s.upcase, "INFO"] & %w[DEBUG INFO WARN ERROR FATAL UNKNOWN]).compact.first)
- logger
- end
-
- case Rails::VERSION::MAJOR
- when 3 then Rails.logger = Rails.application.config.logger = Rails.heroku_stdout_logger
- when 2 then
- # redefine Rails.logger
- def Rails.logger
- @@logger ||= Rails.heroku_stdout_logger
- end
- %w(
- ActiveSupport::Dependencies
- ActiveRecord::Base
- ActionController::Base
- ActionMailer::Base
- ActionView::Base
- ActiveResource::Base
- ).each do |klass_name|
- begin
- klass = Object
- klass_name.split("::").each { |part| klass = klass.const_get(part) }
- klass.logger = Rails.logger
- rescue
- end
- end
- Rails.cache.logger = Rails.logger rescue nil
- end
-
-rescue Exception => ex
-
- puts "WARNING: Exception during rails_log_stdout init: #{ex.message}"
-
+# only applies when used as a plugin
+case Rails::VERSION::MAJOR
+when 2
+ require 'rails_log_stdout/rails2'
+ RailsLogStdout::Rails2.set_logger
+when 3
+ require 'rails_log_stdout/rails3'
+ RailsLogStdout::Rails3.set_logger
end
5 lib/rails_log_stdout.rb
View
@@ -1,5 +1,2 @@
require "rails_log_stdout/version"
-
-module RailsLogStdout
- # Your code goes here...
-end
+require "rails_log_stdout/railtie" if defined?(Rails)
15 lib/rails_log_stdout/rails.rb
View
@@ -0,0 +1,15 @@
+module RailsLogStdout
+ class Rails
+
+ def self.heroku_stdout_logger
+ logger = Logger.new(STDOUT)
+ logger.level = Logger.const_get(([ENV['LOG_LEVEL'].to_s.upcase, "INFO"] & %w[DEBUG INFO WARN ERROR FATAL UNKNOWN]).compact.first)
+ logger
+ end
+
+ def self.set_logger
+ STDOUT.sync = true
+ end
+
+ end
+end
47 lib/rails_log_stdout/rails2.rb
View
@@ -0,0 +1,47 @@
+require 'rails_log_stdout/rails'
+
+module RailsLogStdout
+ class Rails2 < Rails
+
+ def self.set_logger
+ super
+ redefine_rails_logger!
+ classes.each do |klass_name|
+ begin
+ klass = constantize(klass_name)
+ klass.logger = Rails.logger
+ rescue NameError => exception
+ puts "WARNING: #{exception.message}"
+ end
+ end
+ end
+
+ def redefine_rails_logger!
+ class << Rails
+ def memoized_heroku_logger
+ @logger ||= self.heroku_stdout_logger
+ end
+ alias_method :rails_default_logger, :logger
+ alias_method :logger, :memoized_heroku_logger
+ end
+ end
+
+ def self.classes
+ %w(
+ ActiveSupport::Dependencies
+ ActiveRecord::Base
+ ActionController::Base
+ ActionMailer::Base
+ ActionView::Base
+ ActiveResource::Base
+ )
+ end
+
+ def self.constantize(klass_name)
+ klass_name.split("::").inject(Object) { |parent, child| parent.const_get(child) }
+ rescue NameError
+ raise NameError, "Unable to find #{klass_name}"
+ end
+
+ end
+end
10 lib/rails_log_stdout/rails3.rb
View
@@ -0,0 +1,10 @@
+require 'rails_log_stdout/rails'
+
+module RailsLogStdout
+ class Rails3 < Rails
+ def self.set_logger(config)
+ super()
+ Rails.logger = config.logger = heroku_stdout_logger
+ end
+ end
+end
9 lib/rails_log_stdout/railtie.rb
View
@@ -0,0 +1,9 @@
+require 'rails_log_stdout/rails3'
+
+module RailsLogStdout
+ class Railtie < Rails::Railtie
+ config.after_configuration do
+ Rails3.set_logger(config)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.