Script to convert log4j.properties to log4j.xml

2020-04-02 08:29发布

问题:

I need to use custom filters, so I need to convert some long log4j.properties files to log4j.xml.

Is anyone aware of a tool to do this, or willing to contribute one they have used? Searching has thus far turned up no such tool.

回答1:

I needed to do this as well, but couldn't find a tool. Migrating by hand dozens of log4j.properties was not a palatable option. So, I bashed together a tool that can do it and released it for others to use.

http://code.google.com/p/log4j-properties-converter/

It's a bit rough but did the trick for the log4j properties I gave it, so any problems log them on the issue tracker. Hope you find it useful.



回答2:

Here is something that may help you. Ceki Gülcü, the creator of log4j, started another logger project named logback, and he provides an online translator for log4j.properties files to xml config files for logback. It looks like the configuration file schemas of log4j.xml and logback.xml are pretty close.

At least it should produce something that can easily be converted to the log4j.xml format.

For your convenience: here's a sample log4j.properties file from the log4j documentation. Just paste it into the translator and check the output:

log4j.rootLogger=debug, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n


回答3:

Presumably you must have a lot of properties files you need to convert. When I wanted to switch to xml (many years ago) I couldn't find much documentation. I did find sample xml files in the log4j distribution which were very useful.



回答4:

Recently used https://github.com/balent/Log4j-Convert-Configuration to convert a log4j.xml file to a log4j.properties file (an ancient jar I had to use was explicitly looking for log4j.properties). The configuration allows going both ways: xml to properties OR properties to xml.

Download the binary build from https://github.com/balent/Log4j-Convert-Configuration/blob/master/binaries/full-with-dependencies/log4j-convert-full.jar and then just type:

java -jar log4j-convert-full.jar -i log4j.properties -o log4j.xml

Leaving this on here for future me to find.



标签: java log4j