ERROR in ch.qos.logback.classic.joran.action.ContextNameAction - Failed to rename context [nacos] as


Caused by: java.lang.IllegalStateException: java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.classic.joran.action.ContextNameAction - Failed to rename context [nacos] as [logback] java.lang.IllegalStateException: Context has been already given a name
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:321)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:281)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:216)
    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:80)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:345)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140)
    at org.springframework.cloud.stream.binder.DefaultBinderFactory.getBinderInstance(DefaultBinderFactory.java:320)
    at org.springframework.cloud.stream.binder.DefaultBinderFactory.doGetBinder(DefaultBinderFactory.java:209)
    at org.springframework.cloud.stream.binder.DefaultBinderFactory.getBinder(DefaultBinderFactory.java:140)
    at org.springframework.cloud.stream.binding.BindingService.getBinder(BindingService.java:379)
    at org.springframework.cloud.stream.binding.BindingService.bindProducer(BindingService.java:268)
    at org.springframework.cloud.stream.binding.BindingService.bindProducer(BindingService.java:291)
    at org.springframework.cloud.stream.binding.AbstractBindableProxyFactory.createAndBindOutputs(AbstractBindableProxyFactory.java:136)
    at org.springframework.cloud.stream.binding.OutputBindingLifecycle.doStartWithBindable(OutputBindingLifecycle.java:58)
    at java.util.LinkedHashMap$LinkedValues.forEach(LinkedHashMap.java:608)
    at org.springframework.cloud.stream.binding.AbstractBindingLifecycle.start(AbstractBindingLifecycle.java:57)
    at org.springframework.cloud.stream.binding.OutputBindingLifecycle.start(OutputBindingLifecycle.java:34)
    at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:182)
-Dnacos.logging.default.config.enabled=false

错误原因
错误原因在于nacos引入的nacsos-client.jar内含有默认的nacos-logback.xml/nacos-log4j2.xml,其中nacos-logback.xml中contextName属性为nacos
该属性与自定义的logback.xml不一致导致冲突
https://github.com/alibaba/nacos/issues/1959


因为Spring Boot本身已经集成了日志框架Logback,而在Alibaba Nacos中又集成了一套Logback框架
而Alibaba Nacos中的Logback加载要优先于项目自身的Logback框架,在一个项目中其context_name只能定义一次,
所以,在项目启动时,Alibaba Nacos的Logback先加载完成后,再加载项目本身的Logback时就出现了冲突,报错误日志 “ Failed to rename context [logback] as [nacos] java.lang.IllegalStateException: Context has been already given a name ” ,但是不影响使用,
解决这个,只要禁用掉其中一个Logback就可以了,一般禁用外部框架夹带的Logback即可



处理方法分别如下:
1、修改自定义的logback.xml的contextName属性与nacsos-client.jar的配置保持一致
2、启动命令添加配置 -Dnacos.logging.default.config.enabled=false,该配置在nacos加载时,读取系统配置


3、在启动类的main方法中设置系统参数

https://blog.csdn.net/shijinting/article/details/113858339?spm=1001.2101.3001.6661.1&utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1.pc_relevant_default&utm_relevant_index=1

解决项目启动 logback 配置加载失败 问题定位/处理
问题1 :Failed to rename context [springAppName_IS_UNDEFINED]
原因:logback配置 动态获取当前项目 应用名 获取失败
解决方案:将 获取方式 由
改为

参考文档 https://blog.csdn.net/qq_42533633/article/details/106267861


问题2 :Context has been already given a name nacos
原因:项目框架 集成logback 与nacos 自带的logback 冲突, nacos的logback 已经先于 项目启动,logback 本身 在命名之后不允许二次修改contextName的名字,问题在阿里nacos 官方git上已有人提出,
https://github.com/alibaba/nacos/issues/1959
目前的解决方案是 在启动类 加入 System.setProperty("nacos.logging.default.config.enabled", "false");
含义:将该项目对应的nacos日志服务 禁用

https://www.jianshu.com/p/6b7cbb51e98f