【问题标题】:Log4j2 configuration not found with Intellij and Kotlin使用 Intellij 和 Kotlin 找不到 Log4j2 配置
【发布时间】:2019-02-27 18:30:48
【问题描述】:

我使用 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")

我将 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")
}

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

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

如何让 log4j2 工作?

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

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

【问题讨论】:

标签: java intellij-idea kotlin log4j2


【解决方案1】:

如果有人在使用 maven。请在项目中添加以下依赖项。

<dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>2.10.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.10.4</version>
    </dependency>

【讨论】:

    【解决方案2】:

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

    compile("com.fasterxml.jackson.core:jackson-databind:2.9.4")
    compile("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.4")
    

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

    【讨论】:

      猜你喜欢
      • 2014-10-18
      • 2014-12-30
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 2018-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多