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
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'
}
and the App.java reads
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());
}
}
and app/src/main/resources contains the following:
# 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
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
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?