Created
January 19, 2011 17:09
-
-
Save mcmire/786456 to your computer and use it in GitHub Desktop.
Integrating the Logging framework into your Rails 3 app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#----------------------------------------------------------------------------------------------- | |
# config/application.rb | |
#----------------------------------------------------------------------------------------------- | |
require File.expand_path('../boot', __FILE__) | |
require 'rails/all' | |
Bundler.require(:default, Rails.env) if defined?(Bundler) | |
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#----------------------------------------------------------------------------------------------- | |
# lib/my_app/logging-rails.rb | |
#----------------------------------------------------------------------------------------------- | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#----------------------------------------------------------------------------------------------- | |
# config/logging.rb | |
#----------------------------------------------------------------------------------------------- | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment