[英]log4j2.properties cannot config slf4j loggers used by imported package
我在log4j2.properties
文件中创建rootLogger
log4j2.properties
一些问题。
从下面的log4j2.properties
文件中可以看到,我已经用info
level
定义了rootLogger
,并将其指向rolling
附加程序RollingFile
。
但是,当我运行程序时,只有从程序包生成的日志才会转到滚动文件附加程序,并指定了正确的日志级别,在这种情况下为info
。
但是从我导入的打包文件(例如,我的依赖项,例如org.apache.kafka.clients.consumer.KafkaConsumer
)生成的日志不会写入滚动文件中。 而是将其打印在控制台上,并且不带有指定的级别info
,因为甚至会打印出DEBUG日志。 看来我指定的rootLogger
从未创建且有效。
以一种方式,为什么rootLogger无法正常工作? ; 以另一种方式,如何控制从导入的软件包生成的日志?
顺便说一句,我以这种方式使用log4j2.properties
:
java -Dlog4j.configurationFile=/path/to/my/log4j2.properties [options] xxx
log4j2.properties:
status = info
name = PropertiesConfig
property.directory = logs
property.filename = kafka.log
appenders = console, rolling
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${directory}${sys:file.separator}${filename}
appender.rolling.filePattern = ${directory}${sys:file.separator}kafka-%d{yyyy-MM-dd-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.rolling.policies.type = Policies
#appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
#appender.rolling.policies.time.interval = 2
#appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 50MB
loggers = rolling
logger.rolling.name = kafka
logger.rolling.level = info
logger.rolling.additivity = false
logger.rolling.appenderRefs = rolling
logger.rolling.appenderRef.rolling.ref = RollingFile
rootLogger.level = info
rootLogger.additivity = false
rootLogger.appenderRefs = rolling
rootLogger.appenderRef.rolling.ref = RollingFile
我找到了问题,但正在寻找解决方案。
问题是:我正在使用org.apache.logging.log4j.Logger
但是我导入的包正在使用org.slf4j.Logger
。 这就是为什么我的log4f2.properties
无法从导入的软件包(例如, kafka-clients )控制记录器的原因。
然后,我的问题是:我该如何解决这个问题?
我试图通过将log4j-slf4j-impl
放入pom.xml
导入到我的项目中。 它不起作用。
导入log4j-slf4j-impl
,我看到了:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:xxx1]
SLF4J: Found binding in [jar:file:xxx2]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
然后,我回顾了所有依赖关系及其依赖关系,发现其中一个依赖于logback
,并且此logback包含它logger绑定程序,这使log4j-slf4j-impl
的绑定程序变得复杂。
因此,我在pom中的依赖项中添加了以下内容:
<dependencies>
<dependency>
<groupId>aaa</groupId>
<artifactId>bbb</artifactId>
<version>2.0.0</version>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</exclusion>
</exclusions>
</dependency>
...
</dependencies>
繁荣! 问题解决了! slf4j的记录器正在使用我提供的log4j2.properties
。
我自己解决了。 请参阅问题部分中的更新1、2、3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.