log4j 打印错误的字符
有人报告我给他的使用 log4j 的程序无法正确打印字符。他告诉我“é”在文件中打印为“é”(例如:“Vidéo”变成“Vidéo”)。
这可能是一些编码问题,但我喜欢重现问题以证明它已修复。 我无法找到有关该主题的良好(且简短)文档,因此:
- 是什么导致了此问题(以及 log4j 如何选择编码?)?
- 可以通过简单地使用“log4j.appender.myappender.encoding=UTF-8”来修复吗?
谢谢您的帮助!
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:
- What causes this problem (and how does log4j chose the encoding?)?
- Can it be fixed by simply using "log4j.appender.myappender.encoding=UTF-8" ?
Thank you for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
WriterAppender
(它是FileAppender
及其变体的基类,具有setEcoding< /code> 方法。但是,使用
log4j.appender.myappender.encoding=UTF-8
应该可以正常工作。 “Vidéo”看起来正在编写 UTF-8,但无论您使用什么方式查看该文件,都会将其解释为其他某种编码(通常是某种 ISO-8859-* 编码或 ISO 之一) -derivatives)。
à
为 U+00C3,©
为 U+00A9。 ISO-8859-1。é
是 U+00E9,在 UTF-8 中编码为 0xC3 0xA9。WriterAppender
(which is the base class forFileAppender
and its variants. Has asetEcoding
method. So yes: usinglog4j.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.如果您的用户通过 SSH 查看日志文件,那么他们需要告诉 SSH 客户端也使用 UTF-8。
If your user is viewing the log files over SSH, then they need to tell their SSH client to use UTF-8 too.