java - 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) {
}
}
}
解决方案
由于您没有任何配置文件,因此 log4j 使用默认配置,即将根记录器分配给ERROR
日志记录级别。在记录器上配置级别将导致该级别的日志记录事件以及更具体的事件通过过滤器。INFO
,这是您在调用时记录的级别logger.info()
不太具体,因此不会被记录。您需要将记录器配置为至少记录INFO
,或者记录ERROR
或FATAL
使用相应的方法。您可以在 log4j 的文档中阅读如何配置记录器级别。
推荐阅读
- ios - iOS:scanForPeripheralsWithServices 无法找到设备,而 nRF Connect 应用程序能够找到它
- python - 再次按下按钮后如何使窗口刷新?
- pandas - 逐行平均回报 nan
- unity3d - 使用行为树进行玩家移动的想法有多好?
- dynamics-365 - Dynamics 365 for field service 未在实体控件部分显示“可编辑网格”选项
- c# - OpenXml SpreadsheetDocument SaveAs() 生成损坏的文档 - 为什么?
- c# - 将多个列表合并到一个列表中,每个列表都有属性
- html - 当 HTML 和 CSS 在同一个编辑器页面中时,如何在鼠标悬停时放大图像
- apache-spark - Spark Streaming 接收器只处理一条记录
- asciidoc - 是否可以使用 `xxx` 在 AsciiDoctor 中定义词干公式?