Pass argument to log4j file

爱⌒轻易说出口 提交于 2019-12-11 04:29:56

问题


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


来源:https://stackoverflow.com/questions/17442087/pass-argument-to-log4j-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!