简体   繁体   中英

Script to convert log4j.properties to log4j.xml

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.

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.

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

Late answer, but nowadays a class named Log4j1ConfigurationConverter from the log4j bridge API offers batch(recursive) conversion from log4jv1.properties to log4jv2.xml config.

From the javadocs: "Tool for converting a Log4j 1.x properties configuration file to Log4j 2.x XML configuration file."

Example call that converts all files named log4j.properties found recursively in a given path:

 java org.apache.log4j.config.Log4j1ConfigurationConverter --recurse
 <directory-path-to-convert> --in log4j.properties --verbose

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM