首页 > 解决方案 > Log4j maven dependency won't log to console

问题描述

this is my first try with Log4j, when I run the code I got the following error:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...

Here is my code, syntax and logic seems right, should I configure/create any files?

package main;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class Main {

    private static final Logger logger = LogManager.getLogger(Main.class);
    
    public static void main(String[] args) {

        try {
            
            logger.info("START.");
            Service.readConfig();
            
        } catch (Exception e) {
            
        }
        
    }

}

标签: javamavenloggingconsolelog4j

解决方案


由于您没有任何配置文件,因此 log4j 使用默认配置,即将根记录器分配给ERROR 日志记录级别。在记录器上配置级别将导致该级别的日志记录事件以及更具体的事件通过过滤器。INFO,这是您在调用时记录的级别logger.info()不太具体,因此不会被记录。您需要将记录器配置为至少记录INFO,或者记录ERRORFATAL使用相应的方法。您可以在 log4j 的文档中阅读如何配置记录器级别。


推荐阅读