解决方案
./ .env
通常文件.env用于存储应用程序环境的所有重要/秘密属性。如果不存在则创建.env并添加代码:
RAILS_LOG_TO_STDOUT=true
./config/environments/*.rb
编辑您的环境文件(例如 ./config/environments/development.rb)以创建可断开的日志系统。
Rails.application.configure do
# ...
if ENV['RAILS_LOG_TO_STDOUT'] == 'true'
logger = ActiveSupport::Logger.new(STDOUT)
# To support a formatter, you must manually assign a formatter from the config.log_formatter value to the logger.
logger.formatter = config.log_formatter
# config.logger is the logger that will be used for Rails.logger and any
# related Rails logging such as ActiveRecord::Base.logger.
# It defaults to an instance of ActiveSupport::TaggedLogging that wraps an
# instance of ActiveSupport::Logger which outputs a log to the log/ directory.
config.logger = ActiveSupport::TaggedLogging.new(logger)
# config.log_level defines the verbosity of the Rails logger.
# This option defaults to :debug for all environments.
# The available log levels are: :debug, :info, :warn, :error, :fatal, and :unknown
#config.log_level = :debug
# config.log_tags accepts a list of: methods that the request object responds to,
# a Proc that accepts the request object, or something that responds to to_s.
# This makes it easy to tag log lines with debug information like subdomain and request id -
# both very helpful in debugging multi-user production applications.
config.log_tags = [:request_id]
end
end
用法
RAILS_LOG_TO_STDOUT=true
在.env中设置以打开控制台记录器
删除或注释掉行RAILS_LOG_TO_STDOUT=true
,或RAILS_LOG_TO_STDOUT=false
在.env中设置以关闭控制台记录器
如何在设置环境变量的情况下启动 Rails 服务器?
更多信息
配置 Rails 应用程序