14

を実行するrake testと、毎回エラーラインが表示されます。

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

これをご案内ください。このエラーを取り除く方法。

4

3 に答える 3

17

コードのどこかに、エンコーディングが間違っているか、奇妙な文字が含まれている文字列を出力しようとしていて、エンコーディングが機能しない可能性があります。

正しい方法は、問題が発生する文字列を見つけて、エンコードが間違っている理由を見つけることです。string api( http://ruby-doc.org/core-2.2.0/String.html )のencodingとforce_encodingを参照してください。

その行をログに出力してエラーを回避したいだけの場合は、その文字列を使用して次の手順を実行できます。

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
于 2013-03-19T02:13:15.107 に答える
4

postgresqlを使用していますか?データベースがSQL_ASCIIを使用している可能性があります。psql template1 -c 'show server_encoding'データベースクラスターの削除を実行し、で再初期化することで、それが正しいかどうかを確認できますinitdb -E utf8 /path/to/database

于 2015-02-03T00:42:00.820 に答える
1

私の場合、いくつかの奇妙な引用符が原因だったので、これらを次のような安全な文字に置き換えました。

msg = error.message.gsub(/[“”]/, "\"").gsub(/[‘’]/,"\'")
于 2015-12-08T12:33:51.593 に答える