简体   繁体   English

Log4j Appenders的不同.property文件

[英]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). 因此,为了不维护不同的log4j属性文件,所有文件都将设置相同的属性,并在文件追加程序上有所不同(以便知道从哪个版本的系统记录了哪个日志)。 Thank you in advance 先感谢您

You can use DOMConfigurator or PropertyConfigurator to load your log4j settings from an external file. 您可以使用DOMConfigurator或PropertyConfigurator从外部文件加载log4j设置。 You can invoke this API multiple times under a run to load the settings from different sources. 您可以在一次运行中多次调用此API,以从其他来源加载设置。

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. 在您的情况下,您可以根据版本从另一个属性文件单独动态加载Appender详细信息,就像在文件名中添加一些版本ID并以通用方式从代码中加载它一样。

If each version running on a different VM process (on different ports), you can add an argument to the virtual machine. 如果每个版本在不同的VM进程 (不同的端口)上运行,则可以向虚拟机添加参数。 eg: 例如:

-Dmysystem.version=1.0.1

If you are using the XML configuration: 如果您使用的是XML配置:

<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. 这样,您可以维护单个配置文件和动态文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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