如何解决如何修复log4j2不记录到文件?
我正在修复旧的log4j属性文件,以切换到新的log4j2。但是,我注意到在运行代码时,它为.log
文件创建了一个文件夹结构,但是没有任何记录。阅读控制台时,我看到一条错误消息:
错误:
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
当我在网上查找时,其他人说这是因为java类找不到属性文件。因此,我添加了-Dlog4j.configuration="Z:/Projects/properties_file/log4j.properties
和行PropertyConfigurator.configure(“ Z:/Projects/properties_file/log4j.properties”)。尽管即使添加这些行仍然不能解决问题。
代码:
status = TRACE
name= properties_configuration
rootLogger.level = debug,info
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = fileLogger
rootLogger.appenderRef.console.ref = consoleLogger
logger.UserLogger.name = UserLogger
logger.UserLogger.level = debug
logger.UserLogger.additivity = false
logger.UserLogger.appenderRef.rolling.ref = fileLogger
logger.UserLogger.appenderRef.console.ref = consoleLogger
appender.console.layout.pattern = %d{dd-MM-yyyy HH:mm:ss.SSS}.-%t-%x-%-3p-%-5c:%m%n
appender.rolling.type = RollingFile
appender.rolling.name = fileLogger
appender.rolling.fileName= C:/projects/properties/logs/my_log.txt
appender.rolling.filePattern= C:/projects/properties/logs/my_log_%d{dd-MM-yyyy}.zip
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{dd-MM-yyyy HH:mm:ss.SSS}.-%t-%x-%-5p-%-10c:%m%n
appender.rolling.policies.type = Policies
appender.console.type = Console
appender.console.name = consoleLogger
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
解决方法
仔细查看您收到的错误消息。
log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
注意它说要查看log4j 1.2常见问题解答。这意味着您的应用程序仍在使用Log4j 1,而不是Log4j2。您需要确保项目中未包含log4j 1.x jar。另外,log4j.configuration属性是您为Log4j 1(而非Log4j 2)指定配置文件的方式。它使用log4j2.configurationFile。 Log4j 2也不使用PropertyConfigurator.configure()进行自我配置,并且该方法将与您提供log4j2.configurationFile属性冲突。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。