耙测试和错误:日志写入失败. "\ xE2"从ASCII-8BIT到UTF-8 [英] rake test and error : log writing failed. "\xE2" from ASCII-8BIT to UTF-8

查看:253
本文介绍了耙测试和错误:日志写入失败. "\ xE2"从ASCII-8BIT到UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行rake test时,每次都会出现错误行.

When I run rake test, I get error line every time.

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

请对此进行指导.如何清除此错误.

Please guide on this. How to remove this error.

推荐答案

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

You probably have somewhere in your code trying to output string whose encoding is wrong or has some weird characters and the encoding doesn't work.

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

Correct way would be to find the string that results in problems and find out why its in wrong encoding. See encoding and force_encoding in string api (http://ruby-doc.org/core-2.2.0/String.html).

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

If you just want to be able to print that line in the log and avoid the error you can do the following procedure with that string.

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

这篇关于耙测试和错误:日志写入失败. "\ xE2"从ASCII-8BIT到UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆