rails logger 和 session, cookies, request方面的笔记
View Helpers for Debugging
3种方式:
debug,to_yaml,inspect
示例:
<%= debug @post %>Title: <%= @post.title %>
<%= simple_format @post.to_yaml %>Title: <%= @post.title %>
<%= [1, 2, 3, 4, 5].inspect %>Title: <%= @post.title %>
用tagged
给日志内容加前缀:
举例
logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))logger.tagged("BCX") { logger.info "Stuff" } # Logs "[BCX] Stuff"logger.tagged("BCX", "Jason") { logger.info "Stuff" } # Logs "[BCX] [Jason] Stuff"logger.tagged("BCX") { logger.tagged("Jason") { logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"
当然,我们也可以使用 debugger 这个gem
gem install debugger
具体用法,在此不做过多赘述。
给不同日志指定不同的"日志文件"
new_logger = Logger.new('log/exceptions.log')new_logger.info('THIS IS A NEW EXCEPTION!')
Rails.logger和config.log以及Logger它们之间有何联系?
Rails makes use of the ActiveSupport::Logger class to write log information. You can also substitute another logger such as Log4r if you wish.
Rails.logger = Logger.new(STDOUT)Rails.logger = Log4r::Logger.new("Application Log")# 也可以,这么做config.logger = Logger.new(STDOUT)config.logger = Log4r::Logger.new("Application Log")
How can I get the current session id in rails 3? request.session_options[:id]