Skip to content

Commit

Permalink
[+MH] Use Rails engine for the configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Malkov committed Aug 18, 2016
1 parent 764506c commit 25fe06f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 119 deletions.
27 changes: 0 additions & 27 deletions lib/sapience/configuration/rails.rb

This file was deleted.

65 changes: 65 additions & 0 deletions lib/sapience/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Sapience
class Engine < ::Rails::Engine

# Replace Rails logger initializer
Rails::Application::Bootstrap.initializers.delete_if { |i| i.name == :initialize_logger }

initializer :initialize_logger, group: :all do
::Rails.logger = Sapience[::Rails]
[:active_record, :action_controller, :action_mailer, :action_view].each do |name|
ActiveSupport.on_load(name) { include Sapience::Loggable }
end
ActiveSupport.on_load(:action_cable) { self.logger = Sapience["ActionCable"] }
end

# Before any initializers run, but after the gems have been loaded
config.before_initialize do
# Replace the Mongoid Logger
Mongoid.logger = Sapience[Mongoid] if defined?(Mongoid)
Moped.logger = Sapience[Moped] if defined?(Moped)

# Replace the Resque Logger
Resque.logger = Sapience[Resque] if defined?(Resque) && Resque.respond_to?(:logger)

# Replace the Sidekiq logger
Sidekiq::Logging.logger = Sapience[Sidekiq] if defined?(Sidekiq)

# Replace the Sidetiq logger
Sidetiq.logger = Sapience[Sidetiq] if defined?(Sidetiq)

# Replace the Raven logger
# Raven::Configuration.logger = Sapience[Raven::Configuration] if defined?(Raven::Configuration)
Raven.send(:include) { Sapience::Loggable }

# Replace the Sneakers logger
Sneakers.logger = Sapience[Sneakers] if defined?(Sneakers)

# Replace the Bugsnag logger
Bugsnag.configure { |config| config.logger = Sapience[Bugsnag] } if defined?(Bugsnag)

# Set the logger for concurrent-ruby
Concurrent.global_logger = Sapience[Concurrent] if defined?(Concurrent)

# Rails Patches
Kernel.require "sapience/extensions/action_cable/tagged_logger_proxy" if defined?(ActionCable)
Kernel.require "sapience/extensions/action_controller/live" if defined?(ActionController::Live)
Kernel.require "sapience/extensions/action_dispatch/debug_exceptions" if defined?(ActionDispatch::DebugExceptions)
Kernel.require "sapience/extensions/action_view/streaming_template_renderer" if defined?(ActionView::StreamingTemplateRenderer::Body)
Kernel.require "sapience/extensions/active_job/logging" if defined?(ActiveJob)
Kernel.require "sapience/extensions/active_model_serializers/logging" if defined?(ActiveModelSerializers)
Kernel.require "sapience/extensions/action_controller/log_subscriber" if defined?(ActionController)
Kernel.require "sapience/extensions/active_record/log_subscriber" if defined?(ActiveRecord::LogSubscriber)
Kernel.require "sapience/extensions/rails/rack/logger" if defined?(::Rails::Rack::Logger)
Kernel.require "sapience/extensions/rails/rack/logger_info_as_debug" if defined?(::Rails::Rack::Logger)
Kernel.require "sapience/extensions/action_view/log_subscriber" if defined?(ActionView::LogSubscriber)
Kernel.require "sapience/extensions/action_controller/log_subscriber_processing" if defined?(ActionView::LogSubscriber)
end

# Before any initializers run, but after the gems have been loaded
config.after_initialize do
# Replace the Bugsnag logger
Bugsnag.configure { |config| config.logger = Sapience[Bugsnag] } if defined?(Bugsnag)
end

end
end
10 changes: 5 additions & 5 deletions lib/sapience/extensions/active_job/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
module ActiveJob::Logging # rubocop:disable ClassAndModuleChildren
include Sapience::Loggable

# private
private

# alias_method :tag_logger_old, :tag_logger
alias_method :tag_logger_old, :tag_logger

# def tag_logger(*tags, &block)
# logger.tagged(*tags, &block)
# end
def tag_logger(*tags, &block)
logger.tagged(*tags, &block)
end
end
8 changes: 4 additions & 4 deletions lib/sapience/extensions/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class ActiveRecord::LogSubscriber # rubocop:disable ClassAndModuleChildren
module Inclusions
ActiveRecord::LogSubscriber # rubocop:disable ClassAndModuleChildren

module ActiveRecord
class LogSubscriber
def sql(event) # rubocop:disable AbcSize
self.class.runtime += event.duration

Expand Down Expand Up @@ -32,6 +34,4 @@ def sql(event) # rubocop:disable AbcSize
debug(log)
end
end

include Inclusions
end
83 changes: 0 additions & 83 deletions spec/lib/sapience/configuration/rails_spec.rb

This file was deleted.

0 comments on commit 25fe06f

Please sign in to comment.