带有 log4j 的 slf4j,没有配置文件,但我可以在文件中看到日志

slf4j with log4j, no configuration files but I can see logs in file

在一个项目上使用 slf4j 和 log4j(coa 我可以看到所有需要的 log4j jar 文件就位。不使用环回或 java 日志记录实用程序。) 但是我在任何地方都找不到任何配置文件(例如 xml 文件)。 但我可以看到所有日志都转到一个 DEBUG 级别打开的文件。 我的问题是,这是 log4j 的默认行为吗?或者它必须存在于某处的配置文件? 如果我添加配置文件,哪里是正确的地方? 谢谢。

默认配置是仅将错误记录到控制台。(log4j2)

rootlogger 默认用于调试,但它只是带有基本配置器 (log4j1) 的控制台

来自官方文档

This occurs when the default configuration files log4j.properties and log4j.xml can not be found and the application performs no explicit configuration. log4j uses Thread.getContextClassLoader().getResource() to locate the default configuration files and does not directly check the file system. Knowing the appropriate location to place log4j.properties or log4j.xml requires understanding the search strategy of the class loader in use. log4j does not provide a default configuration since output to the console or to the file system may be prohibited in some environments.

如果您看不到配置文件(log4j.xml 最有可能),可以使用 DOMConfigurator class 在代码中配置它,或者可以将配置文件指定为命令行参数(-Dlog4j.configurationFile 我认为,对于 log4j 1.x 可能有所不同)。

通常您将配置文件放在 classpath 目录的根目录中,例如 main class 是 src/com/Main.class,配置文件将位于src/ 文件夹。

我会搜索 import 语句 import org.apache.log4j,如果您使用的是 slf4j,则除了自定义 config/logging

外,不应有任何对此包的引用