Pass argument to log4j file

2019-08-08 16:16发布

问题:

I have heard that a .properties file in java can accept String arguments ({0}, {1}, and so on...)

Can you please provide an example to show how to use these kind of arguments in the log4j.properties file?

Also, please help with a sample code showing how to pass the argument to the file while loading the file.

Links to any useful articles will also help

回答1:

Sample properties in log4j.properties

log4j.appender.file.File=${LOG_FILE_PATH}/${LOG_FILE_NAME}

Bear in mind that log4j will try to find log4j.properties and load automatically when initialised. Therefore, I can only think of passing the argument as JVM arguments.

In VM Arguments:

-DLOG_FILE_PATH=C:\log -DLOG_FILE_NAME=apps.log



回答2:

In log4j file:

log4j.appender.file.File=${log4jjava}

In your java code(the class that init log4j):

System.setProperty("log4jjava", PATH_YOU_WANT_TO_SET);
//then load your property file