gpt4 book ai didi

ruby-on-rails - 登录Rails应用

转载 作者:行者123 更新时间:2023-12-02 04:14:07 25 4
gpt4 key购买 nike

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

最佳答案

将其放入ruby文件中,将文件放在/ lib文件夹(Convention)中,然后从您的环境中“请求”它。

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

关于ruby-on-rails - 登录Rails应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3654827/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com