我将我的(非Rails)ActiveRecord SQL日志记录重定向到我的spec_helper
中包含以下内容的文本文件:
logfile = File.expand_path('log/test.log')
FileUtils.mkdir_p File.dirname(logfile)
ActiveRecord::Base.logger = Logger.new(logfile) if defined?(ActiveRecord::Base)
这很好用,除了日志文件充满了ANSI颜色转义。我想这是一个功能,如果您在终端中查看它,但在IDE /编辑器中查看文件时非常烦人:
如何构建输出纯文本的Logger
?
ETA:不,如果您想知道,您将在互联网上找到旧的ActiveRecord::Base.colorize_logging
标记,以便早期的ActiveRecord版本不再存在,大概是因为假设只有Rails开发人员需要纯文本日志。
/Users/me/.rvm/gems/ruby-2.2.2/gems/activerecord-4.2.3/lib/active_record/dyna
mic_matchers.rb:26:in `method_missing': undefined method `colorize_logging='
for ActiveRecord::Base:Class (NoMethodError)
答案 0 :(得分:-1)
只是一个猜测(如果我误解了你的问题,不要开枪):
ActiveRecord::Base includes a variety of configuration options:
colorize_logging (true by default) specifies whether or not to use ANSI color codes when logging information from ActiveRecord.
http://guides.rubyonrails.org/v2.3.11/configuring.html
中的更多详情对于Rails 3.0及其ActiveRecord版本,您需要设置Rails::LogSubscriber
或config.colorize_logging
。来自the 3.0 release notes:
ActiveRecord::Base.colorize_logging and
config.active_record.colorize_logging are deprecated in favor of
Rails::LogSubscriber.colorize_logging or config.colorize_logging