how to change the log4j log file dynamically?

2019-02-18 02:21发布

I want to have a log4j configuration such that the log file name should have like ${System-name}log.log. that is if the application is launched on any system then without changing the configuration file or code. it should generate the log file name as mentioned. thanks.

标签: java logging
2条回答
倾城 Initia
2楼-- · 2019-02-18 03:09

I do it this way:
1) init logger by:

System.setProperty("my.logsDir", vcsLogsDir);
DOMConfigurator.configure("c:/log4j.xml");

2) in log4j.xml i use variable:

<param name="File" value="${my.logsDir}Default.log"/>d
查看更多
Rolldiameter
3楼-- · 2019-02-18 03:14

You can use Java system properties with the ${property} syntax. There are number of system properties that are defined by default including

"os.arch"     Operating system architecture
"os.name"     Operating system name
"os.version"  Operating system version

For a full list of default properties see API of System or list them with System.getProperties().list(System.out).

If the default properties won't do then you'll have top add your own properties using System.setProperty(property, value).

查看更多
登录 后发表回答