2

I built a fresh IntelliJ project with Gradle and the Kotlin DSL build script, only Kotlin (Java) and java version 10.0.2 as project SDK.

I added the dependencies for log4j into build.gradle.kts:

compile("org.apache.logging.log4j:log4j-api:2.11.1")
compile("org.apache.logging.log4j:log4j-core:2.11.1")

And I put a log4j2.yaml file into /src/main/resources with some configuration.

When I now run this test program:

import org.apache.logging.log4j.LogManager

fun main(args: Array<String>) {
    val logger = LogManager.getLogger()!!

    logger.warn("Warn")
    logger.info("Info")
    logger.debug("Debug")
}

I do not get logging output but this message

ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations. Set system property 'log4j2.debug' to show Log4j 2 internal initialization logging. See https://logging.apache.org/log4j/2.x/manual/configuration.html for instructions on how to configure Log4j 2

How do I get log4j2 to work?

Here's what I've done so far:

  • I checked that the log4j2 config is copied to the build directory and included in the classpath
  • I successfully read the resource file via classloader.getResource("log4j2.yaml");
  • I tried an xml config file instead
contradictioned
  • 1,253
  • 2
  • 14
  • 26
  • 1
    If I'm not mistaken [already solved a similar problem](https://stackoverflow.com/questions/28572783/no-log4j2-configuration-file-found-using-default-configuration-logging-only-er). –  Sep 24 '18 at 10:03
  • Going through the answers of that thread: Cleaning does not help/is not applicable, putting the config in src/main/{java,kotlin} instad of src/main/resources does not help, but this helped, see answer in a minute: https://stackoverflow.com/a/40421738/1216878 PS: Thanks :) – contradictioned Sep 24 '18 at 10:38

2 Answers2

5

Turns out, that these dependencies are not enough if you want to use YAML config files. Additionally these are required:

compile("com.fasterxml.jackson.core:jackson-databind:2.9.4")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.9.4")

After including these requirements into build.gradle.kts, logging worked as expected.

contradictioned
  • 1,253
  • 2
  • 14
  • 26
2

if any one is using maven. please add bellow dependancies to the project.

<dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>2.10.4</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.10.4</version>
    </dependency>
arvin_v_s
  • 1,036
  • 1
  • 12
  • 18