spring - 如何在 Spring 5 和 Tomcat 8 (Log4jConfigListener) 中加载外部 log4j 日志配置
问题描述
我有一个部署到 Tomcat 8.5.15 的 java spring 5 应用程序。Java 应用程序包含 log4j 框架和(默认)配置文件“log4j.properties”。
在某些情况下,我想加载一个外部 log4j 文件,它会覆盖应用程序中打包的文件,这可能吗?
您可以在之前使用 Spring 的“Log4jConfigListener”来做到这一点。
解决方案
是的,您可以通过编写少量代码来使用它的外部配置设置。
// import org.apache.logging.log4j.core.LoggerContext;
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
// this will force a reconfiguration
context.setConfigLocation(file.toURI());
参考:链接
如果您不想这样做,*.xml
则可以使用其他类型,例如JSON
, YAML
, properties
...
1. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Note that this is not restricted to a location on the local file system and may contain a URL.
2. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
3. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
4. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
5. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
6. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
7. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
8. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
9. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
10. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
参考:链接
推荐阅读
- mongodb - Spark - MongoDb - 与 pyspark 版本相比,java 中的 dataframe.limit(2) 慢
- ios - iOS Swift 分段控件与用于切换的容器视图 - 视图未首次加载
- text - Keras predict_proba 为每个输入预测相同的概率(LSTM)
- vim - Vdebug - 加载时最大化 DebuggerWatch 窗口
- python - 在 python 中作为模块调用时,类如何继承 tk.frames
- python - 创建一个新的 virtualenv 项目并为现有项目创建一个 virtualenv 会导致错误
- asp.net - AddOrUpdate() 抛出错误:使用“身份”模式修改列 - 我应该如何处理?
- java - 尽管可以看到和分配 web 元素,但无法单击复选框
- c++ - 升压测试无法找到自定义打印
- ruby-on-rails - Vue.js 应用程序在开发中工作,但不能在 Rails 5.2.0 / Webpacker 的生产中安装模板 - 控制台中没有错误的空白屏幕