log4j prints wrong characters

情到浓时终转凉″ 提交于 2019-12-23 10:54:51

问题


Somebody reports me that the program I gave him that uses log4j doesn't correctly print characters. He tells me that "é" gets printed in the file as "é" (for example: "Vidéo" becomes "Vidéo").

It's probably some encoding issue, but I like to reproduce problems to prove that it's fixed. I was unable to find good (and short) documentation on the subject so:

  1. What causes this problem (and how does log4j chose the encoding?)?
  2. Can it be fixed by simply using "log4j.appender.myappender.encoding=UTF-8" ?

Thank you for the help!


回答1:


WriterAppender (which is the base class for FileAppender and its variants. Has a setEcoding method. So yes: using log4j.appender.myappender.encoding=UTF-8 should simply work.

Note, however, that "Vidéo" becoming "Vidéo" looks like it is writing UTF-8, but whatever you use to view the file interprets it as some other encoding (usually that's some ISO-8859-* encoding or one of the ISO-derivatives).

à is U+00C3 and © is U+00A9. They are encoded as 0xC3 and 0xA9 in ISO-8859-1.

é is U+00E9 which is encoded as 0xC3 0xA9 in UTF-8.




回答2:


If your user is viewing the log files over SSH, then they need to tell their SSH client to use UTF-8 too.



来源:https://stackoverflow.com/questions/7024512/log4j-prints-wrong-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!