I haven’t been aggressive for a long time, and I want to fiddle with new things. There is a problem that cannot be solved in log4j, so I want to try log4j2; I have never written yaml before, so I want to try a new configuration by the way.
The author uses maven to build Demo, and adds dependencies according to the instructions on the official website Configuration with YAML
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId> jackson-data-format-yaml </artifactId> <version>2.8.6</version> </dependency>
I am excited to write the simplest main method, print the info-level log, but keep reporting:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
I can't find the configuration file of log4j2. I checked it several times. The configuration file has been compiled successfully. How can it be found?
I have searched a lot of related articles on the Internet, but I can't find any similar problems. . .
Can't get stuck here, I really want to try log4j2, Debug the source code, this Debug, I'll go, the jackson-dataformat-yaml dependency package has some dependencies that have not been introduced, com.fasterxml.jackson. databind.ObjectMapper is red in the source file...
As a result, the constructor in YamlConfigurationFactory cannot properly load the Class of com.fasterxml.jackson.databind.ObjectMapper...
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" }; private final boolean isActive; public YamlConfigurationFactory() { for (final String dependency : dependencies) { if (!Loader.isClassAvailable(dependency)) { LOGGER.debug("Missing dependencies for Yaml support"); isActive = false; return; } } isActive = true; }
The result of isActive is false, and the configuration obtained by getConfiguration() of YamlConfigurationFactory is always null
@Override public Configuration getConfiguration(final LoggerContext loggerContext, final ConfigurationSource source) { if (!isActive) { return null; } return new YamlConfiguration(loggerContext, source); }
Solution:
Add jackson-databind dependency package
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.6</version> </dependency>