Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

Ruby on Rails | Railsのdevelopment時のみ出力されるデバッグログ空行2行を無効化

概要

Railsのdevelopment時のみ出力されるデバッグログ空行2行を無効化

内容

Railsのdevelopment時のみ出力されるデバッグログ空行2行を無効化します。
該当箇所のソースコードGitHubの下記参照。
https://github.com/rails/rails/blob/ffa9540fd361eb34c445568b66abf283b9e658f8/railties/lib/rails/rack/logger.rb
この部分をオープンクラスで無効化します。

実装

config/initializers/logger.rb

# develop時に出力される空行のログを無効化するためにRails::Rack::Logger#call_appをオープン
module Rails
  module Rack
    # Sets log tags, logs the request, calls the app, and flushes the logs.
    class Logger < ActiveSupport::LogSubscriber
      def call_app(request, env)
        # 空ログを無効化
        # Put some space between requests in development logs.
        # if development?
        #   logger.debug ''
        #   logger.debug ''
        # end

        @instrumenter.start 'action_dispatch.request', request: request
        logger.info started_request_message(request)
        resp = @app.call(env)
        resp[2] = ::Rack::BodyProxy.new(resp[2]) { finish(request) }
        resp
      rescue
        finish(request)
        raise
      ensure
        ActiveSupport::LogSubscriber.flush_all!
      end
    end
  end
end

修正前ログ

※独自実装でLTSV形式にしてます
デバッグログを確認。

time:[2013/07/28 22:17:52.549]	level:[DEBUG] msg:[]
time:[2013/07/28 22:17:52.550]	level:[DEBUG] msg:[]
time:[2013/07/28 22:17:52.550]	level:[INFO]	type:[POST] uri:["/signin/signin"]	host:[192.168.xxx.xxx]

修正後ログ

デバッグログがなくなりました。

time:[2013/07/28 22:22:16.923]  level:[INFO]  type:[POST] uri:["/signin/signin"]  host:[192.168.xxx.xxx]