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

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

问题描述

Logback 1.1.3 LayoutWrappingEncoder 文档t 表示如果用户不设置默认字符集是什么,但源代码说:

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 的默认字符集)输出文件专业系统可能不是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?

推荐答案

Logback 字符编码

您可以在 PatternLayoutEncoder 的定义中使用 因为这是 LayoutWrappingEncoder 的子类,它提供了 setCharset 方法.这在文档中由类的摘录指示,但没有给出示例 xml 配置.对于 LayoutWrappingEncoder,这里给出了一个答案:[Logback-user]: 如何使用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).正确的做法是将 logback 配置为 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.

顺便说一下,Sun 之前曾说过 file.encoding,如果您在 Oracle VM 上设置它:

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天全站免登陆