Log4j dynamically adjusts log levels

Requirements: the online log level is high, and the location problem requires low-level logs to facilitate analysis of the problem
function: do not restart the server, provide the settings page, manually trigger changes in the log4j project log level


Demo

Import java.io.Writer; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Level; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; / * * * * @Controller log4j to dynamically modify the log level of @RequestMapping (value = "/logger") public class LoggerController {/ * * * * @param P need to modify the package name @param * L * @return * @ResponseBody @RequestMapp log level Ing (value = "/change", method = RequestMethod.GET, produces = "application/json") public String change (String P, String L) {try {Level level = Level.toLevel (L); Logger logger = LogManager.getLogger (P); logger.setLevel (level);} catch (Exception E) {return "failed"; return "success";}} / * * * modify the global log level, but when I tested only modify the third jar log level, my project package log level does not modify the success you can get to the Logger * * @param L after looping through the set @ResponseBody (value * @return * / @RequestMapping = "/changeRoot". Method = RequestMethod.GET, produces = Application /json public String (String) change L) {try {Level level = Level.toLevel (L); LogManager.getRootLogger (.SetLevel) (level);} catch (Exception E) {return failed}; return success;} / * * * view now packet log level (value = @RequestMapping * @return * "" /loggers, method = RequestMethod.GET) public void index (HttpServletRequest request, HttpServletResponse response) {StringBuilder sb = new (StringBuilder); try {sb.append ("< html&"; gt; Writer; writer) = (response.getWriter); response.setCharacterEncoding ("UTF-8"); response.setContentType ("text/htm L; charset=utf-8; Enumeration) = LogManager.getCurrentLoggers (logs); while (logs.hasMoreElements (Logger)) {logger = (Logger) (logs.nextElement); sb.append (< span style='display:block;'> "); sb.append (logger.getName) (.Append) (".Append ") ((logger.getEffectiveLevel)); / / can according to the specified name display package name sb.append (< /span>");} sb.append (< /html> "); writer.write (sb.toString ()); (writer.flush); if (writer! = null) {(writer.close)}; System.out.println (sb.toString) (catch (Exception E);}) {}}}
Log4j dynamically adjusts log levels
LoggerController
Log4j dynamically adjusts log levels
modifies the specified class log level
Log4j dynamically adjusts log levels
modify the log level under project cn.nubia
Log4j dynamically adjusts log levels
looks at the current package log level
Log4j dynamically adjusts log levels
looks at the package log level 2 now
Log4j dynamically adjusts log levels
log adjustment unified access interface

Reference

Good simple and practical dynamic modification log4j log level, without restarting the server
U.S. demand level well: the log level dynamic adjustment gadget to solve big problems
log4j dynamic log level adjustment