简体   繁体   English

适用于多种环境的log4j模板文件

[英]log4j template file for multiple environments

I wish to have a single WAR file that will work in different environments (like dev, ci and prod) and spring environments largely cater to our use cases. 我希望有一个WAR文件可以在不同的环境中工作(如dev,ci和prod),弹簧环境很大程度上适合我们的用例。 log4j per environment is one of the outstanding items. log4j每个环境是一个优秀的项目。 As suggested in other answers , we can have a log4j configuration per environment - but there is a lot of repetition in the files for each of the environments and the only kind of things that differ are minor properties like toAddress of SMTPAppender, etc.. Is there an easy way by which one can use a template-like log4j configuration file and have the parts that differ for each environment replaced at runtime? 正如其他答案中所建议的那样,我们可以为每个环境配置一个log4j配置 - 但是每个环境的文件都有很多重复,唯一不同的是像SMTPAppender的地址这样的次要属性等。有一种简单的方法可以使用类似模板的log4j配置文件,并为运行时替换的每个环境提供不同的部分吗? Maven will not work here because it will cause compile time choice. Maven不会在这里工作,因为它会导致编译时选择。

One solution I can think of is to extend the Log4jConfigurer to read some environment variables viz. 我能想到的一个解决方案是扩展Log4jConfigurer以读取一些环境变量即。 map of template key-value pairs and replace the template variables in the template file with the actual values before passing it down to the DOMConfigurator. 模板键值对的映射,并将模板文件中的模板变量替换为实际值,然后再将其传递给DOMConfigurator。 Just wanted to check if there are simpler ways of doing it or if I am barking up a wrong tree from a best practices point of view. 只是想检查是否有更简单的方法,或者从最佳实践的角度来看,我是在咆哮错误的树。

Thanks in advance! 提前致谢!

I don't fully understand your case but property placeholders should help: http://jvleminc.blogspot.ru/2008/05/avoid-absolute-path-in-log4j-properties.html 我不完全理解你的情况,但财产占位符应该有所帮助: http//jvleminc.blogspot.ru/2008/05/avoid-absolute-path-in-log4j-properties.html

  1. Use a placeholder in the properties file when you indicate the appender file, for instance 例如,当您指示appender文件时,请在属性文件中使用占位符

     log4j.appender.file.File=${log4j.logFile} 
  2. Pass this placeholder value as a VM parameter to the (web) application. 将此占位符值作为VM参数传递给(Web)应用程序。 The log4j engine will try to resolve the placeholder and eventually fall back on the System parameters, therefore, in our example: log4j引擎将尝试解析占位符并最终回退到System参数,因此,在我们的示例中:

     -Dlog4j.logFile=C:/logs/MyApplication.log 

Btw, I think usually people externalize logging configuration, ie they use external log4j.properties file. 顺便说一下,我认为人们通常会外化日志配置,即他们使用外部log4j.properties文件。 Release engineers manage these configurations per environment. 发布工程师根据环境管理这些配置。 Also it gives an ability to change the file and restart an app, or in some cases allows to change some values even without restart. 此外,它还提供了更改文件和重新启动应用程序的功能,或者在某些情况下允许更改某些值,甚至无需重新启动。

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

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