Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to convert log4j.properties to log4j.xml

Tags:

java

log4j

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.

like image 986
sbrian Avatar asked Dec 13 '09 21:12

sbrian


People also ask

How do I use RollingFileAppender in log4j properties?

3.1. If using RollingFileAppender , then use TimeBasedRollingPolicy to specify when to roll over log files based on date time. Notice the FileNamePattern property. It defines the name pattern for rolled over files. In given example, it will rename the rollover log files with date-month in log file name.


2 Answers

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.

like image 123
Jason Royals Avatar answered Sep 28 '22 11:09

Jason Royals


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
like image 21
Andreas Dolk Avatar answered Sep 28 '22 11:09

Andreas Dolk