LayoutWrappingEncoder的LogBack默认字符集? [英] LogBack default charset for LayoutWrappingEncoder?

查看:468
本文介绍了LayoutWrappingEncoder的LogBack默认字符集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Logback 1.1.3 LayoutWrappingEncoder文档并未指出默认值如果用户未设置字符集,则为字符集,但源代码显示为:

The Logback 1.1.3 LayoutWrappingEncoder documentation doesn't indicate what the default charset will be if the user doesn't set it, but the source code says:

默认情况下,此属性的值为null,它对应于系统的默认字符集.

By default this property has the value null which corresponds to the system's default charset.

但是我使用的是PatternLayoutEncoder(带有RollingFileAppender),它似乎是以UTF-8格式输出文件的(而Windows 7 Professional系统的默认字符集可能不是UTF-8).

However I'm using a PatternLayoutEncoder (with a RollingFileAppender), and it seems to be outputting files in UTF-8 (and the default charset of my Windows 7 Professional system is probably not UTF-8).

UTF-8输出实际上是我想要的,但是我想确保我不会偶然得到它,因为文档似乎指示了其他内容.那么,为什么在我未明确指定字符集的情况下,Logback会给我提供UTF-8输出?

UTF-8 output is actually what I want, but I want to make sure I'm not getting this by chance, since the documentation seems to indicate something else. So why is Logback giving me UTF-8 output when I haven't explicitly specified a charset?

推荐答案

登录字符编码

您可以在PatternLayoutEncoder的定义中使用<charset>,因为这是LayoutWrappingEncoder的子类,它提供了setCharset方法.文档中该类的摘录指出了这一点,但是没有给出示例xml配置.对于LayoutWrappingEncoder,此处给出了答案: [Logback用户] :如何使用UTF-8 .

You can use <charset> in the definition of your PatternLayoutEncoder as this is a subclass of LayoutWrappingEncoder, which provides the setCharset method. This is indicated in the documentation by an excerpt from the class, but no example xml configuration is given. For the LayoutWrappingEncoder an answer has been given here: [Logback-user]: How to use UTF-8.

因此,如果您通过代码进行配置,则可以使用UTF-8调用setCharset方法.或者,如果您是通过xml配置的,则为:

So if you configure via code you can call the setCharset method with UTF-8. Or if you are configuring via xml this is:

<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
        <charset>UTF-8</charset>            
        <outputPatternAsHeader>true</outputPatternAsHeader>
        <pattern>[%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>

默认文件编码

Logback的文档正确地表明使用了默认字符编码.默认字符集通常不是Windows上的UTF-8(例如,我的字符为windows-1252).正确的做法是将登录配置为如上所述的UTF-8.即使logback从某个地方获取UTF-8,或者您以某种方式设置了file.encoding,也无法保证将来会发生这种情况.

Logback's documentation is correct in stating that the default character encoding is used. The default character set is not typically UTF-8 on windows (mine is windows-1252 for instance). The correct thing to do it configure logback to be UTF-8 as above. Even if logback is picking UTF-8 up from somewhere, or file.encoding is somehow being set by you, there's no guarentee that this will happen in the future.

如果您是在Oracle VM上进行设置的,那么Sun之前曾提到过file.encoding:

Incidentally Sun had previously said about file.encoding, if you are setting this on an Oracle VM:

J2SE平台规范不需要"file.encoding"属性;它是Sun实现的内部细节,不应由用户代码检查或修改.它也打算是只读的.从技术上讲,在命令行上或程序执行期间的任何其他时间,都无法支持将此属性设置为任意值.

The "file.encoding" property is not required by the J2SE platform specification; it's an internal detail of Sun's implementations and should not be examined or modified by user code. It's also intended to be read-only; it's technically impossible to support the setting of this property to arbitrary values on the command line or at any other time during program execution.

Eclipse和Maven

如果您从eclipse运行maven,并且已经将环境设置为UTF-8(用于环境/项目)或运行配置"(在公共选项卡中对我来说),那么eclipse将安排新的JVM通过设置file.encoding具有UTF-8编码.请参阅: Eclipse的编码文档

If you are running maven from eclipse and you've already set your environment to be UTF-8 either in for the environment/project or the Run Configuration (for me in the common tab) then eclipse will arrange for the new JVM to have UTF-8 encoding by setting file.encoding. See: Eclipse's encoding documentation

这篇关于LayoutWrappingEncoder的LogBack默认字符集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆