package com.hanshow.wise.base.privileges.controller;
import com.hanshow.wise.base.privileges.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.logback.LogbackLoggingSystem;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("logback")
public class TestController {
private static Logger logger = LoggerFactory.getLogger(TestController.class);
@Autowired(required = false)
private LogbackLoggingSystem logbackLoggingSystem;
@RequestMapping(value = "/setlevel", method = RequestMethod.POST)
@ResponseBody
public Object setlevel(HttpServletRequest request, String level, String loggerNames) {
LogLevel logLevel;
if (StringUtil.isEmpty(level)) {
logLevel = LogLevel.INFO;
} else {
switch (level) {
//TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
case "TRACE":
case "trace":
logLevel = LogLevel.TRACE;
break;
case "DEBUG":
case "debug":
logLevel = LogLevel.DEBUG;
break;
case "INFO":
case "info":
logLevel = LogLevel.INFO;
break;
case "WARN":
case "warn":
logLevel = LogLevel.WARN;
break;
case "ERROR":
case "error":
logLevel = LogLevel.ERROR;
break;
case "FATAL":
case "fatal":
logLevel = LogLevel.FATAL;
break;
case "OFF":
case "off":
logLevel = LogLevel.OFF;
break;
default:
logLevel = LogLevel.INFO;
}
}
if (StringUtil.isEmpty(loggerNames)) {
logbackLoggingSystem.setLogLevel("ROOT", logLevel);
//logbackLoggingSystem.setLogLevel("org.mybatis", logLevel);
logbackLoggingSystem.setLogLevel("com.hanshow.wise.base.privileges.mapper", logLevel);
} else {
String[] split = loggerNames.split(",");
for (int i = 0; i < split.length; i++) {
logbackLoggingSystem.setLogLevel(split[i].trim(), logLevel);
}
}
return logbackLoggingSystem.getLoggerConfigurations();
}
}
logback.xml配置文件
<logger name="org.mybatis" level="INFO"></logger>
<logger name="com.hanshow.wise.base.privileges.mapper" level="INFO"></logger>
<logger name="com.hanshow.wise" additivity="false">
<level value="INFO" />
<appender-ref ref="StdOut"/>
<appender-ref ref="ALL"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="INFO" />
<appender-ref ref="WARN" />
</logger>
<root level="INFO">
<appender-ref ref="StdOut" />
<appender-ref ref="ALL"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="INFO" />
<appender-ref ref="WARN" />
</root>