Skip to content

Instantly share code, notes, and snippets.

@indirect
Created July 19, 2011 07:00
Show Gist options
  • Select an option

  • Save indirect/1091527 to your computer and use it in GitHub Desktop.

Select an option

Save indirect/1091527 to your computer and use it in GitHub Desktop.

Revisions

  1. André Arko revised this gist Dec 12, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Prepend pid and severity to the written message
    log = "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    log = "[%s] %-5.5s %s" % [$$, SEVERITIES[severity], message.gsub(/^\n+/, '')]
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
  2. André Arko revised this gist Dec 1, 2011. 1 changed file with 21 additions and 13 deletions.
    34 changes: 21 additions & 13 deletions better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -1,41 +1,48 @@
    # You must require this file in application.rb, above the Application
    # definition, for this to work. For example:
    #
    # # PIDs prepended to logs
    # # Syslog-like Rails logs
    # if Rails.env.production?
    # require File.expand_path('../../lib/pid_logger', __FILE__)
    # require File.expand_path('../../lib/better_logger', __FILE__)
    # end
    #
    # module MyApp
    # class Application < Rails::Application

    require 'active_support/buffered_logger'

    class PidLogger < ActiveSupport::BufferedLogger
    class BetterLogger < ActiveSupport::BufferedLogger

    SEVERITIES = Severity.constants.sort_by{|c| Severity.const_get(c) }

    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Insert a newline before the log line if there was one in the first place.
    log = (message[0] == ?\n) ? "\n" : ""
    # Prepend pid and severity to the written message
    log << "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then end with a newline.
    log = "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    log
    message
    end

    class Railtie < ::Rails::Railtie
    initializer "swap in PidLogger" do
    path = Rails.application.config.paths.log.first
    level = Rails.logger.level
    Rails.logger = PidLogger.new(path, level)
    # overwrite Rails' initializer to set up our own instead
    initializer :initialize_logger do |app|
    Rails.logger = begin
    logger = BetterLogger.new(app.config.paths.log.to_a.first)
    level_name = app.config.log_level.to_s.upcase
    logger.level = ActiveSupport::BufferedLogger.const_get(level_name)
    logger.auto_flushing = false if Rails.env.production?
    logger
    end
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger

    # cache has no callback of its own, but is set before this callback
    ActiveSupport.on_load(:before_initialize) do
    Rails.cache.logger = Rails.logger
    end
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.logger = Rails.logger
    end
    @@ -46,6 +53,7 @@ class Railtie < ::Rails::Railtie
    ActionMailer::Base.logger = Rails.logger
    end
    end

    end

    end
  3. Andre Arko revised this gist Sep 10, 2011. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,11 @@ class PidLogger < ActiveSupport::BufferedLogger
    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Insert a newline before the log line if there was one in the first place.
    log = (message[0] == ?\n) ? "\n" : ""
    # Prepend pid and severity to the written message
    log = "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then end with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    @@ -46,4 +48,4 @@ class Railtie < ::Rails::Railtie
    end
    end

    end
    end
  4. Andre Arko revised this gist Sep 10, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@ def add(severity, message = nil, progname = nil, &block)
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    message
    log
    end

    class Railtie < ::Rails::Railtie
  5. Andre Arko revised this gist Sep 7, 2011. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,9 @@ def add(severity, message = nil, progname = nil, &block)

    class Railtie < ::Rails::Railtie
    initializer "swap in PidLogger" do
    Rails.logger = PidLogger.new(Rails.application.config.paths.log.first)
    path = Rails.application.config.paths.log.first
    level = Rails.logger.level
    Rails.logger = PidLogger.new(path, level)
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger
    ActiveSupport.on_load(:active_record) do
  6. Andre Arko revised this gist Aug 18, 2011. 1 changed file with 39 additions and 40 deletions.
    79 changes: 39 additions & 40 deletions better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -1,48 +1,47 @@
    # You must require this file in application.rb, above the Application
    # definition, for this to work. For example:
    #
    # # PIDs prepended to logs
    # if Rails.env.production?
    # require File.expand_path('../../lib/pid_logger', __FILE__)
    # end
    #
    # module MyApp
    # class Application < Rails::Application

    # You must require this file in application.rb, above the Application
    # definition, for this to work. For example:
    #
    # # PIDs prepended to logs
    # if Rails.env.production?
    # require File.expand_path('../../lib/pid_logger', __FILE__)
    # end
    #
    # module MyApp
    # class Application < Rails::Application
    require 'active_support/buffered_logger'

    require 'active_support/buffered_logger'
    class PidLogger < ActiveSupport::BufferedLogger

    class PidLogger < ActiveSupport::BufferedLogger
    SEVERITIES = Severity.constants.sort_by{|c| Severity.const_get(c) }

    SEVERITIES = Severity.constants.sort_by{|c| Severity.const_get(c) }
    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Prepend pid and severity to the written message
    log = "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    message
    end

    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Prepend pid and severity to the written message
    log = "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    message
    class Railtie < ::Rails::Railtie
    initializer "swap in PidLogger" do
    Rails.logger = PidLogger.new(Rails.application.config.paths.log.first)
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.logger = Rails.logger
    end

    class Railtie < ::Rails::Railtie
    initializer "swap in PidLogger" do
    Rails.logger = PidLogger.new(Rails.application.config.paths.log.first)
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_controller) do
    ActionController::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_mailer) do
    ActionMailer::Base.logger = Rails.logger
    end
    end
    ActiveSupport.on_load(:action_controller) do
    ActionController::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_mailer) do
    ActionMailer::Base.logger = Rails.logger
    end

    end
    end

    end
  7. Andre Arko revised this gist Aug 18, 2011. 1 changed file with 40 additions and 41 deletions.
    81 changes: 40 additions & 41 deletions better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -1,49 +1,48 @@
    # You must require this file in application.rb, above the Application
    # definition, for this to work. For example:
    #
    # # Syslog-like Rails logs
    # if Rails.env.production?
    # require File.expand_path('../../lib/better_logger', __FILE__)
    # end
    #
    # module MyApp
    # class Application < Rails::Application

    require 'active_support/buffered_logger'
    # You must require this file in application.rb, above the Application
    # definition, for this to work. For example:
    #
    # # PIDs prepended to logs
    # if Rails.env.production?
    # require File.expand_path('../../lib/pid_logger', __FILE__)
    # end
    #
    # module MyApp
    # class Application < Rails::Application

    class BetterLogger < ActiveSupport::BufferedLogger
    require 'active_support/buffered_logger'

    SEVERITIES = Severity.constants.sort_by{|c| Severity.const_get(c) }
    class PidLogger < ActiveSupport::BufferedLogger

    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # (Optional) timestamp, not needed if you feed your logs to syslog or equivalent
    #log = Time.now.to_formatted_s(:db)} + " "
    # Add severity and pid
    log = "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    message
    end
    SEVERITIES = Severity.constants.sort_by{|c| Severity.const_get(c) }

    class Railtie < ::Rails::Railtie
    initializer "swap in BetterLogger" do
    Rails.logger = BetterLogger.new(Rails.application.config.paths.log.first)
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.logger = Rails.logger
    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Prepend pid and severity to the written message
    log = "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    message
    end
    ActiveSupport.on_load(:action_controller) do
    ActionController::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_mailer) do
    ActionMailer::Base.logger = Rails.logger

    class Railtie < ::Rails::Railtie
    initializer "swap in PidLogger" do
    Rails.logger = PidLogger.new(Rails.application.config.paths.log.first)
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_controller) do
    ActionController::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_mailer) do
    ActionMailer::Base.logger = Rails.logger
    end
    end
    end
    end
    end

    end
    end
  8. Andre Arko revised this gist Aug 3, 2011. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -18,9 +18,10 @@ class BetterLogger < ActiveSupport::BufferedLogger
    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Add timestamp, severity, and pid, kinda like syslog
    log = "#{Time.now.to_formatted_s(:db)} #{SEVERITIES[severity]}"
    log << " [#{$$}] #{message.gsub(/^\n+/, '')}"
    # (Optional) timestamp, not needed if you feed your logs to syslog or equivalent
    #log = Time.now.to_formatted_s(:db)} + " "
    # Add severity and pid
    log = "[#{$$}] #{SEVERITIES[severity]} #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
  9. Andre Arko revised this gist Jul 19, 2011. 1 changed file with 0 additions and 48 deletions.
    48 changes: 0 additions & 48 deletions snippet.rb
    Original file line number Diff line number Diff line change
    @@ -1,48 +0,0 @@
    # You must require this file in application.rb, above the Application
    # definition, for this to work. For example:
    #
    # # Syslog-like Rails logs
    # if Rails.env.production?
    # require File.expand_path('../../lib/better_logger', __FILE__)
    # end
    #
    # module MyApp
    # class Application < Rails::Application

    require 'active_support/buffered_logger'

    class BetterLogger < ActiveSupport::BufferedLogger

    SEVERITIES = Severity.constants.sort_by{|c| Severity.const_get(c) }

    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Add timestamp, severity, and pid, kinda like syslog
    log = "#{Time.now.to_formatted_s(:db)} #{SEVERITIES[severity]}"
    log << " [#{$$}] #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    message
    end

    class Railtie < ::Rails::Railtie
    initializer "swap in BetterLogger" do
    Rails.logger = BetterLogger.new(Rails.application.config.paths.log.first)
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_controller) do
    ActionController::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_mailer) do
    ActionMailer::Base.logger = Rails.logger
    end
    end
    end

    end
  10. Andre Arko revised this gist Jul 19, 2011. 1 changed file with 48 additions and 0 deletions.
    48 changes: 48 additions & 0 deletions better_logger.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    # You must require this file in application.rb, above the Application
    # definition, for this to work. For example:
    #
    # # Syslog-like Rails logs
    # if Rails.env.production?
    # require File.expand_path('../../lib/better_logger', __FILE__)
    # end
    #
    # module MyApp
    # class Application < Rails::Application

    require 'active_support/buffered_logger'

    class BetterLogger < ActiveSupport::BufferedLogger

    SEVERITIES = Severity.constants.sort_by{|c| Severity.const_get(c) }

    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Add timestamp, severity, and pid, kinda like syslog
    log = "#{Time.now.to_formatted_s(:db)} #{SEVERITIES[severity]}"
    log << " [#{$$}] #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    message
    end

    class Railtie < ::Rails::Railtie
    initializer "swap in BetterLogger" do
    Rails.logger = BetterLogger.new(Rails.application.config.paths.log.first)
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_controller) do
    ActionController::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_mailer) do
    ActionMailer::Base.logger = Rails.logger
    end
    end
    end

    end
  11. Andre Arko revised this gist Jul 19, 2011. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion snippet.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@
    # definition, for this to work. For example:
    #
    # # Syslog-like Rails logs
    # require File.expand_path('../../lib/better_logger', __FILE__)
    # if Rails.env.production?
    # require File.expand_path('../../lib/better_logger', __FILE__)
    # end
    #
    # module MyApp
    # class Application < Rails::Application
  12. Andre Arko revised this gist Jul 19, 2011. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions snippet.rb
    Original file line number Diff line number Diff line change
    @@ -18,10 +18,9 @@ def add(severity, message = nil, progname = nil, &block)
    message = (message || (block && block.call) || progname).to_s
    # Add timestamp, severity, and pid, kinda like syslog
    log = "#{Time.now.to_formatted_s(:db)} #{SEVERITIES[severity]}"
    log << " [#{$$}] #{message}"
    log << " [#{$$}] #{message.gsub(/^\n+/, '')}"
    # If a newline is necessary then create a new message ending with a newline.
    # Ensures that the original message is not mutated.
    log << "\n" unless message[-1] == ?\n
    log << "\n" unless log[-1] == ?\n
    buffer << log
    auto_flush
    message
  13. Andre Arko created this gist Jul 19, 2011.
    47 changes: 47 additions & 0 deletions snippet.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # You must require this file in application.rb, above the Application
    # definition, for this to work. For example:
    #
    # # Syslog-like Rails logs
    # require File.expand_path('../../lib/better_logger', __FILE__)
    #
    # module MyApp
    # class Application < Rails::Application

    require 'active_support/buffered_logger'

    class BetterLogger < ActiveSupport::BufferedLogger

    SEVERITIES = Severity.constants.sort_by{|c| Severity.const_get(c) }

    def add(severity, message = nil, progname = nil, &block)
    return if @level > severity
    message = (message || (block && block.call) || progname).to_s
    # Add timestamp, severity, and pid, kinda like syslog
    log = "#{Time.now.to_formatted_s(:db)} #{SEVERITIES[severity]}"
    log << " [#{$$}] #{message}"
    # If a newline is necessary then create a new message ending with a newline.
    # Ensures that the original message is not mutated.
    log << "\n" unless message[-1] == ?\n
    buffer << log
    auto_flush
    message
    end

    class Railtie < ::Rails::Railtie
    initializer "swap in BetterLogger" do
    Rails.logger = BetterLogger.new(Rails.application.config.paths.log.first)
    ActiveSupport::Dependencies.logger = Rails.logger
    Rails.cache.logger = Rails.logger
    ActiveSupport.on_load(:active_record) do
    ActiveRecord::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_controller) do
    ActionController::Base.logger = Rails.logger
    end
    ActiveSupport.on_load(:action_mailer) do
    ActionMailer::Base.logger = Rails.logger
    end
    end
    end

    end