title: How to configure a Gradle application to use SLF4J with a Log4J 2 implementation?
body: <p>I'm trying to configure a Gradle project (which uses Lombok annotation processors) to use SLF4J with a Log4j 2 implementation. The project - which was started using <code>gradle init</code> and selecting an <code>application</code> - has the following structure:</p>
  <pre><code>.
  ├── 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
  </code></pre>
  <p>I've put a <code>log4j2.xml</code> file in the project directory. According to step 9 in the Automatic Configuration section of <a href="https://logging.apache.org/log4j/2.x/manual/configuration.html" rel="noreferrer">https://logging.apache.org/log4j/2.x/manual/configuration.html</a>,</p>
  <blockquote>
  <p>If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.</p>
  </blockquote>
  <p>My <code>build.gradle</code> has the needed dependencies,</p>
  <pre><code>/*
   * 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 &amp; 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'
  }
  </code></pre>
  <p>and the <code>App</code> class itself is</p>
  <pre class="lang-java prettyprint-override"><code>package log4j;
  
  import lombok.extern.slf4j.Slf4j;
  
  @Slf4j
  public class App {
      public String getGreeting() {
          return &quot;Hello World!&quot;;
      }
  
      public static void main(String[] args) {
          log.info(&quot;Printing a greeting...&quot;);
          System.out.println(new App().getGreeting());
      }
  }
  </code></pre>
  <p>The problem is that if I run the application using <code>./gradlew run</code>, it doesn't print the <code>log.info()</code> line,</p>
  <pre><code>&gt; ./gradlew run
  
  &gt; Task :app:run
  Hello World!
  </code></pre>
  <p>whereas if I change it to <code>log.error()</code>, it does get logged:</p>
  <pre><code>&gt; ./gradlew run
  
  &gt; Task :app:run
  19:26:50.782 [main] ERROR log4j.App - Printing a greeting...
  Hello World!
  </code></pre>
  <p>Since I've set the level to <code>trace</code> in the <code>log4j2.xml</code> configuration file,</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;Configuration status=&quot;WARN&quot;&gt;
      &lt;Appenders&gt;
          &lt;Console name=&quot;Console&quot; target=&quot;SYSTEM_OUT&quot;&gt;
              &lt;PatternLayout pattern=&quot;%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n&quot;/&gt;
          &lt;/Console&gt;
      &lt;/Appenders&gt;
      &lt;Loggers&gt;
          &lt;Root level=&quot;trace&quot;&gt;
              &lt;AppenderRef ref=&quot;Console&quot;/&gt;
          &lt;/Root&gt;
      &lt;/Loggers&gt;
  &lt;/Configuration&gt;
  </code></pre>
  <p>I would suggest <code>info</code>-level logs to get displayed if the configuration file were 'picked up'. This suggest to me that it is not in the class path. I've tried copying it to the <code>app</code> directory but to no avail. Where should the <code>log4j2.xml</code> configuration file be located to correctly configure Log4J 2?</p>