java log4j 打印错误的字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7024512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:20:05  来源:igfitidea点击:

log4j prints wrong characters

javalog4j

提问by AndrewBourgeois

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

有人报告我说我给他的使用 log4j 的程序不能正确打印字符。他告诉我“é”在文件中被打印为“??” (例如:“Vidéo”变成“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" ?
  1. 是什么导致了这个问题(以及 log4j 是如何选择编码的?)?
  2. 可以通过简单地使用“log4j.appender.myappender.encoding=UTF-8”来修复吗?

Thank you for the help!

感谢您的帮助!

回答by Joachim Sauer

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

WriterAppender(这是FileAppender及其变体的基类。有一个setEcoding方法。所以是的:使用log4j.appender.myappender.encoding=UTF-8应该很简单。

Note, however, that "Vidéo" becoming "Vid??o" looks like it iswriting 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).

但是请注意,“Vidéo”变成“Vid??o”看起来像是编写 UTF-8,但是无论您使用什么查看文件,都会将其解释为某种其他编码(通常是某种 ISO-8859-* 编码或一种ISO 衍生物)。

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

?是 U+00C3 和?U+00A9。它们在 ISO-8859-1 中编码为 0xC3 和 0xA9。

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

é是 U+00E9,它在 UTF-8 中被编码为 0xC3 0xA9。

回答by Rich

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

如果您的用户正在通过 SSH 查看日志文件,那么他们需要告诉他们的 SSH 客户端也使用 UTF-8。