想要实现日志级别动态切换有两个要点:
1.监听Nacos配置文件修改
2.根据配置动态设置当前日志级别
监听Nacos
监听类代码:
package com.yibing.nacosprovider.listener;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.listener.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import com.yibing.nacosprovider.service.LogLevelChangeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.concurrent.Executor;
/**
* Date:2022/12/2
* Author: yibing
*/
@Slf4j
@Configuration
public class LoggerDynamicsConfigListener {
/**
* 配置中心地址
*/
@Value("${spring.cloud.nacos.discovery.server-addr}")
private String serverAddr;
/**
* 配置文件
*/
@Value("${nacos.config.filename}")
private String dataId;
/**
* 配置GROUP
*/
@Value("${nacos.config.group}")
private String group;
/**
* 配置项key
*/
@Value("${nacos.config.log.level}")
private String logLevelName;
/**
* 动态修改日志级别service
*/
@Autowired
private LogLevelChangeService logLevelChangeService;
@PostConstruct
public void init() {
try {
log.info("init NacosConfigListener start...");
NacosFactory.createConfigService(serverAddr).addListener(dataId, group, new Listener() {
@Override
public void receiveConfigInfo(String configInfo) {
try {
JSONObject jsonObject = JSONObject.parseObject(configInfo);
Object levelObj = jsonObject.get(logLevelName);
if (levelObj != null) {
logLevelChangeService.changeLogLevel(levelObj.toString());
}
} catch (Exception e) {
log.error("has exception:", e);
}
}
@Override
public Executor getExecutor() {
return null;
}
});
log.info("init NacosConfigListener end...");
} catch (NacosException e) {
log.error("has exception", e);
}
}
}
配置项
spring.cloud.nacos.config.server-addr=http://192.168.1.100:81
nacos.config.filename=nacos-provider-log-level.json
nacos.config.group=DEFAULT_GROUP
nacos.config.log.level=log.level
动态切换日志级别
package com.yibing.nacosprovider.service.impl;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import com.yibing.nacosprovider.service.LogLevelChangeService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
/**
* 动态调整日志级别
*/
@Slf4j
@Service
public class LogLevelChangeServiceImpl implements LogLevelChangeService {
@Override
public boolean changeLogLevel(String level) {
try {
log.info("level:{}",level);
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
Logger logger = loggerContext.getLogger("ROOT");
switch (level) {
case "trace":
logger.setLevel(Level.TRACE);
break;
case "debug":
logger.setLevel(Level.DEBUG);
break;
case "info":
logger.setLevel(Level.INFO);
break;
case "warn":
logger.setLevel(Level.WARN);
break;
case "error":
logger.setLevel(Level.ERROR);
break;
default:
break;
}
return true;
} catch (Exception e) {
log.error("exception:", e);
return false;
}
}
}
标签:log,level,config,nacos,Nacos,切换,import,日志,com
From: https://www.cnblogs.com/zhangyibing/p/16946321.html