将 log4j .properties 更改为 log4j2 .properties
log4j.threshold=INFO
log4j.rootCategory=DEBUG, DEFAULT, ERROR
log4j.appender.DEFAULT=org.apache.log4j.RollingFileAppender
log4j.appender.DEFAULT.File=/logs/appname/${SERVER_NAME}/log.log
log4j.appender.DEFAULT.MaxFileSize=10MB
log4j.appender.DEFAULT.MaxBackupIndex=10
log4j.appender.DEFAULT.layout=org.apache.log4j.PatternLayout
log4j.appender.DEFAULT.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
log4j.appender.ERROR=org.apache.log4j.RollingFileAppender
log4j.appender.ERROR.maxFileSize=10MB
log4j.appender.ERROR.maxBackupIndex=10
log4j.appender.ERROR.File=/logs/appname/${SERVER_NAME}/logError.log
log4j.appender.ERROR.threshold=ERROR
log4j.appender.ERROR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
log4j.logger.org.apache = INFO
log4j.logger.org.opensaml = INFO
你好。您能帮我将 log4j 属性迁移到 log4j2 吗?第一个代码片段是原始的 .properties,第二个代码片段是我尝试创建 log4j2 .properties。
appender.DEFAULT.type = RollingFile
appender.DEFAULT.name = fileLogger1
appender.DEFAULT.fileName= /logs/appname/${SERVER_NAME}/log.log
appender.DEFAULT.layout.type = PatternLayout
appender.DEFAULT.layout.pattern = %d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
appender.DEFAULT.policies.type = Policies
appender.DEFAULT.policies.size.type = SizeBasedTriggeringPolicy
appender.DEFAULT.policies.size.size = 10MB
appender.ERROR.type = RollingFile
appender.ERROR.name = fileLogger2
appender.ERROR.fileName= /logs/appname/${SERVER_NAME}/logError.log
appender.ERROR.layout.type = PatternLayout
appender.ERROR.layout.pattern = %d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
appender.ERROR.policies.type = Policies
appender.ERROR.policies.size.type = SizeBasedTriggeringPolicy
appender.ERROR.policies.size.size = 10MB
rootLogger.appenderRefs = DEFAULT, ERROR
rootLogger.appenderRef.DEFAULT.ref = fileLogger1
rootLogger.appenderRef.ERROR.ref = fileLogger2
第二个片段正确吗?我应该如何在 log4j2 中实现以下几行?
log4j.threshold=INFO
log4j.appender.ERROR.threshold=ERROR
log4j.appender.DEFAULT.MaxBackupIndex=10
log4j.appender.ERROR.maxBackupIndex=10
log4j.logger.org.apache = INFO
log4j.logger.org.opensaml = INFO
log4j.threshold=INFO
log4j.rootCategory=DEBUG, DEFAULT, ERROR
log4j.appender.DEFAULT=org.apache.log4j.RollingFileAppender
log4j.appender.DEFAULT.File=/logs/appname/${SERVER_NAME}/log.log
log4j.appender.DEFAULT.MaxFileSize=10MB
log4j.appender.DEFAULT.MaxBackupIndex=10
log4j.appender.DEFAULT.layout=org.apache.log4j.PatternLayout
log4j.appender.DEFAULT.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
log4j.appender.ERROR=org.apache.log4j.RollingFileAppender
log4j.appender.ERROR.maxFileSize=10MB
log4j.appender.ERROR.maxBackupIndex=10
log4j.appender.ERROR.File=/logs/appname/${SERVER_NAME}/logError.log
log4j.appender.ERROR.threshold=ERROR
log4j.appender.ERROR.layout=org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern=%d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
log4j.logger.org.apache = INFO
log4j.logger.org.opensaml = INFO
Hello. Could you please help me with migrating my log4j properties to log4j2? The first snippet of code is the original .properties, the second one is my attempt to create log4j2 .properties.
appender.DEFAULT.type = RollingFile
appender.DEFAULT.name = fileLogger1
appender.DEFAULT.fileName= /logs/appname/${SERVER_NAME}/log.log
appender.DEFAULT.layout.type = PatternLayout
appender.DEFAULT.layout.pattern = %d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
appender.DEFAULT.policies.type = Policies
appender.DEFAULT.policies.size.type = SizeBasedTriggeringPolicy
appender.DEFAULT.policies.size.size = 10MB
appender.ERROR.type = RollingFile
appender.ERROR.name = fileLogger2
appender.ERROR.fileName= /logs/appname/${SERVER_NAME}/logError.log
appender.ERROR.layout.type = PatternLayout
appender.ERROR.layout.pattern = %d{dd.MM.yyyy HH:mm:ss.SSS} %-5p [%X{userName}][%X{myInterfaceName}][%t] %c - %m%n
appender.ERROR.policies.type = Policies
appender.ERROR.policies.size.type = SizeBasedTriggeringPolicy
appender.ERROR.policies.size.size = 10MB
rootLogger.appenderRefs = DEFAULT, ERROR
rootLogger.appenderRef.DEFAULT.ref = fileLogger1
rootLogger.appenderRef.ERROR.ref = fileLogger2
Is the second snippet correct? How should I implement the following lines in log4j2?
log4j.threshold=INFO
log4j.appender.ERROR.threshold=ERROR
log4j.appender.DEFAULT.MaxBackupIndex=10
log4j.appender.ERROR.maxBackupIndex=10
log4j.logger.org.apache = INFO
log4j.logger.org.opensaml = INFO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的配置看起来没问题。
MaxBackupIndex 属性并未消失,但已移至 滚动策略。您需要向附加程序添加一个带有
max
属性的DefaultRolloverStrategy
:其余属性是过滤器。有许多等效的方法可以向组件添加过滤器(参见过滤器文档< /a>):
log4j.threshold
属性可以替换为全局ThresholdFilter
:以同样的方式,您可以向附加程序添加
Threshold
过滤器,但是通过在AppenderRef
上设置level
属性可以更有效地完成此操作:Log4j.logger 前缀在 Log4j 1.x 中用于配置记录器。在 Log4j 2.x 中,您可以这样做:
Your configuration seems alright.
The
MaxBackupIndex
property is not gone, but it was moved to the rollover strategy. You need to add to your appenders aDefaultRolloverStrategy
with amax
attribute:The remaining properties are filters. There are many equivalent ways to add filters to components (cf. filters documentation):
the
log4j.threshold
property can be replaced by a globalThresholdFilter
:in the same way you can add a
Threshold
filter to the appenders, but that can be done more efficiently by setting thelevel
property on theAppenderRef
s:the
log4j.logger
prefix is used in Log4j 1.x to configure the loggers. In Log4j 2.x you can do it like:与这个问题不完全相关,但如果您要从 log4j 迁移到 log4j2,我建议阅读我下面的 stackoverflow 答案。
https://stackoverflow.com/a/76363764/2606228
Not Exactly Related to this question, but if your are migrating from log4j to log4j2, i suggest reading my below stackoverflow answer.
https://stackoverflow.com/a/76363764/2606228