如何配置 Gradle 应用程序以将 SLF4J 与 Log4J 2 实现一起使用?
How to configure a Gradle application to use SLF4J with a Log4J 2 implementation?
我正在尝试配置一个 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
class 本身就是
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!
由于我在 log4j2.xml
配置文件中将级别设置为 trace
,
<?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>
如果配置文件是 'picked up',我建议显示 info
级别的日志。这向我表明它不在 class 路径中。我试过将它复制到 app
目录,但无济于事。 log4j2.xml
配置文件应该位于何处才能正确配置 Log4J 2?
要使 log4j2.xml
位于类路径中,必须将文件移至 src/main/resources
。在官方文档中阅读有关资源的更多信息:
我正在尝试配置一个 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、
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
class 本身就是
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!
由于我在 log4j2.xml
配置文件中将级别设置为 trace
,
<?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>
如果配置文件是 'picked up',我建议显示 info
级别的日志。这向我表明它不在 class 路径中。我试过将它复制到 app
目录,但无济于事。 log4j2.xml
配置文件应该位于何处才能正确配置 Log4J 2?
要使 log4j2.xml
位于类路径中,必须将文件移至 src/main/resources
。在官方文档中阅读有关资源的更多信息: