V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
luxinfl
V2EX  ›  程序员

日志组件可以在 springboot 加载配置文件之前启动吗?

  •  
  •   luxinfl · 2021-06-10 10:25:12 +08:00 · 1600 次点击
    这是一个创建于 1022 天前的主题,其中的信息可能已经有所发展或是发生改变。
    有个现象,当 application.yml 格式错误的时候,启动程序,控制台打印的日志格式并没有按照日志配置文件的来,甚至此时应该没有到日志组件加载的那一步。所以有没有什么方法可以让日志组件提前启动?

    问这个问题的原因是,项目在 linux 用 java -jar xxz.jar >/dev/null 2>&1 &启动时,脚本把打印信息吞了,此时系统配置文件格式错误的日志也不会打印了。所以在不改动脚本的情况下可以吗????
    13 条回复    2021-06-10 18:11:00 +08:00
    suenlai
        1
    suenlai  
       2021-06-10 10:43:58 +08:00
    这个时候日志相关的 bean 还没有加载进来,springboot 好像有种机制是先把要打印的放在缓冲区, 等加载完后再打印。
    luxinfl
        2
    luxinfl  
    OP
       2021-06-10 10:45:55 +08:00
    @suenlai 那就是没法搞到指定文件?只能靠修改 shell 脚本来打印了么。有没有可能在监听器里面实现
    suenlai
        3
    suenlai  
       2021-06-10 10:57:28 +08:00
    Deferred Log that can be used to store messages that shouldn't be written until the logging system is fully initialized.
    你参考下这个 API:Deferred Log
    suenlai
        4
    suenlai  
       2021-06-10 11:12:28 +08:00
    deferred log 在 logback 初始化前用的是 commons-logging
    "
    The name (<code>commons-logging.properties</code>) of the properties file to search for.
    "
    Aruforce
        5
    Aruforce  
       2021-06-10 11:17:35 +08:00
    在 main 方法执行 application run 之前就使用 logger 。。。这样 logger 就在 application 启动之前启动了
    Aruforce
        6
    Aruforce  
       2021-06-10 11:21:07 +08:00
    @Aruforce 比如 logback 这种...但是 spring-logback.xml 和 logback.xml 不知道能不能共存..有个不确定的问题是 spring logger starter 会不会对 logback reconfigure... 如果不能的话。。spring-logback.xml 就没啥用了
    wz497345846
        7
    wz497345846  
       2021-06-10 11:24:02 +08:00
    写在 bootstrap.yml 里面
    luxinfl
        8
    luxinfl  
    OP
       2021-06-10 14:21:11 +08:00
    @suenlai 我去查一下,我今天才刚知道有这么个 log
    luxinfl
        9
    luxinfl  
    OP
       2021-06-10 14:21:41 +08:00
    @wz497345846 这个只是配置文件会优先读到吧
    luxinfl
        10
    luxinfl  
    OP
       2021-06-10 14:22:36 +08:00
    @Aruforce 我尝试过,但打印的格式不是自定义的,说明日志组件还是没有初始化完成吧
    Aruforce
        11
    Aruforce  
       2021-06-10 17:06:55 +08:00
    @luxinfl 因为你在使用 SpringBoot 我假定你在使用 spring-logback.xml 做日志组件的配置。。但是 logback 本身是默认读取 logback.xml 的。。。所以在 Springboot 对 logback 配置完成之前 logback 是在使用自己的默认配置...

    你可以增加一个 logback.xml 并且里面有你自己的格式配置 这个可能要求配置参数不能依赖 springprofile...来验证
    Aruforce
        12
    Aruforce  
       2021-06-10 17:10:20 +08:00
    @Aruforce 或者你可以看看 LoggingApplicationListener SpringBootJoranConfigurator LoggingSystem LogbackLoggingSystem 这几个类。。。我是以前大略看过..不过大体启动逻辑和一些默认配置 就在这些类里面
    Amit
        13
    Amit  
       2021-06-10 18:11:00 +08:00
    log 组件是不依赖 spring 容器的,spring 启动后会通过 log 组件的 api 更新日志级别和格式,所以我猜启动的时候应该是用的默认格式
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1146 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 22:59 · PVG 06:59 · LAX 15:59 · JFK 18:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.