在Gradle Java应用程序中未获取Log4j2.properties文件? [英] Log4j2.properties file not picked up in Gradle Java application?
问题描述
我正在使用 Gradle应用程序插件,并尝试配置日志记录通过SLF4J和Log4J 2实现.我已经将 log4j2.properties
文件放在 src/main/resources
中:
I'm using the Gradle application plugin and trying to configure logging through SLF4J with a Log4J 2 implementation. I've put a log4j2.properties
file in src/main/resources
:
.
├── app
│ ├── build
│ │ ├── classes
│ │ │ └── java
│ │ │ └── main
│ │ │ └── log4j
│ │ │ └── App.class
│ │ ├── generated
│ │ │ └── sources
│ │ │ ├── annotationProcessor
│ │ │ │ └── java
│ │ │ │ └── main
│ │ │ └── headers
│ │ │ └── java
│ │ │ └── main
│ │ ├── resources
│ │ │ └── main
│ │ │ └── log4j2.properties
│ │ └── tmp
│ │ └── compileJava
│ │ └── source-classes-mapping.txt
│ ├── build.gradle
│ └── src
│ ├── main
│ │ ├── java
│ │ │ └── log4j
│ │ │ └── App.java
│ │ └── resources
│ │ └── log4j2.properties
│ └── test
│ ├── java
│ │ └── log4j
│ │ └── AppTest.java
│ └── resources
├── gradle
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
└── settings.gradle
我的 build.gradle
包含Log4j 2 SLF4J绑定( https://logging.apache.org/log4j/2.x/log4j-slf4j-impl/):
My build.gradle
contains the Log4j 2 SLF4J binding (https://logging.apache.org/log4j/2.x/log4j-slf4j-impl/):
/*
* 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.java
读取
package log4j;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class App {
public String getGreeting() {
return "Hello World!";
}
public static void main(String[] args) {
log.debug("Printing a greeting...");
System.out.println(new App().getGreeting());
}
}
和 app/src/main/resources
包含以下内容:
# Define the root logger with appender file
log4j.rootLogger = DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n
因此,如果我在应用程序中执行 log.debug()
,我希望将日志级别设置为 DEBUG
,并将某些内容打印到控制台.但是,如果我 ./gradlew运行
该应用程序,则看不到日志行:
Thus, I would expect the log level to get set to DEBUG
and something to get printed to the console if I log.debug()
in the application. However, if I ./gradlew run
the application I don't see a log line:
> ./gradlew run
> Task :app:run
Hello World!
BUILD SUCCESSFUL in 953ms
3 actionable tasks: 1 executed, 2 up-to-date
但是,我没有看到这样的日志行,这表明未拾取"配置文件.如何获得此Java应用程序来提取 log4j2.properties
文件?
However, I see no such log line, suggesting that the configuration file wasn't 'picked up'. How can I get this Java application to pick up the log4j2.properties
file?
推荐答案
您应该配置 log4j2
的详细信息,而不是 log4j
.我的意思是:
You are supposed to configure log4j2
with specifics from it, not with log4j
. What I mean by that is:
appenders = console
appender.console.type = Console
appender.console.name = LogToConsole
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %p\t%d{ISO8601}\t%r\t%c\t[%t]\t%m%n
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = LogToConsole
例如.如果通过上述方法更改 log4j2.properties
的内容,我会看到日志记录.
for example. If change the contents of log4j2.properties
with the above, I do see the logging.
这篇关于在Gradle Java应用程序中未获取Log4j2.properties文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!