我创建了一个spring boot rest api并计划使用log4j进行日志记录。我能够使用dailyrollingfileappender进行日志记录,但我不喜欢文件名[app.log.date]的格式,所以我尝试使用log4j附加功能。我遇到问题我在启动tomcat时遇到错误
" log4j:错误无法实例化类[TimeBasedRollingPolicy]。 java.lang.ClassNotFoundException:TimeBasedRollingPolicy"
这是我的pom.xml log4j依赖项
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>apache-log4j-extras</artifactId>
<version>1.2.17</version>
</dependency>
这是我的log4j属性文件(目前正在修改auditLogger)
log4j.rootLogger=INFO, stdout
log4j.logger.auditLogger = INFO, auditLogger
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.appender.generalLogger=org.apache.log4j.DailyRollingFileAppender
log4j.appender.generalLogger.File=C:\\logs\\sms-syslog.log
log4j.appender.generalLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.generalLogger.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.generalLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
log4j.category.generalLogger=INFO, generalLogger
log4j.additivity.generalLogger=false
log4j.appender.auditLogger=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.auditLogger.rollingPolicy=TimeBasedRollingPolicy
log4j.appender.auditLogger.File=C:\\logs\\sms-audit.log
log4j.appender.auditLogger.rollingPolicy.FileNamePattern=C:\\logs\\sms-audit-%d{yyyy-MM-dd}.log
log4j.appender.auditLogger.layout=org.apache.log4j.PatternLayout
log4j.appender.auditLogger.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p: %m%n
log4j.additivity.auditLogger=false
这是我在代码
中对auditlogger的调用private static final Logger auditLogger = Logger.getLogger("auditLogger");
请帮忙。我很失落谢谢你!
答案 0 :(得分:0)
使用以下TimeBasedRollingPolicy
依赖项:
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.2</version>
</dependency>
请参阅以下链接以供参考:https://examples.javacodegeeks.com/enterprise-java/logback/logback-rollingfileappender-example/
答案 1 :(得分:0)
所以答案是由皮特提供的。
属性文件很好,我只需要FQN。
log4j.appender.auditLogger.rollingPolicy=org.apache.log4j.rolling.TimeBasedRollingPolicy