springboot多环境日志配置,启动时logback-test.xml文件冲突导致启动失败:openFile(null,true) call failed. java.io.FileNotFoundException

  如题,在项目中用到了4个环境的日志配置文件,启动时在application.properties中指定环境,让springboot自动加载logback对应的配置文件:

  每个环境的日志目录都不一样,比如sit和test环境分别是:

  如果spring.profiles.active配置的是test,那么一切正常,非test环境,则启动失败。比如我现在配置spring.profiles.active=sit来启动联调环境,报错日志如下:

2020-01-10 14:32:33.579 |-ERROR [main] org.springframework.boot.SpringApplication [822] -| Application run failed
java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[ERROR] - Failed to create parent directories for [/home/wlf/logs/prize/error-20200110.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[ERROR] - openFile(null,true) call failed. java.io.FileNotFoundException: /home/wlf/logs/prize/error-20200110.log (No such file or directory)
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CDR] - Failed to create parent directories for [/home/wlf/REPORT/send/prize20020000601809988202001101432.txt]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CDR] - openFile(null,true) call failed. java.io.FileNotFoundException: /home/wlf/REPORT/send/prize20020000601809988202001101432.txt (No such file or directory)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:167)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.reinitialize(LogbackLoggingSystem.java:220)
        at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:73)
        at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:118)
        at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:289)
        at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:264)
        at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:226)
        at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:203)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:341)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
        at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140)
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:191)
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:105)
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:71)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:341)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
        at com.wlf.order.prize.PrizeApplication.main(PrizeApplication.java:12)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Exception in thread "main" java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
        at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
        at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.lang.IllegalStateException: Logback configuration error detected:
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[ERROR] - Failed to create parent directories for [/home/wlf/logs/prize/error-20200110.log]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[ERROR] - openFile(null,true) call failed. java.io.FileNotFoundException: /home/wlf/logs/prize/error-20200110.log (No such file or directory)
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CDR] - Failed to create parent directories for [/home/wlf/REPORT/send/prize20020000601809988202001101432.txt]
ERROR in ch.qos.logback.core.rolling.RollingFileAppender[CDR] - openFile(null,true) call failed. java.io.FileNotFoundException: /home/wlf/REPORT/send/prize20020000601809988202001101432.txt (No such file or directory)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:167)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.reinitialize(LogbackLoggingSystem.java:220)
        at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:73)
        at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:60)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:118)
        at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:289)
        at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:264)
        at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:226)
        at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:203)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:341)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
        at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140)
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:191)
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:105)
        at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:71)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
        at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
        at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:76)
        at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
        at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:341)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:305)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1214)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:1203)
        at com.wlf.order.prize.PrizeApplication.main(PrizeApplication.java:12)
        ... 8 more

  我们从日志中看到,我启动的是sit,可spring boot竟然跑去读取logback-test.xml里面的信息了,我的sit环境在prize用户下,当然找不到test环境下wlf用户下的文件了。

  诡异的是为何spring boot会去读取logback-test.xml而不是logback-sit.xml呢?原因其实就在源码里。在LogbackLoggingSystem的118行initialize初始化方法中,spring boot开始加载logback:

  继续点进去,来到抽象父类的世界:

  继续在抽象父类里畅游,我们发现spring boot先去找自身特定的logback配置文件,找到了就执行reinitialize方法(见上面日志标黄处):

  既然日志打印了,说明代码已经取到了config对象。我们关心的是spring boot取到的是哪些指定的logback配置?怎么取的?其实一切尽在getSelfInitializationConfig方法中:

  上面已经回答刚才那两个问题:

  1、logback配置文件怎么取的——很明显,它拿指定的logback文件去classpath中(比如我们的src/resources)查找匹配,如果匹配上了就加载。

  2、核心问题来了:取哪些指定的logback配置文件——我们需要先结束抽象父类的游览旅程了,回到LogbackLoggingSystem去:

  一目了然,我们的4个日志配置文件里,刚好有一个logback-test.xml跟logback默认的配置重复了,而且能被匹配出来,spring boot当然优先读取默认的logback-test.xml,管你的环境变量是prod还是sit还是dev。

  既然知道了问题所在,解决方案也自然就出来了:别给测试环境的日志配置起名叫logback-test了,随便改个名吧。记得改名后重先跑maven要先clean一把,否则原来logback-test.xml还会在你的jar包里,问题依然存在。

原文地址:https://www.cnblogs.com/wuxun1997/p/12187098.html

时间: 2024-08-05 07:08:19

springboot多环境日志配置,启动时logback-test.xml文件冲突导致启动失败:openFile(null,true) call failed. java.io.FileNotFoundException的相关文章

配置ssm 时, web.xml 文件无 # 自动代码提示

环境:STS 版本:spring-tool-suite-3.8.1.RELEASE-e4.6-win32-x86_64 配置ssm 时, web.xml 文件无 如下图蓝色圈范围内的提示 问题与 链接 类似 后来在 链接 中 13楼 找到答案 解决方法如下: 在web.xml中先输入左尖括号(<),等提示框出来后,再删除这个符号,再在提示信息中翻到最下边儿就看到了. 原文地址:https://www.cnblogs.com/mumu122GIS/p/10012245.html

SpringBoot 上传文件到linux服务器 异常java.io.FileNotFoundException: /tmp/tomcat.50898……解决方案

SpringBoot 上传文件到linux服务器报错java.io.FileNotFoundException: /tmp/tomcat.50898-- 报错原因: 解决方法 java.io.IOException: java.io.FileNotFoundException: /tmp/tomcat.5089835798184465073.8081/work/Tomcat/localhost/ROOT/www/server/apache-tomcat-8.5.32/webapps/jxmsto

Spring Boot中实现logback多环境日志配置

在Spring Boot中,可以在logback.xml中的springProfile标签中定义多个环境logback.xml: <springProfile name="production"> <root level="DEBUG"> <appender-ref ref="STDOUT"/> </root> </springProfile> <springProfile nam

springboot 关于log4j日志配置

自动加载配置文件:(1)如果采用log4j输出日志,要对log4j加载配置文件的过程有所了解.log4j启动时,默认会寻找source folder下的log4j.xml配置文件,若没有,会寻找log4j.properties文件.然后加载配置.配置文件放置位置正确,不用在程序中手动加载log4j配置文件.如果将配置文件放到了config文件夹下,在build Path中设置下就好了. log4j日志配置文件的命名一般采用log4j.properties,位置放在resources/log4j.

springboot添加AOP日志配置

目录 1,在pom.xml配置文件中添加以下配置 2,自定义配置文件logback.xml,该配置文件位置在templates文件夹下第一层,和html文件夹位置并列. 3,logback.xml文件配置内容 4,写一个LogUtil类 @(springboot添加aop) 1,在pom.xml配置文件中添加以下配置 <!--spring aop 包--> <dependency> <groupId>org.springframework.boot</groupI

转:嵌入式linux启动时运行的inittab文件

嵌入式系统下的linux启动配置文件,不同与普通的PC linux启动配置,启动相关文件与文件的内容也要少得多.嵌入式系统下的linux启动过程一般是: 1 在bootloader中制定各种要求传给linux内核的参数,制作ramdisk或ramfs文件系统,并在开机后首先mount上,该文件系统主要负责包含启动运行的配置文件,嵌入式系统主要是/etc/inittab和/etc/rc文件: 2 在init进程启动后,进程首先执行/etc/inittab文件,该文件语法下面介绍,一般包括三项内容就

django启动时执行某个PY文件

Djang启动时,且在读取项目中路由之前执行某个py文件 在任意app的apps.py中的Config类中定义ready方法,并调用autodiscover_modules from django.apps import AppConfig from djngo.utils.module_loading import autodiscover_modules class StarkConfig(AppConfig) name = 'stark' def read(self) autodiscov

启动项目时,mapper.xml文件没有导入

原因分析:绑定的statement没有发现,原因是只有mapper接口的java文件,没有xml文件 解决方法:需要在pom文件中进行配置 1 <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉. --> 2 <build> 3 <resources> 4 <resource> 5 <directory>src/main/java</directory> 6 <includes> 7 <in

创建Maven web项目时 出现 web.xml is missing and &lt;failOnMissingWebXml&gt; is set to true错误 pox.xml编译错误

今天创建一个maven项目 pom.xml出现如下错误: web.xml is missing and <failOnMissingWebXml> is set to true 这是因为你因为你WEB-INF下没有web.xml导致造成的 解决方案: 右击项目——>Java EE Tools——>Generate Deployment Descriptor Stub.然后系统会在src/main/webapp/WEB_INF文件加下创建web.xml文件.错误解决! 当然这个方法是