2012-08-16 29 views
23

Làm cách nào để ngăn Rails đăng nhập quá nhiều? Đây là một dấu vết điển hình trong tập tin production.log của tôi, nhiều partials, truy cập bộ nhớ cache ... Nó rất hữu ích trong phát triển nhưng tôi không muốn nó trong môi trường sản xuất của tôi.Đường ray ghi quá chi tiết

Started GET "/?redirected=true" for 46.193.131.53 at 2012-08-16 18:39:20 +0200 
Processing by HomeController#index as HTML 
    Parameters: {"redirected"=>"true"} 
    Rendered application/_successfully_connected.html.haml (0.8ms) 
    Rendered hotspot_infos/_infos.html.haml (0.4ms) 
    Rendered application/_hotspot_infos.html.haml (1.8ms) 
    Rendered application/_news.html.haml (0.3ms) 
Read fragment views/social-zone-341-directory (0.5ms) 
    Rendered application/_directory.html.haml (2.5ms) 
    Rendered application/_meteo.html.haml (1.1ms) 
    Rendered application/_notifications.html.haml (0.8ms) 
    Rendered application/_like_button.html.haml (0.3ms) 
    Rendered application/_navbar.html.haml (4.2ms) 
    Rendered application/_connection.html.haml (0.5ms) 
    Rendered application/_gallery.html.haml (0.2ms) 
    Rendered application/_search_bar.html.haml (0.4ms) 
    Rendered pictures/_picture_frame.html.haml (0.3ms) 
    Rendered application/_profile_preview.html.haml (1.4ms) 
    Rendered application/_profile_block.html.haml (1.7ms) 
    Rendered application/_menus.html.haml (3.3ms) 
    Rendered application/_left_pane.html.haml (5.5ms) 
    Rendered application/_langs.html.haml (0.8ms) 
    Rendered application/_footer.html.haml (1.9ms) 
    Rendered application/_flash_modal.html.haml (0.1ms) 
    Rendered application/_connection_required.js.erb (0.2ms) 
Completed 200 OK in 159ms (Views: 25.5ms | ActiveRecord: 88.0ms) 

Thank của bạn đã giúp đỡ

PS: Tôi đang sử dụng Rails 3.2.6

+1

Sử dụng ** [Lograge] (https://github.com/roidrage/lograge) **, tốt nhất đá quý Tôi đã tìm thấy để làm sạch đăng nhập Rails – Yarin

Trả lời

30

Trong Rails 4 sẽ có một môi trường để làm sạch các bản ghi:

config.action_view.logger = nil 

Để đạt được điều đó trong Rails 3, bạn phải ActionView khỉ vá:

module ActionView 
    class LogSubscriber < ActiveSupport::LogSubscriber 
    def logger 
     @memoized_logger ||= Logger.new('/dev/null') 
    end 
    end 
end 
+7

FWIW, bản vá lỗi này dường như gây ra một tệp xử lý rò rỉ (ít nhất là với Rails 3.2.12). Tôi khuyên bạn nên thay thế 'Logger.new ('/ dev/null')' bằng '@memoized_logger || = Logger.new ('/ dev/null')'. Giọng nói đắng đắng ở đây. :) – bheeshmar

+1

Cảm ơn báo cáo. Tôi đã chỉnh sửa câu trả lời của mình. – sailor

+1

Để duy trì tính năng ghi nhật ký partials khi mức nhật ký bị gỡ lỗi, bạn có thể sử dụng '@memoized_logger || = Rails.logger.debug? ? super: Logger.new ('/ dev/null') ' – robd

6

Bạn cần phải thiết lập config.log_level của bạn khác nhau. Tìm hiểu về Log Levels.

Ví dụ, thêm dòng sau vào config/evironments/production.rb

config.log_level = :warn 

Yours có khả năng thiết lập để :debug hoặc :info.

+5

Vấn đề là khi mức độ đăng nhập được thiết lập để cảnh báo, tôi không nhận được gì cả trong tệp nhật ký của tôi. Tôi muốn các thông tin cơ bản được ghi lại như chỉ mục "Processing by HomeController # dưới dạng HTML" hoặc "Hoàn thành 200 OK trong 369ms (Lượt xem: 272.3ms | ActiveRecord: 8.1ms)" – sailor

3

tôi đặt này trong tôi initializers để monkeypatch nhất định đăng nhập để đi đến gỡ lỗi thay vì thông tin:

module ActionView 
    class LogSubscriber 
    def render_template(event) 
     message = "Rendered #{from_rails_root(event.payload[:identifier])}" 
     message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] 
     message << " (#{event.duration.round(1)}ms)" 
     debug(message) 
    end 
    alias :render_partial :render_template 
    alias :render_collection :render_template 
    end 
end 

module ActionController 
    class LogSubscriber 
    # Use debug logging for read_fragment 
    # %w(write_fragment read_fragment exist_fragment? expire_fragment expire_page write_page).each do |method| 
    %w(read_fragment).each do |method| 
     class_eval <<-METHOD, __FILE__, __LINE__ + 1 
     def #{method}(event) 
      return unless logger.info? 
      key_or_path = event.payload[:key] || event.payload[:path] 
      human_name = #{method.to_s.humanize.inspect} 
      debug("\#{human_name} \#{key_or_path} (\#{event.duration.round(1)}ms)") 
     end 
     METHOD 
    end 
    end 
end 
Các vấn đề liên quan