如何将 log4j 日志文件更改为 utf8

2022-09-02 14:09:39

我得到了一个使用log4j作为记录器应用程序的代码。如何为它生成 UTF8 日志记录文件?log4j 创建的日志文件目前采用 ASCII 格式。

我试过以下方法

按照说明设置日志文件的文件编码

vi current
:set bomb
:set fileencoding=utf-8
:wq

事实上,在完成上述操作后,日志文件本身停止工作,即由于某种奇怪的原因,没有进一步的日志被写入文件

我还尝试更改.属性文件

log4j.rootLogger=TRACE, logfile

log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.encoding=UTF-8
log4j.appender.logfile.File=/usr/vm/log/webconsole/current
log4j.appender.logfile.Append=true
log4j.appender.logfile.MaxFileSize=5MB
log4j.appender.logfile.MaxBackupIndex=1
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%5p] %d{ISO8601} (%F:%M:%L)%n%m%n%n

log4j.logger.com.company.npm=ALL
log4j.logger.org.apache.axis=OFF

由于某种原因,日志文件在滚动更新后采用 ASCII 格式

我正在使用 log4j 1.2.8

当我启用log4j调试时,我得到了以下输出

@4000000053b16b142afa961c log4j: Parsing for [root] with value=[TRACE, logfile, cdr].
@4000000053b16b142afc630c log4j: Level token is [TRACE].
@4000000053b16b142afe85ec log4j: Category root set to DEBUG
@4000000053b16b142aff510c log4j: Parsing appender named "logfile".
@4000000053b16b142e3ebccc log4j: Parsing layout options for "logfile".
@4000000053b16b143333a0bc log4j: Setting property [conversionPattern] to [[%5p] %d{ISO8601} (%F:%M:%L)%n%m%n%n].
@4000000053b16b1433442f04 log4j: End of parsing for "logfile".
@4000000053b16b14343015a4 log4j: Setting property [file] to [/usr/vm/log/webconsole/current].
@4000000053b16b1434331b14 log4j: Setting property [append] to [true].
@4000000053b16b1434350b2c log4j: Setting property [maxFileSize] to [100KB].
@4000000053b16b14343885cc log4j: Setting property [maxBackupIndex] to [1].
@4000000053b16b14343a79cc log4j: Setting property [encoding] to [UTF-8].
@4000000053b16b14343d311c log4j: setFile called: /usr/vm/log/webconsole/current, true
@4000000053b16b1434937edc log4j: setFile ended
@4000000053b16b143496a774 log4j: Parsed "logfile" options.
@4000000053b16b1434a97fac log4j: Parsing appender named "cdr".
@4000000053b16b150111c014 log4j: Parsed "cdr" options.
@4000000053b16b1501166394 log4j: Parsing for [com.company.npm] with value=[ALL].
@4000000053b16b150118d87c log4j: Level token is [ALL].
@4000000053b16b15011abcdc log4j: Category com.company.npm set to ALL
@4000000053b16b150122003c log4j: Handling log4j.additivity.com.company.npm=[null]
@4000000053b16b150123d4fc log4j: Finished configuring.




@4000000053b16b17086f0114 log4j: rolling over count=102406
@4000000053b16b1708703d7c log4j: maxBackupIndex=1
@4000000053b16b170910fdd4 log4j: Renaming file /usr/vm/log/webconsole/current to /usr/vm/log/webconsole/current.1
@4000000053b16b1709140b14 log4j: setFile called: /usr/vm/log/webconsole/current, false
@4000000053b16b170916494c log4j: setFile ended

答案 1

它应该是:

    log4j.appender.FILE.encoding=UTF-8

答案 2

在 log4j 2 中,该属性似乎已被删除。相反,布局中的属性可用于编码。encodingcharset

http://logging.apache.org/log4j/2.x/manual/layouts.html


推荐