如何为带有模块的 gradle 项目设置 log4j?

how do you set up log4j for a gradle project with modules?

如何为 gradle 带有模块的项目设置 log4j?

我的项目设置如下:

project root
build.gradle
gradle.properties
settings.gradle
// this root project does:
// include CommonModule
// includeBuild <all composite modules within module folder>

---CommonModule
------build.gradle
------gradle.properties
------settings.gradle
------src/main/groovy/...<common-code>
------src/main/resources/log4j2.xml

Modules
---OtherModule-1 
// this is a gradle composite module
// it also includes the common module 
------build.gradle
------gradle.properties
------settings.gradle
------src/main/groovy/...module1-code
------src/main/resources/log4j2.xml
---OtherModule-2 
// this is a gradle composite module
// it also includes the common module 
------build.gradle
------gradle.properties
------settings.gradle
------src/main/groovy/...module2-code
------src/main/resources/log4j2.xml

如上所示,我们有一个common gradle 模块和一个包含模块相关复合模块的模块文件夹,每个都依赖于包含的CommonModule (核心通用代码是通用的,每个复合模块都包含扩展通用内容的代码

我的问题很简单:

我去 gradle 论坛 https://discuss.gradle.org/t/how-do-you-set-up-log4j-for-a-gradle-project-with-modules/28262 上问了同样的问题并得到了答案 -

  • 公共模块不包含 log4j 配置独立库
  • 模块不包含 log4j 配置顶级应用程序
  • 模块确实包含 log4j 配置,但没有将其放在 类路径(我预留了一个 src/[configuration]/config 目录 为此)
  • 应用程序本身的启动器(无论是来自发布版 build 或来自 Gradle build 中的 JavaExec)指定 log4j config是从中加载的,这样就不会不小心获取到 被类路径上的其他配置中毒
  • 您可以通过在 gradle 属性文件中提供 -D 标志来定义 xml 位置,例如-Dlog4j.configurationFile=
  • 还有 -Dlog4j.debug 可能有助于追踪问题