【问题标题】:Spring Boot logging - log4j2 logging not working?Spring Boot 日志记录 - log4j2 日志记录不起作用?
【发布时间】:2021-11-19 00:06:04
【问题描述】:

互联网上有很多帖子建议如何从 logback 迁移到 log4j2 以进行 Spring Boot 日志记录。
参考这个软帖子 -Spring-Boot logging with log4j2 - 我已将我的项目配置为使用 log4j2。然后我添加了一些基本的 log4j2.xml 和 log4j2.properties 文件进行测试。

但我的项目在这些更改后无法进行任何日志记录。谁能帮我找出问题所在?

项目代码在 github - https://github.com/ramshers/spring-security-jwt/tree/log4j2_configuration 分支上可用:log4j2_configuration 和 commit-id:5e65e7d em>

这里是项目 pom.xml - https://github.com/ramshers/spring-security-jwt/blob/log4j2_configuration/pom.xml#L26
这里是 log4j2 属性文件 -
https://github.com/ramshers/spring-security-jwt/blob/log4j2_configuration/src/main/resources/log4j2.properties
https://github.com/ramshers/spring-security-jwt/blob/log4j2_configuration/src/main/resources/log4j2.xml

【问题讨论】:

    标签: spring spring-boot log4j2 spring-logback


    【解决方案1】:

    我查看了存储库代码,并对属性文件进行了一些更改。您可以使用以下内容作为起点(注意:这里的模式不同,只需根据您的意愿进行修改即可)。 另请注意:我必须从您的 application.properties 文件中删除属性,因为它们现在正在从 log4j2.properties 文件中获取。

    log4j2 属性文件

    rootLogger.level = debug
    rootLogger.appenderRefs = stdout
    rootLogger.appenderRef.stdout.ref = STDOUT
    
    appenders = console
    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} [%t] %c{1} - %msg%n
    
    

    修改主类以测试它是否正常工作

    @SpringBootApplication
    public class SpringSecurityJwtApplication {
    
        private  static final Logger logger = LoggerFactory.getLogger(SpringSecurityJwtApplication.class);
        
        public static void main(String[] args) {
            
            SpringApplication.run(SpringSecurityJwtApplication.class, args);
            
            logger.info("Running..., info level");
            logger.debug("Running..., debug level");
        }
    }
    

    【讨论】:

    • 感谢和抱歉迟到了。 1) 完成您建议的更改后,它开始工作,但 log4j.rootLogger=DEBUGlog4j.appender.STDOUT.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n 值仍然没有被拾取
    • 2) 你能帮我修复一下 log4j2.xml 文件吗
    • 更新代码推送到github
    • 我使用改进的属性文件编辑了帖子,还请注意,我们可以删除相关的 application.properties 属性,因为它们现在是从 log4j2 属性文件中提取的。我会指导您到this 非常说明性的示例,了解如何进一步配置属性文件
    猜你喜欢
    • 1970-01-01
    • 2017-12-20
    • 2014-10-30
    • 2015-01-21
    • 2017-12-26
    • 2017-11-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    相关资源
    最近更新 更多