package com.chinasofti.cloudeasy.api.web; import org.slf4j.LoggerFactory; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.LoggerContext; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; @Api(tags = "动态修改日志级别") @RequestMapping("api/v1/monitor") @RestController @Slf4j public class LogController { /** * 修改项目日志输出级别 * * @param allLevel 全局日志级别 * @param singleLevel 某个类日志级别 * @param singlePath 需要单独设置日志输出级别的类的全限定名(例:com.chinasofti.cloudeasy.api.web.LogController) * @return */ @ApiOperation(value = "changeLogLevel") @GetMapping("changeLevel") @ApiImplicitParams({ @ApiImplicitParam(name = "rootLevel", value = "root,全局级别:ALL,TRACE,DEBUG,INFO,WARN,ERROR,OFF", required = false), @ApiImplicitParam(name = "singleLevel", value = "单独设置类日志级别:ALL,TRACE,DEBUG,INFO,WARN,ERROR,OFF", required = false), @ApiImplicitParam(name = "singlePath", value = "单独类路径:com.chinasofti.cloudeasy.api.web.LogController", required = false)}) public String changeLevel(String rootLevel, String singleLevel, String singlePath) { LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory(); log.warn("set log rootLevel:{},singleLevel:{},singlePath:{}", rootLevel, singleLevel, singlePath); if (!StringUtils.isEmpty(rootLevel)) { // 设置全局日志级别 ch.qos.logback.classic.Logger logger = loggerContext.getLogger("root"); logger.setLevel(Level.toLevel(rootLevel)); } if (!StringUtils.isEmpty(singleLevel)) { // 设置某个类日志级别-可以实现定向日志级别调整 ch.qos.logback.classic.Logger vLogger = loggerContext.getLogger(singlePath); if (vLogger != null) { vLogger.setLevel(Level.toLevel(singleLevel)); } } return "success"; } }