登录Rails应用 [英] logging in rails app

查看:65
本文介绍了登录Rails应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

log4r是正确登录Rails应用程序(日期时间,严重性,通知等?)的好选择吗?还是还有其他东西?

Is log4r a good option for proper logging in rails app (date time, severity, notification etc?) or is there something else out there?

推荐答案

将其放入ruby文件中,将该文件放在/lib文件夹(Convention)中,并从您的环境中获取".

Put this into a ruby file , put the file the /lib folder (Convention) and 'require' it from your environment.

require 'active_support'

# Logger class for custom logging format
class CustomLogger < ActiveSupport::BufferedLogger

private
# CustomLogger doesn't define strings for log levels
# so we have to do it ourselves
def severity_string(level)
  case level
    when DEBUG
        :DEBUG
    when INFO
        :INFO
    when WARN
        :WARN
    when ERROR
        :ERROR
    when FATAL
        :FATAL
    else
        :UNKNOWN
  end
end

public
# monkey patch the CustomLogger add method so that
# we can format the log messages the way we want
def add(severity, message = nil, progname = nil, &block)
  return if @level > severity
  message = (message || (block && block.call) || progname).to_s
  # If a newline is necessary then create a new message ending with a newline.
  # Ensures that the original message is not mutated.
  message = "[%5s %s] %s\n" % [severity_string(severity),
                      Time.now.strftime("%d-%m-%Y %H:%M:%S"),
                      message] unless message[-1] == ?\n
  buffer << message
  auto_flush
  message
end

end

这些行在您的环境中的初始化块内.

And these lines in your environment inside the initializer block.

config.log_level = ENV['RAILS_ENV']=='development' ?       
ActiveSupport::BufferedLogger::Severity::INFO :     
ActiveSupport::BufferedLogger::Severity::DEBUG

这篇关于登录Rails应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆