通过URL方式动态修改logback level级别
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import ch.qos.logback.classic.Level; import ch.qos.logback.classic.LoggerContext; public class LogLeveLController{ private static final Logger log = LoggerFactory.getLogger(LogLeveLController.class); public void control(HttpServletRequest req) { String logLevel = req.getParameter("logLevel"); try { LoggerContext logcontext = (LoggerContext) LoggerFactory.getILoggerFactory(); logcontext.getLogger("org.mybatis").setLevel(Level.valueOf(logLevel)); logcontext.getLogger("org.springframework").setLevel(Level.valueOf(logLevel)); logcontext.getLogger("org.quartz").setLevel(Level.valueOf(logLevel)); } catch (Exception e) { log.error("动态修改日志级别出错", e); } } }
该种实现方式主要是为了应对在生产环境动态查看logback日志级别,一般生产环境中的level为info级别,可能查看问题时可修改为debug级别,当然也可使用界面设置切换级别的模式