log4j2.properties 无法配置导入包使用的 slf4j 记录器
log4j2.properties cannot config slf4j loggers used by imported package
我在 log4j2.properties
文件中创建 rootLogger
时遇到一些问题。
正如你从我下面的 log4j2.properties
文件中看到的,我已经用 info
的 level
定义了 rootLogger
,并将其指向 rolling
] appender RollingFile
.
但是,当我 运行 程序时,只有从我的包生成的日志才会进入滚动文件附加程序,并指定正确的日志级别,在本例中为 info
。
但是我导入的包(我的依赖,比如org.apache.kafka.clients.consumer.KafkaConsumer
)生成的日志并没有写到rolling文件中。而是打印在控制台上,而且不是指定级别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
更新 1
我发现了问题,但正在寻找解决方案。
问题是:我正在使用 org.apache.logging.log4j.Logger
,但我导入的包正在使用 org.slf4j.Logger
。这就是为什么我的 log4f2.properties
无法控制来自导入包的记录器(例如,kafka-clients)。
那么,我的问题是:如何解决这个问题?
更新 2
我试图通过将 log4j-slf4j-impl
放入 pom.xml
来将其导入到我的项目中。它不起作用。
更新 3
导入 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 binder,它与[=中的binder复杂化了31=].
所以我在 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。
我在 log4j2.properties
文件中创建 rootLogger
时遇到一些问题。
正如你从我下面的 log4j2.properties
文件中看到的,我已经用 info
的 level
定义了 rootLogger
,并将其指向 rolling
] appender RollingFile
.
但是,当我 运行 程序时,只有从我的包生成的日志才会进入滚动文件附加程序,并指定正确的日志级别,在本例中为 info
。
但是我导入的包(我的依赖,比如org.apache.kafka.clients.consumer.KafkaConsumer
)生成的日志并没有写到rolling文件中。而是打印在控制台上,而且不是指定级别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
更新 1
我发现了问题,但正在寻找解决方案。
问题是:我正在使用 org.apache.logging.log4j.Logger
,但我导入的包正在使用 org.slf4j.Logger
。这就是为什么我的 log4f2.properties
无法控制来自导入包的记录器(例如,kafka-clients)。
那么,我的问题是:如何解决这个问题?
更新 2
我试图通过将 log4j-slf4j-impl
放入 pom.xml
来将其导入到我的项目中。它不起作用。
更新 3
导入 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 binder,它与[=中的binder复杂化了31=].
所以我在 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。