gpt4 book ai didi

java - 如何配置 Gradle 应用程序以将 SLF4J 与 Log4J 2 实现一起使用?

转载 作者:行者123 更新时间:2023-12-04 17:20:58 28 4
gpt4 key购买 nike

我正在尝试配置一个 Gradle 项目(它使用 Lombok 注释处理器)以将 SLF4J 与 Log4j 2 实现一起使用。该项目 - 使用 gradle init 开始并选择一个 application - 具有以下结构:

.
├── app
│   ├── build
│   │   ├── classes
│   │   │   └── java
│   │   │   └── main
│   │   │   └── log4j
│   │   │   └── App.class
│   │   ├── generated
│   │   │   └── sources
│   │   │   ├── annotationProcessor
│   │   │   │   └── java
│   │   │   │   └── main
│   │   │   └── headers
│   │   │   └── java
│   │   │   └── main
│   │   └── tmp
│   │   └── compileJava
│   │   └── source-classes-mapping.txt
│   ├── build.gradle
│   └── src
│   ├── main
│   │   ├── java
│   │   │   └── log4j
│   │   │   └── App.java
│   │   └── resources
│   └── test
│   ├── java
│   │   └── log4j
│   │   └── AppTest.java
│   └── resources
├── gradle
│   └── wrapper
│   ├── gradle-wrapper.jar
│   └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── log4j2.xml
└── settings.gradle
我放了一个 log4j2.xml项目目录下的文件。根据 https://logging.apache.org/log4j/2.x/manual/configuration.html 的自动配置部分中的步骤 9 ,

If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.


我的 build.gradle具有所需的依赖项,
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.8.1/userguide/building_java_projects.html
*/

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}

repositories {
// Use JCenter for resolving dependencies.
jcenter()
}

dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13'

// This dependency is used by the application.
implementation 'com.google.guava:guava:29.0-jre'

implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha1'
implementation 'org.apache.logging.log4j:log4j-slf4j18-impl:2.14.0'

compileOnly 'org.projectlombok:lombok:1.18.16'
annotationProcessor 'org.projectlombok:lombok:1.18.16'

testCompileOnly 'org.projectlombok:lombok:1.18.16'
testAnnotationProcessor 'org.projectlombok:lombok:1.18.16'
}

application {
// Define the main class for the application.
mainClass = 'log4j.App'
}
App类本身是
package log4j;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class App {
public String getGreeting() {
return "Hello World!";
}

public static void main(String[] args) {
log.info("Printing a greeting...");
System.out.println(new App().getGreeting());
}
}
问题是,如果我使用 ./gradlew run 运行应用程序,它不打印 log.info()线,
> ./gradlew run

> Task :app:run
Hello World!
而如果我将其更改为 log.error() ,它确实被记录:
> ./gradlew run

> Task :app:run
19:26:50.782 [main] ERROR log4j.App - Printing a greeting...
Hello World!
由于我已将级别设置为 tracelog4j2.xml配置文件,
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
我建议 info如果配置文件被“拾取”,则显示 -level 日志。这向我表明它不在类路径中。我试过把它复制到 app目录,但无济于事。 log4j2.xml应该在哪里配置文件位于正确配置Log4J 2?

最佳答案

对于 log4j2.xml要在类路径上结束,文件必须移动到 src/main/resources .在官方文档中阅读更多关于资源的信息:

  • Building Java & JVM projects: Managing resources
  • The Java Plugin: Project layout
  • 关于java - 如何配置 Gradle 应用程序以将 SLF4J 与 Log4J 2 实现一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66271408/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com