Skip to content

Instantly share code, notes, and snippets.

@michealbenedict
Forked from mcmire/application.rb
Created February 2, 2012 19:54

Revisions

  1. @mcmire mcmire revised this gist Sep 16, 2011. 1 changed file with 0 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions application.rb
    Original file line number Diff line number Diff line change
    @@ -3,16 +3,9 @@
    #-----------------------------------------------------------------------------------------------

    require File.expand_path('../boot', __FILE__)

    require 'rails/all'

    # If you have a Gemfile, require the gems listed there, including any gems
    # you've limited to :test, :development, or :production.
    Bundler.require(:default, Rails.env) if defined?(Bundler)

    # For debugging purposes
    require 'pp'

    # Bring in the Railtie that will set Rails' various logger variables to
    # instances of Logging::Logger instead of Rails' built-in ActiveSupport::BufferedLogger.
    require File.expand_path('../../lib/my_app/logging-rails', __FILE__)
  2. @mcmire mcmire revised this gist Jan 19, 2011. 3 changed files with 3 additions and 0 deletions.
    1 change: 1 addition & 0 deletions application.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #-----------------------------------------------------------------------------------------------
    # config/application.rb
    #-----------------------------------------------------------------------------------------------

    1 change: 1 addition & 0 deletions logging-rails.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #-----------------------------------------------------------------------------------------------
    # lib/my_app/logging-rails.rb
    #-----------------------------------------------------------------------------------------------

    1 change: 1 addition & 0 deletions logging.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    #-----------------------------------------------------------------------------------------------
    # config/logging.rb
    #-----------------------------------------------------------------------------------------------

  3. @mcmire mcmire revised this gist Jan 19, 2011. 3 changed files with 9 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions config/application.rb → application.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    # config/application.rb
    #-----------------------------------------------------------------------------------------------

    require File.expand_path('../boot', __FILE__)

    require 'rails/all'
    3 changes: 3 additions & 0 deletions lib/my_app/logging-rails.rb → logging-rails.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    # lib/my_app/logging-rails.rb
    #-----------------------------------------------------------------------------------------------

    require 'logging'

    module Logging
    3 changes: 3 additions & 0 deletions config/logging.rb → logging.rb
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    # config/logging.rb
    #-----------------------------------------------------------------------------------------------

    Logging::Rails.configure do |config|
    Logging.format_as :inspect
    layout = Logging.layouts.pattern(:pattern => '[%d] %-5l %c : %m\n')
  4. @mcmire mcmire created this gist Jan 19, 2011.
    20 changes: 20 additions & 0 deletions config/application.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    require File.expand_path('../boot', __FILE__)

    require 'rails/all'

    # If you have a Gemfile, require the gems listed there, including any gems
    # you've limited to :test, :development, or :production.
    Bundler.require(:default, Rails.env) if defined?(Bundler)

    # For debugging purposes
    require 'pp'

    # Bring in the Railtie that will set Rails' various logger variables to
    # instances of Logging::Logger instead of Rails' built-in ActiveSupport::BufferedLogger.
    require File.expand_path('../../lib/my_app/logging-rails', __FILE__)

    module MyApp
    class Application < Rails::Application
    # ...
    end
    end
    37 changes: 37 additions & 0 deletions config/logging.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    Logging::Rails.configure do |config|
    Logging.format_as :inspect
    layout = Logging.layouts.pattern(:pattern => '[%d] %-5l %c : %m\n')

    Logging.appenders.stdout('stdout',
    :auto_flushing => true,
    :layout => layout
    )

    Logging.appenders.rolling_file('logfile',
    :filename => config.paths.log.to_a.first,
    :keep => 7,
    :age => 'daily',
    :truncate => false,
    :auto_flushing => true,
    :layout => layout
    )

    Logging.logger.root.level = config.log_level
    Logging.logger.root.appenders = %w[logfile]


    # Under Phusion Passenger smart spawning, we need to reopen all IO streams
    # after workers have forked.
    #
    # The rolling file appender uses shared file locks to ensure that only one
    # process will roll the log file. Each process writing to the file must have
    # its own open file descriptor for flock to function properly. Reopening the
    # file descriptors afte forking ensure that each worker has a unique file
    # descriptor.
    #
    if defined?(PhusionPassenger)
    PhusionPassenger.on_event(:starting_worker_process) do |forked|
    Logging.reopen if forked
    end
    end
    end
    59 changes: 59 additions & 0 deletions lib/my_app/logging-rails.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    require 'logging'

    module Logging
    module Rails
    class << self
    attr_reader :configuration
    def configure(&block)
    @configuration = block
    end
    end

    module ControllerMixin
    def self.included(base)
    base.extend(self)
    end

    def logger
    @_logger ||= Logging::Logger[self]
    end
    end

    class Railtie < ::Rails::Railtie
    initializer "logging.configure_logger", :before => :initialize_logger do |app|
    file = ::Rails.root.join('config/logging.rb')
    return unless File.exists?(file)
    require file
    Logging::Rails.configuration.call(app.config)
    end

    initializer "logging.initialize_logger", :before => :initialize_logger do |app|
    ::Rails.logger = Logging::Logger[::Rails]
    end

    initializer "logging.active_record.logger", :before => "active_record.logger" do |app|
    ActiveSupport.on_load(:active_record) { ActiveRecord::Base.logger = Logging::Logger[ActiveRecord::Base] }
    end

    initializer "logging.action_controller.logger", :before => "action_controller.logger" do
    ActiveSupport.on_load(:action_controller) { ActionController::Base.logger = Logging::Logger[ActionController::Base] }
    end

    initializer "logging.action_mailer.logger", :before => "action_mailer.logger" do
    ActiveSupport.on_load(:action_mailer) { ActionMailer::Base.logger = Logging::Logger[ActionMailer::Base] }
    end

    initializer "logger.active_support.dependencies.logger" do
    ActiveSupport::Dependencies.logger = Logging::Logger[ActiveSupport::Dependencies]
    end

    initializer "logger.initialize_cache", :after => :initialize_cache do
    ::Rails.cache.logger = Logging::Logger[::Rails.cache]
    end

    config.after_initialize do
    Logging.show_configuration if Logging.logger[::Rails].debug?
    end
    end
    end
    end