rake测试和错误:日志写入失败."\ xE2"从ASCII-8BIT到UTF-8

Man*_*ava 14 logging ruby-on-rails ruby-on-rails-3.2

当我跑步时rake test,我每次都会得到错误.

log writing failed. "\xE2" from ASCII-8BIT to UTF-8  
Run Code Online (Sandbox Code Playgroud)

请指导一下.如何删除此错误.

hol*_*lli 17

您可能在代码中的某处尝试输出编码错误或具有一些奇怪字符的字符串,并且编码不起作用.

正确的方法是找到导致问题的字符串,并找出错误编码的原因.请参阅字符串api中的encoding和force_encoding(http://ruby-doc.org/core-2.2.0/String.html).

如果您只是希望能够在日志中打印该行并避免错误,则可以使用该字符串执行以下过程.

str = 'here is some string with wrong encoding and funny characters e.g. "\xE2"'
# note if you do this in console, the str is encoded correctly

# Rails.logger.info(str) # this would result in the error line

# This will change the encoding and remove weird characters
str = str.encode('utf-8', :invalid => :replace, :undef => :replace, :replace => '_')

Rails.logger.info(str) # this now works
Run Code Online (Sandbox Code Playgroud)