Qbi*_*iek 85 java logging log4j2
最近我决定学习如何使用log4j2记录器.我下载了所需的jar文件,创建了库,xml配置文件,并尝试使用它.不幸的是我在控制台(Eclipse)中得到了这个声明:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Run Code Online (Sandbox Code Playgroud)
这是我的测试类代码:
package log4j.test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class Log4jTest
{
static final Logger logger = LogManager.getLogger(Logger.class.getName());
public static void main(String[] args) {
logger.trace("trace");
logger.debug("debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.fatal("fatal");
}
}
Run Code Online (Sandbox Code Playgroud)
和我的xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test"
status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Logger name="log4j.test.Log4jTest" level="trace">
<AppenderRef ref="Console"/>
</Logger>
<Root level="trace">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Run Code Online (Sandbox Code Playgroud)
我也试过xml没有<Logger>标签,包规范和各种文件夹/包目录,但它没有帮助.现在我的log4j2.xml文件直接位于eclipse中的项目文件夹中.
Tom*_*z W 136
这是一个没有maven等的简单的eclipse java项目吗?在这种情况下,您需要将log4j2.xml文件放在文件src夹下,以便能够在类路径中找到它.如果你使用maven将它放在src/main/resources或下src/test/resources
小智 30
您需要选择以下解决方案之一:
Jam*_*hin 14
正在关注文档 - Apache Log4j2 Configuratoin和Apache Log4j2 Maven在使用yaml配置log4j2时.根据文档,需要以下maven依赖项:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
和
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.8.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
只是添加这些没有选择配置,总是给出错误.通过添加调试配置的方式-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE有助于查看日志.后来不得不使用Maven下载源代码并调试有助于理解log4j2的依赖类.它们列在org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory:
com.fasterxml.jackson.databind.ObjectMapper
com.fasterxml.jackson.databind.JsonNode
com.fasterxml.jackson.core.JsonParser
com.fasterxml.jackson.dataformat.yaml.YAMLFactory
Run Code Online (Sandbox Code Playgroud)
添加依赖关系映射jackson-dataformat-yaml将不具有前两个类.因此,添加jackson-databind依赖项以使yaml配置工作:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
您可以通过参考MVN存储库中Test Dependencies的log4j-api版本项部分来添加版本.例如,对于log4j-api的2.8.1版本,请参阅此链接并找到版本.jackson-databind
此外,您可以使用以下Java代码检查类路径中是否有类:
System.out.println(ClassLoader.getSystemResource("log4j2.yml")); //Check if file is available in CP
ClassLoader cl = Thread.currentThread().getContextClassLoader(); //Code as in log4j2 API. Version: 2.8.1
String [] classes = {"com.fasterxml.jackson.databind.ObjectMapper",
"com.fasterxml.jackson.databind.JsonNode",
"com.fasterxml.jackson.core.JsonParser",
"com.fasterxml.jackson.dataformat.yaml.YAMLFactory"};
for(String className : classes) {
cl.loadClass(className);
}
Run Code Online (Sandbox Code Playgroud)