18
votes

I have been handed a code which uses log4j as the logger application. How can I generate a UTF8 logging file for it?. The logfile being created by log4j is in ASCII format at the moment.

I have tried the follow

Setting the file encoding of the log file by following instructions

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

Infact after doing the above the logfile itself stops working ie no further logs get written to the file for some strange reason

I have also tried changing the .properties file

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

The log file after it rolls over is in ASCII format for some reason

I am using log4j 1.2.8

When I enabled log4j debugging I got the following output

@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
2
log4j.appender.logfile.encoding=UTF-8 should work. How did you determine that it didn't?DanielBarbarian
When the logfile rolled over the new file that was created was in ASCII formatManuj
@Manuj do you mean that you didn't restart the application after changing log configuration?Dmitry Ginzburg
@Manuj: AFAIK: should be good. ASCII is a proper subset of UTF-8. So anything that is valid 7-bit ASCII is by definition ALSO valid UTF-8.StackzOfZtuff

2 Answers

2
votes

It should be:

    log4j.appender.FILE.encoding=UTF-8
0
votes

In log4j 2, the encoding attribute seems to be removed. Instead, the charset attribute in Layout can be used for encoding.

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