logback 动态设置日志级别

12 篇文章 0 订阅
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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值