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

当我运行rake test ,我每次都会得到错误行。

 log writing failed. "\xE2" from ASCII-8BIT to UTF-8 

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

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

正确的方法是找到导致问题的字符串,并找出错误编码的原因。 请参阅字符串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 eg "\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 

你在使用postgresql吗? 您的数据库可能正在使用SQL_ASCII。 您可以通过运行psql template1 -c 'show server_encoding'来检查它是否已删除数据库集群并使用initdb -E utf8 /path/to/database重新初始化它。

在我的情况下,一些奇怪的引号是罪魁祸首所以我只是用这样的安全字符替换它们,

 msg = error.message.gsub(/[“”]/, "\"").gsub(/['']/,"\'")