简体   繁体   中英

rake test and error : log writing failed. “\xE2” from ASCII-8BIT to UTF-8

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.

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

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM