Log4j2配置 - 找不到log4j2配置文件 [英] Log4j2 configuration - No log4j2 configuration file found

查看:1573
本文介绍了Log4j2配置 - 找不到log4j2配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我决定学习如何使用log4j2记录器。我下载了所需的jar文件,创建了库,xml配置文件并尝试使用它。不幸的是我在控制台(Eclipse)中得到了这个声明:

Lately I decided to learn how to use the log4j2 logger. I downloaded required jar files, created library, xml comfiguration file and tried to use it. Unfortunately i get this statement in console (Eclipse) :

ERROR StatusLogger No log4j2 configuration file found. Using default configuration:       logging only errors to the console.

这是我的测试类代码:

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");
    }
}

我的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>

我还试过xml而没有< Logger> 标签,包规范和各种文件夹/包目录,但它没有帮助。现在我的 log4j2.xml 文件直接位于eclipse中的项目文件夹中。

I tried also with xml without <Logger> tags, and package specification and in various folders/packages directories, but it didn't help. Now my log4j2.xml file is located directly in project folder in eclipse.

推荐答案

这是一个没有maven等的简单的eclipse java项目吗?在这种情况下,您需要将 log4j2.xml 文件放在 src 文件夹下,以便能够找到它在类路径上。
如果你使用maven将它放在 src / main / resources src / test / resources

Is this a simple eclipse java project without maven etc? In that case you will need to put the log4j2.xml file under src folder in order to be able to find it on the classpath. If you use maven put it under src/main/resources or src/test/resources

这篇关于Log4j2配置 - 找不到log4j2配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆