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"; } }