Rails3 custom logging options?

二次信任 提交于 2020-01-31 18:42:19

问题


What are the "best-practice" custom logging options for Rails3? Can anyone suggest good gems, or techniques, for logging.

In general, what is the convention for custom, non-controller, non-model stuff? If I make a custom logger like this:

#custom_logger.rb
class CustomLogger < Logger
  def format_message(severity, timestamp, progname, msg)
    "#{msg}\n"
  end
end

logfile = File.open(RAILS_ROOT + '/log/custom.log', 'a')  #create log file
logfile.sync = true  #automatically flushes data to file
CUSTOM_LOGGER = CustomLogger.new(logfile)  #constant accessible anywhere

(stolen from here) Should that go in a file in "/lib"? In "application.rb"? Or in an initializer?

I can't seem to find anything that describes these little differences from previous Rails versions.

Thanks


回答1:


'lib' still seems to be the proper place to put this stuff.

See:

Where should libraries go in Rails 3?




回答2:


This gem works really well for customizing log output: https://github.com/johmas/itslog




回答3:


I put mine in application.rb, because it's only a few lines. I'm lazy like that. If you put your class in a file in lib/, you'll have to require it manually or set up lib/ autoloading.

Here's my logger code:

application.rb:

class DreamLogFormatter < Logger::Formatter
  def call(severity, time, progname, msg)
    "[%s(%d)%5s] %s\n" % [time.to_s(:short), $$, severity, msg2str(msg)]
  end
end

....

config.logger = Logger.new(Rails.root.join('log', "#{Rails.env}.log"), 10, 30*1024*1024)
config.logger.formatter = DreamLogFormatter.new

Notice I roll the logs every 30MB and keep the last 10 logs.



来源:https://stackoverflow.com/questions/4937396/rails3-custom-logging-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!