登录 Rails 应用程序

2024-01-02

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


将其放入 ruby​​ 文件中,将该文件放入 /lib 文件夹(约定)并从您的环境中“需要”它。

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

这些行位于您的环境中的初始化程序块内。

config.log_level = ENV['RAILS_ENV']=='development' ?       
ActiveSupport::BufferedLogger::Severity::INFO :     
ActiveSupport::BufferedLogger::Severity::DEBUG
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

登录 Rails 应用程序 的相关文章

随机推荐