什么是最好的 Rails Logging Gem

发布于 2024-11-26 14:09:16 字数 1565 浏览 2 评论 0原文

在 Rails 项目上配置日志记录功能的最佳方法是什么?我正在寻找像 Log4J 这样可用于 Rails 的东西。我发现 log4r 和它在 Logger 类中内置的冲突,并且还尝试了“Logging”gem,并且它在配置为审核记录器时存在一些问题。请让我知道您对此主题的建议,因为我是该主题的初学者。

我在logging.rb中使用了以下代码块并包含在environment.rb中 但我收到了“returning”关键字的错误,因为它在 Rails 2.8

config/environment.rb

# Logging
require File.join(File.dirname(__FILE__), 'logging')

Rails::Initializer.run do |config|

config/logging.rb上已弃用

require 'logging'

# Logging.init is required to avoid 
#   unknown level was given 'info' (ArgumentError)
# or
#   uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
Logging.init :debug, :info, :warn, :error, :fatal

layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"

# Default logfile, history kept for 10 days
default_appender = Logging::Appenders::RollingFile.new 'default', \
  :filename => 'log/default.log', :age => 'daily', :keep => 10, :safe => true, :layout => layout

# Audit logfile, history kept forever
audit_appender = Logging::Appenders::RollingFile.new 'audit', \
  :filename => 'log/audit.log', :age => 'daily', :safe => true, :layout => layout

# Production logfile, history kept forever
prod_appender = Logging::Appenders::RollingFile.new 'prod', \
  :filename => 'log/production.log', :age => 'daily', :safe => true, :layout => layout

DEFAULT_LOGGER = returning Logging::Logger['server'] do |l|
  l.add_appenders default_appender
end

what is the best way of configuring Logging features on a rails project? I'm looking for something like Log4J which is available to Rails. I have found log4r and it's conflicting built in Logger class and also tried 'Logging' gem and It has some issues configuring as a audit logger. Please let me know your suggestions on this topic since I'm a beginner on the subject.

I have used below code block in logging.rb and included in environment.rb
But I'm receiving a error on 'returning' keyword as it's deprecated on rails 2.8

config/environment.rb

# Logging
require File.join(File.dirname(__FILE__), 'logging')

Rails::Initializer.run do |config|

config/logging.rb

require 'logging'

# Logging.init is required to avoid 
#   unknown level was given 'info' (ArgumentError)
# or
#   uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
Logging.init :debug, :info, :warn, :error, :fatal

layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"

# Default logfile, history kept for 10 days
default_appender = Logging::Appenders::RollingFile.new 'default', \
  :filename => 'log/default.log', :age => 'daily', :keep => 10, :safe => true, :layout => layout

# Audit logfile, history kept forever
audit_appender = Logging::Appenders::RollingFile.new 'audit', \
  :filename => 'log/audit.log', :age => 'daily', :safe => true, :layout => layout

# Production logfile, history kept forever
prod_appender = Logging::Appenders::RollingFile.new 'prod', \
  :filename => 'log/production.log', :age => 'daily', :safe => true, :layout => layout

DEFAULT_LOGGER = returning Logging::Logger['server'] do |l|
  l.add_appenders default_appender
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪推晚风 2024-12-03 14:09:16

它应该是这样的:

config/logging.rb

require 'logging'

# Logging.init is required to avoid
#   unknown level was given 'info' (ArgumentError)
# or
#   uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
Logging.init :debug, :info, :warn, :error, :fatal

layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"

# Default logfile, history kept for 30 days
default_appender = Logging::Appenders::RollingFile.new 'default', \
  :filename => "log/#{Rails.env}.log", :age => 'daily', :keep => 30, :safe => true, :layout => layout

log = Logging::Logger[:root]
log.add_appenders default_appender
log.level = :info

Rails.logger = log

It should be like this:

config/logging.rb

require 'logging'

# Logging.init is required to avoid
#   unknown level was given 'info' (ArgumentError)
# or
#   uninitialized constant Logging::MAX_LEVEL_LENGTH (NameError)
# when an Appender or Layout is created BEFORE any Logger is instantiated:
Logging.init :debug, :info, :warn, :error, :fatal

layout = Logging::Layouts::Pattern.new :pattern => "[%d] [%-5l] %m\n"

# Default logfile, history kept for 30 days
default_appender = Logging::Appenders::RollingFile.new 'default', \
  :filename => "log/#{Rails.env}.log", :age => 'daily', :keep => 30, :safe => true, :layout => layout

log = Logging::Logger[:root]
log.add_appenders default_appender
log.level = :info

Rails.logger = log
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文