What does log4j2 file appenders use for file encoding/format?
Asked Answered
S

3

13

What file format does log4j2 use when it writes to rollingfileappender? Furthermore, is there a way to change it? What I'd like to do use use UTF-16.

Seeing answered 15/4, 2016 at 19:33 Comment(0)
H
10

The Charset is specified on the Layout. AbstractStringLayout defaults to UTF-8.

You should be able to change that by specifying charset="UTF-16" on the Layout configuration.

Hamadryad answered 15/4, 2016 at 19:54 Comment(3)
Thank you! It did not occur to me it was part of the pattern as I was looking all over the appenders for it. Information on layouts are hereSeeing
+10. It may be worth pointing out that most layouts including PatternLayout extend AbstractStringLayout so they all accept a charset parameter.Rik
You are right! It works in my log4j2.xml <PatternLayout pattern="<mypattern>" charset="UTF-8" />Jetport
R
10

Small correction on Ralph's answer: for PatternLayout the default charset is the system default. This is important because for example the Windows console won't be able to correctly display UTF-8 on Japanese Windows.

Rik answered 16/4, 2016 at 5:45 Comment(0)
L
2

If you use log4j.properties, configure it as follow:

appender.rolling.layout.charset = UTF-16

or make the corresponding configurations in your xml configuration file.

Littell answered 21/12, 2021 at 10:31 Comment(1)
You should provide a larger excerpt of your configuration, because the current content of your answer can give the wrong impression that appender.rolling.layout.charset is a universally valid configuration property. In reality rolling and layout are arbitrary identifiers.Repugnance

© 2022 - 2024 — McMap. All rights reserved.