2011-06-24 6 views
1

私は、分析のための多くのリクエストを生成するレール3アプリを持っています。残念ながら、これはログを壊し、私は実際に気にしているメインページのリクエストを失います。これらの要求を別々のログファイルに分けたいと思います。特定のログファイルに行く特定のアクションを指定する方法はありますか?または、これらのアクションのロギングレベルを減らし、ログファイルを読み戻すときに特定のレベルのログのみを表示する方法がありますか?アクション毎のレールログを分離する

+0

try http://agilewebdevelopment.com/plugins/tagged_logger –

答えて

6

私はthis siteを見つけました。これは、ログアクションをサイレンシングするためのミドルウェアの使用について話しました。私は同じ種類のアイデアを使用し、どのアクションが呼び出されたかに応じてロガーをスワップするミドルウェアを作成しました。ここで私はnoisy_logger.rb/libに

class NoisyLogger < Rails::Rack::Logger 
    def initialize app, opts = {} 
    @default_log = Rails.logger 

    # Put the noisy log in the same directory as the default log. 
    @noisy_log = Logger.new Rails.root.join('log', 'noisy.log') 

    @app = app 
    @opts = opts 
    @opts[:noisy] = Array @opts[:noisy] 

    super app 
    end 

    def call env 
    if @opts[:noisy].include? env['PATH_INFO'] 
     logfile = @noisy_log 
    else 
     logfile = @default_log 
    end 

    # What?! Why are these all separate? 
    ActiveRecord::Base.logger = logfile 
    ActionController::Base.logger = logfile 
    Rails.logger = logfile 

    # The Rails::Rack::Logger class is responsible for logging the 
    # 'starting GET blah blah' log line. We need to call super here (as opposed 
    # to @app.call) to make sure that line gets output. However, the 
    # ActiveSupport::LogSubscriber class (which Rails::Rack::Logger inherits 
    # from) caches the logger, so we have to override that too 
    @logger = logfile 

    super 
    end 
end 

に入れ、その後、これは設定/初期化子に行くミドルウェア、/ noisy_log.rb

MyApp::Application.config.middleware.swap(
    Rails::Rack::Logger, NoisyLogger, :noisy => "/analytics/track" 
) 

は、誰かを役に立てば幸いです!

0

1つのオプションは、New Relicのようなサービスを使用しています。このサービスは、必要なスコープ(アクションごと)を提供します。