log4j 2.X的Log4j属性文件升级

ulydmbyx  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(212)

我们正在将Log4J从1.X升级到2.X。这是旧的log4J属性文件-
我已经从文档和一些研究中找到了如何编写滚动文件附加器和控制台。但是由于我是日志框架的新手,我很难为第三方库/框架(如旧属性文件中的spring框架)Map日志程序。
如果有人能提供任何指点,将不胜感激。
旧的log4j.properties-

log4j.rootLogger=INFO, stdout, fileout

log4j.logger.org.springframework.security=INFO, stdout, fileout
log4j.logger.org.springframework.aop.framework.autoproxy=INFO, stdout, fileout
log4j.logger.org.springframework.aop.framework.autoproxy.metadata=INFO, fileout
log4j.logger.org.springframework.aop.framework.autoproxy.target=INFO, fileout
log4j.logger.org.springframework.transaction.interceptor=INFO, fileout
log4j.logger.org.acegisecurity.intercept=INFO, fileout
log4j.logger.org.acegisecurity.intercept.method=INFO, fileout
log4j.logger.org.acegisecurity.intercept.web=INFO, fileout
log4j.logger.org.acegisecurity.afterinvocation=INFO, fileout
log4j.logger.org.acegisecurity.acl=INFO, fileout
log4j.logger.org.acegisecurity.acl.basic=INFO, fileout
log4j.logger.org.acegisecurity.taglibs.authz=INFO, fileout
log4j.logger.org.acegisecurity.ui.basicauth=INFO, fileout
log4j.logger.org.acegisecurity.ui.rememberme=INFO , fileout
log4j.logger.org.acegisecurity.ui=INFO, fileout
log4j.logger.org.acegisecurity.ui.rmi=INFO, fileout
log4j.logger.org.acegisecurity.ui.httpinvoker=INFO, fileout
log4j.logger.org.acegisecurity.util=INFO, fileout
log4j.logger.org.acegisecurity.providers.dao=INFO, fileout

# log4j.logger.org.acegisecurity=DEBUG, stdout, fileout

log4j.logger.org.acegisecurity=INFO, fileout
log4j.logger.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=OFF
log4j.logger.org.hibernate.stat.internal.StatisticsImpl.queryExecuted=OFF
log4j.org.hibernate.SQL.logStatement = ERROR, fileout, console

log4j.logger.org.apache.commons.vfs2.impl.StandardFileSystemManager=INFO, fileout

log4j.logger.org.quartz.core.QuartzSchedulerThread=INFO, fileout
log4j.logger.org.hibernate.hql.internal.ast.HqlSqlWalker=ERROR, fileout

# Console output...

log4j.defaultInitOverride = false
log4j.rootCategory = INFO, console

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.conversionPattern=[%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n

# Rolling log file output...

log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=logs/restAPIlog.log

# log4j.appender.fileout.File=${webapp.root}/WEB-INF/log4j.log

log4j.appender.fileout.MaxFileSize=2048KB
log4j.appender.fileout.MaxBackupIndex=9
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=[%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n

# Report logging

reportlogging.directory=logs

新的log4j2.properties-

status = error
dest = err
name = PropertiesConfig

property.filename = logs/restAPIlog.log

appenders = console, rolling
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%n

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = ${filename}-%d{MM-dd-yy-HH-mm-ss}
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss,SSS}] %-5p [%c.%M()] (%t) %m%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=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

rootLogger.level = info
rootLogger.appenderRefs = console, rolling
rootLogger.appenderRef.console.ref = STDOUT
rootLogger.appenderRef.rolling.ref = RollingFile

pom.xml-

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.17.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.17.1</version>
  </dependency>
nkhmeac6

nkhmeac61#

首先请注意,Log4j 1.x配置中存在一个潜在问题:默认情况下,每个记录器也会记录其父记录器的附加器。这在Log4j中称为 “可加性”。例如,发送到org.acegisecurity.intercept.method的消息将在fileout记录器中记录4次。在Log4j 1.x中纠正此问题的方法是将可加性设置为false

log4j.logger.org.acegisecurity.intercept.method=INFO, fileout
log4j.additivity.org.acegisecurity.intercept.method=false

您可以轻松地将上面的摘录转换为Log4j 2.x <Logger>标记(XML语法):

<Logger name="org.acegisecurity.intercept.method" level="INFO" additivity="false">
    <AppenderRef ref="fileout" />
</Logger>

属性语法要详细得多,但可以很容易地从XML语法推导出来:

logger.<identifier1>.name=org.acegisecurity.intercept.method
logger.<identifier1>.level=INFO
logger.<identifier1>.additivity=false
logger.<identifier1>.appenderRef.<identifier2>.ref=fileout

其中<identifier1><identifier2>是没有点的字符串。

相关问题