Configuring log4j at runtime

I am using org.apache.tools.ant.listener.Log4jListener to control logging using my ant script. ant script has a high degree of configuration and is designed to run in various ways with different parameters, and therefore I need to be able to register in the files specified at runtime. I have log4j.properties, which defines the log file that will be build.log, and despite my attempts to run ant override the properties defined in log4j.properties, were unsuccessful.

The line ignores them and continues to write to build.log. I did not find much support in writing custom files, if only in Java their class is Logger.

Maybe I'm thinking about it wrong. log4j.properties is not treated the same as the properties file in ant script (therefore, it is overridden from the command line)? Is there a way that I can do this wisely without creating a custom task or anything else?

+3
source share
1 answer

You configure the log4j.properties file using a system property that can be dynamically defined on the command line. The property below is $ {logfile.name}. An example log4j configuration would be like this:

# logfile is set to be a RollingFileAppender
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=${logfile.name}
log4j.appender.logfile.MaxFileSize=10MB
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=[%-5p]%d{yyyyMMdd@HH\:mm\:ss,SSS}\:%c - %m%n

"ant" "-Dlogfile.name = {runtime path/filename }". {runtime path/filename } . ant, . log4j.properties .

http://ant.apache.org/manual/running.html

+1

Source: https://habr.com/ru/post/1782607/


All Articles