8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Rails]不要なアプリケーションログを削除する

Posted at

rails server しているときに、下記のようなcssやjsが大量に出力されてログが見難くなることがある。

log/development.log
Started GET "/assets/application-ab419c13308629544db2ed02ed5cccfb.css?body=1" for 192.168.56.1 at 2015-04-03 13:20:39 +0900

Started GET "/assets/jquery-87424c3c19e96d4fb033c10ebe21ec40.js?body=1" for 192.168.56.1 at 2015-04-03 13:20:39 +0900

そんなときは、 config/initializers ディレクトリに以下ファイルを追加すればログがスッキリする :sparkles:

config/initializers/quiet_assets.rb
Rails.application.assets.logger = Logger.new('/dev/null')
Rails::Rack::Logger.class_eval do
  def call_with_quiet_assets(env)
    previous_level = Rails.logger.level
    if env['HTTP_X_RAILS_ENABLE_ASSETS_LOG'].nil? && env['PATH_INFO'].index('/assets/') == 0
      Rails.logger.level = Logger::ERROR
    end
    call_without_quiet_assets(env).tap do
      Rails.logger.level = previous_level
    end
  end
  alias_method_chain :call, :quiet_assets
end

さらに、Chromeのextension(Header Hacker)を使えば、cssやjsのログも見たいとき、以下の設定で簡単に表示を再開することも可能(゚д゚)ウマー

header_hacker_setting1.png

設定値

Title Header Append/Replace String
rails_enable_assets_log X_RAILS_ENABLE_ASSETS_LOG Append 1

設定例
header_hacker_setting2.png

8
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?