Intellij 和 Kotlin 找不到 Log4j2 配置
Log4j2 configuration not found with Intellij and Kotlin
我用 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 配置文件
事实证明,如果您想使用 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
后,日志记录按预期工作。
如果有人在使用 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>
我用 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 配置文件
事实证明,如果您想使用 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
后,日志记录按预期工作。
如果有人在使用 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>