本文介绍了log4j打印错误字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人报告我我给他的使用log4j的程序无法正确打印字符.他告诉我在文件中将é"打印为Ã"(例如:Vidéo"变为Vidéo").

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. 是什么原因导致此问题(以及log4j如何选择编码?)?
  2. 是否可以仅使用"log4j.appender.myappender.encoding = UTF-8"来解决?

谢谢您的帮助!

推荐答案

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

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.

但是请注意,Vidéo"变成Vidéo"看起来像是正在编写UTF-8,但是您用来查看文件的任何内容都会将其解释为其他某种编码(通常是一些ISO-8859- *编码或ISO衍生物之一.

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

Ã是U + 00C3,©是U + 00A9.在ISO-8859-1中,它们被编码为0xC3和0xA9.

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

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

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

这篇关于log4j打印错误字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:16