简体   繁体   中英

Log4j different .property files for Appenders

I was wondering if there is a way to define the appenders (file, console etc) on a different file than the one defining the actual logging properties. The idea came up from a system I am developing and we have the following requirement: Different versions of the system will be deployed on the same server. So in order not to maintain different log4j properties file, that will all set the same properties and differ on the file appenders (so as to know which log was recorded from which version of the system). Thank you in advance

You can use DOMConfigurator or PropertyConfigurator to load your log4j settings from an external file. You can invoke this API multiple times under a run to load the settings from different sources.

In your case, you can load the Appender details alone dynamically from another property file based on the version.Just like suffixing some version id to the file name and loading it from your code in a generic way.

If each version running on a different VM process (on different ports), you can add an argument to the virtual machine. eg:

-Dmysystem.version=1.0.1

If you are using the XML configuration:

<param name="file" value="/logs/system.v${mysystem.version}.log" />

Of if you are using the properties:

log4j.appender.ROLL.File=/logs/system.v${mysystem.version}.log

In both cases, the generated file might be:

/logs/system.v1.0.1.log

In this way, you can maintain a single configuration file and dynamic filenames.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM