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

时间:2023-01-05 20:49:12

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

当我运行rake测试时,我每次都会得到错误行。

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

Please guide on this. How to remove this error.

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

3 个解决方案

#1


16  

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.

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

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).

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

#2


4  

Are you using postgresql? Your database might be using SQL_ASCII. You can check if it is by running psql template1 -c 'show server_encoding' Delete the database cluster and reinitialize it with initdb -E utf8 /path/to/database.

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

#3


1  

In my case some weird quotation marks were the culprit so I just replaced them with the safe characters like this,

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

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

#1


16  

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.

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

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).

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

#2


4  

Are you using postgresql? Your database might be using SQL_ASCII. You can check if it is by running psql template1 -c 'show server_encoding' Delete the database cluster and reinitialize it with initdb -E utf8 /path/to/database.

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

#3


1  

In my case some weird quotation marks were the culprit so I just replaced them with the safe characters like this,

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

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