java如何使用在Log4j中启用调试。属性文件和maven?
我正在尝试为maven项目启用打印调试信息。我将log4j作为依赖项添加到pom.xml
,并将log4j.properties
以及log4j2.properties
与rootloger=DEBUG, sdout
一起添加到src/main/resources
文件夹。然后在所需的类中,我在所需的类“org”中启动一个记录器。包装1。ClassA'和addlogger.debug()
行,但在我的领事中没有显示任何内容。当我选中logger.isDebugEnabled()
时,它返回false
pom。xml
<dependencies> ..... <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.25</version> </dependency>
log4j。属性和类似的log4j2
log4j.debug=true log4j.rootLogger=DEBUG, stdout log4j.appender.Stdout.threshold=debug log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%-5p %d [%t] %c{1} - %m%n
作为额外的步骤,我尝试添加
log4j.logger.extendedsldnf.ExtendedSLDNFEvaluator=DEBUG
但它也不起作用
package package1; ...... class ClassA{ private Logger logger = LoggerFactory.getLogger(getClass()); ...... public static void main(String []args){ logger.debug("message"); } }
知道我得到了警告
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
此外,该项目还有其他子模块,这些子模块也同样启用了日志记录,并且调试消息在log4j.properties
文件中声明时运行良好
有什么东西不见了吗?如何检查是否存在冲突强>
# 1 楼答案
如果您想使用log4j appender打印slf4j记录器的日志,请确保您有slf4j-log4j12 jar并移除logback jar,否则无论使用log4j还是logback,您都无法控制
# 2 楼答案
第一个用于log4j 1,第二个用于log4j 2。 声明两者都不是解决问题的方法
仅使用与实际log4j版本匹配的版本
事实上,问题相当清楚:您希望使用log4J实现作为SLF4J绑定,但在运行时类路径中至少有一个SLF4J绑定。这里提到了另一个绑定Logback中的
ContextSelectorStaticBinder
在运行时,必须在类路径中为SLF4J提供单个实现/绑定
要解决你的问题,很简单
在使用Maven时,我建议您从应用程序的agreegator pom执行
mvn dependency:tree
命令,如果您没有agreegator pom,则从打包应用程序的pom执行mvn dependency:tree
命令此命令将作为输出写入依赖项(包括pom提取的可传递依赖项)
它将输出如下内容:
您只需确定从何处提取回登录依赖项
一旦识别了它,如果它是从我们自己的pom显式声明的依赖项,那么就删除它
如果它是可传递依赖项,use the exclusion dependency mechanism of Maven