2

I wanted to experiment with using the YAML configuration file with log4j2, but log4j2 cannot load the configuration because the YamlConfigurationFactory cannot find its dependencies from the classpath.

The relevant section of my pom.xml:

<dependencies>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.1</version>
    </dependency>
</dependencies>

After that didn't work, I tried adding the <dependencyManagement> section to the pom:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-bom</artifactId>
            <version>2.1</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>

but that didn't help either. Anyone know what's wrong?

Also, if someone can point me to an example of YAML log4j2 config, that would be very much appreciated. (I just thought I would do a "quick" experiment with this, and of course, it became a time sink...)

2 Answers 2

5

Including the jackson-dataformat-yaml dependency is not enough. You also need to include jackson-core and jackson-databind.

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
        <version>2.4.2</version>
    </dependency>

You can see these dependencies are hardcoded in the YAML configuration factory implementation (org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory)

private static final String[] dependencies = new String[] {
            "com.fasterxml.jackson.databind.ObjectMapper",
            "com.fasterxml.jackson.databind.JsonNode",
            "com.fasterxml.jackson.core.JsonParser",
            "com.fasterxml.jackson.dataformat.yaml.YAMLFactory"
    };
1

I'm not really a maven user, but I think the issue here is that the YAML support is an optional feature, so it doesn't get pulled in automatically by specifying the log4j dependency. The Log4j Runtime Dependencies page lists the Jackson YAML data format as the (only) dependency required for YAML configuration support. Quoting the maven dependency snippet from there:

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-yaml</artifactId>
  <version>2.4.0</version>
</dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.