使用 Intellij 和 Kotlin 找不到 Log4j2 配置

con*_*ned 2 java intellij-idea kotlin log4j2

我用 Gradle 和 Kotlin DSL 构建脚本构建了一个新的 IntelliJ 项目,只有 Kotlin (Java) 和 Java 版本 10.0.2 作为项目 SDK。

我将 log4j 的依赖项添加到 build.gradle.kts 中:

compile("org.apache.logging.log4j:log4j-api:2.11.1")
compile("org.apache.logging.log4j:log4j-core:2.11.1")
Run Code Online (Sandbox Code Playgroud)

我将一个 log4j2.yaml 文件放入 /src/main/resources 并进行了一些配置。

当我现在运行这个测试程序时:

import org.apache.logging.log4j.LogManager

fun main(args: Array<String>) {
    val logger = LogManager.getLogger()!!

    logger.warn("Warn")
    logger.info("Info")
    logger.debug("Debug")
}
Run Code Online (Sandbox Code Playgroud)

我没有得到日志输出但是这条消息

ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2
Run Code Online (Sandbox Code Playgroud)

如何让 log4j2 工作?

这是我到目前为止所做的:

  • 我检查了 log4j2 配置是否已复制到构建目录并包含在类路径中
  • 我通过成功读取了资源文件 classloader.getResource("log4j2.yaml");
  • 我尝试了一个 xml 配置文件

con*_*ned 5

事实证明,如果您想使用 YAML 配置文件,这些依赖项是不够的。此外,这些是必需的:

compile("com.fasterxml.jackson.core:jackson-databind:2.9.4")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.4")
Run Code Online (Sandbox Code Playgroud)

将这些要求包含到 中后build.gradle.kts,日志记录按预期工作。