将 log4j .properties 更改为 log4j2 .properties

发布于 2025-01-09 22:48:47 字数 2388 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断舍离 2025-01-16 22:48:47

你的配置看起来没问题。

MaxBackupIndex 属性并未消失,但已移至 滚动策略。您需要向附加程序添加一个带有 max 属性的 DefaultRolloverStrategy

appender.DEFAULT.strategy.type = DefaultRolloverStrategy
appender.DEFAULT.strategy.max = 10
appender.ERROR.strategy.type = DefaultRolloverStrategy
appender.ERROR.strategy.max = 10

其余属性是过滤器。有许多等效的方法可以向组件添加过滤器(参见过滤器文档< /a>):

  • log4j.threshold 属性可以替换为全局 ThresholdFilter

    filter.1.type = ThresholdFilter
    过滤器.1.级别=信息
    
  • 以同样的方式,您可以向附加程序添加 Threshold 过滤器,但是通过在 AppenderRef 上设置 level 属性可以更有效地完成此操作:

    rootLogger.appenderRef.ERROR.level = 错误
    
  • Log4j.logger 前缀在 Log4j 1.x 中用于配置记录器。在 Log4j 2.x 中,您可以这样做:

    logger.1.name = org.apache
    logger.1.level = 信息
    logger.2.name = org.opensaml
    logger.2.level = 信息
    

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 a DefaultRolloverStrategy with a max attribute:

appender.DEFAULT.strategy.type = DefaultRolloverStrategy
appender.DEFAULT.strategy.max = 10
appender.ERROR.strategy.type = DefaultRolloverStrategy
appender.ERROR.strategy.max = 10

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 global ThresholdFilter:

    filter.1.type = ThresholdFilter
    filter.1.level = INFO
    
  • in the same way you can add a Threshold filter to the appenders, but that can be done more efficiently by setting the level property on the AppenderRefs:

    rootLogger.appenderRef.ERROR.level = ERROR
    
  • the log4j.logger prefix is used in Log4j 1.x to configure the loggers. In Log4j 2.x you can do it like:

    logger.1.name = org.apache
    logger.1.level = INFO
    logger.2.name = org.opensaml
    logger.2.level = INFO
    
情深缘浅 2025-01-16 22:48:47

与这个问题不完全相关,但如果您要从 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文