日志bug,临时测试

This commit is contained in:
2026-07-20 18:02:34 +08:00
parent 55354130c6
commit ae6d5453e6

View File

@@ -38,6 +38,10 @@ public class NacosLogbackConfigLoader implements SpringApplicationRunListener, A
public void environmentPrepared(ConfigurableEnvironment environment) { public void environmentPrepared(ConfigurableEnvironment environment) {
String active = environment.getProperty("spring.profiles.active"); String active = environment.getProperty("spring.profiles.active");
if(!"jb".equalsIgnoreCase(active)){ if(!"jb".equalsIgnoreCase(active)){
String serverAddr = environment.getProperty("spring.cloud.nacos.config.server-addr"); String serverAddr = environment.getProperty("spring.cloud.nacos.config.server-addr");
String namespace = environment.getProperty("spring.cloud.nacos.config.namespace"); String namespace = environment.getProperty("spring.cloud.nacos.config.namespace");
String projectName = environment.getProperty("microservice.ename"); String projectName = environment.getProperty("microservice.ename");
@@ -51,18 +55,21 @@ public class NacosLogbackConfigLoader implements SpringApplicationRunListener, A
if (response.getStatusCode().is2xxSuccessful()) { if (response.getStatusCode().is2xxSuccessful()) {
String logbackConfigContent = response.getBody(); String logbackConfigContent = response.getBody();
//替换项目名称 //替换项目名称
logbackConfigContent = logbackConfigContent.replace("projectDefaultName",projectName); //logbackConfigContent = logbackConfigContent.replace("projectDefaultName",projectName);
logbackConfigContent = logbackConfigContent.replace("logDefaultLevel",logLevel); // logbackConfigContent = logbackConfigContent.replace("logDefaultLevel",logLevel);
// 应用获取到的Logback配置 // 应用获取到的Logback配置
applyLogbackConfig(logbackConfigContent,logLevel); applyLogbackConfig(logbackConfigContent,logLevel,projectName);
} }
} }
} }
private void applyLogbackConfig(String logbackConfigContent,String logLevel) { private void applyLogbackConfig(String logbackConfigContent,String logLevel,String projectName) {
LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
JoranConfigurator configurator = new JoranConfigurator(); JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context); configurator.setContext(context);
// 注意:变量名必须与 XML 中 ${...} 内的名称完全一致
context.putProperty("log.projectName", projectName != null ? projectName : "undefined_pqs");
context.putProperty("log.commonLevel", logLevel != null ? logLevel : "INFO");
context.reset(); context.reset();
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(logbackConfigContent.getBytes(StandardCharsets.UTF_8))) { try (ByteArrayInputStream inputStream = new ByteArrayInputStream(logbackConfigContent.getBytes(StandardCharsets.UTF_8))) {
configurator.doConfigure(inputStream); configurator.doConfigure(inputStream);