在一个新的rails 7.1.2应用程序中,可以在config/environments/production.rb中找到以下几行:

config.logger = ActiveSupport::Logger.new(STDOUT)
  .tap  { |logger| logger.formatter = ::Logger::Formatter.new }
  .then { |logger| ActiveSupport::TaggedLogging.new(logger) }

这会告诉Rails记录器将记录到STDOUT.

我想配置它,让它也记录到log/production.log,但我无论如何也想不出来……

In this article by Fly.io它说要添加以下几行:

logger = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
volume_logger = ActiveSupport::Logger.new("/logs/production.log", 3)
logger = logger.extend ActiveSupport::Logger.broadcast(volume_logger)

但这些说明似乎是针对Rails<7.1的,因为我收到了ActiveSupport::Logger:Class`的错误NoMethodError: undefined method Broadcast‘.

如何在Rails 7.1中实现这一点?

推荐答案

Rails v7.1 added处理广播的新BroadcastLogger类:

stdout_logger           = ActiveSupport::Logger.new(STDOUT)
stdout_logger.formatter = ::Logger::Formatter.new

file_logger             = ActiveSupport::Logger.new("log/production.log")
file_logger.formatter   = ::Logger::Formatter.new

tagged_stdout_logger    = ActiveSupport::TaggedLogging.new(stdout_logger)
tagged_file_logger      = ActiveSupport::TaggedLogging.new(file_logger)

broadcast_logger = ActiveSupport::BroadcastLogger.new(tagged_stdout_logger, tagged_file_logger)
config.logger    = broadcast_logger

100

100

Ruby-on-rails相关问答推荐

尽管安装并需要 pry-rails gem,但 Rails 容器内出现未定义方法‘pry’错误

一对多关联 counter_cache 在 Rails 中无法正确重新加载

刺激不添加侦听器以搜索表单输入

Rails - 如何从 http://example.com 重定向到 https://www.example.com

如何使用 Vim 插入 ERB 标签?

在 JS 模块中使用 Rails-UJS(带有 webpacker 的 Rails 6)

jbuilder vs rails-api/active_model_serializers 用于 Rails 4 中的 JSON 处理

FactoryGirl + Faker - 为数据库种子数据中的每个对象生成相同的数据

安装mysql2(0.4.8)时出错,Bundler无法继续

如何在rails中发回js.haml

如何从邮箱中获取域

在 Rails 生产中禁用assets资源 缩小

ActiveRecord 查找现有表索引

通过 Ruby 或 Rails 的 LDAP

用户注册时设计确认令牌无效

即使X-Frame-Options为ALLOWALL,也无法在 iframe 中显示我的 rails 4 应用程序

Ruby on Rails:如何在 select_tag 中使用默认占位符?

如何设置 MiniTest?

保存用户偏好的最佳方法?

Date.current 和 Date.today 有什么区别?