基于JMX动态配置Log4J日志级别

时间:2021-11-14 13:20:24

先来看比较low的修改日志级别的方式,在写程序里面。

http://blog.gssxgss.me/java%E8%BF%90%E8%A1%8C%E6%97%B6%E5%8A%A8%E6%80%81%E6%94%B9%E5%8F%98%E6%97%A5%E5%BF%97%E7%BA%A7%E5%88%AB/

再来通过JMX的方法动态修改程序的日志级别。

https://blog.csdn.net/z2007130205/article/details/23910431

package com.minxinloan.common.log;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedOperationParameter;
import org.springframework.jmx.export.annotation.ManagedOperationParameters;
import org.springframework.jmx.export.annotation.ManagedResource; /**
* 基于JMX动态配置Log4J日志级别,并控制Trace开关的MBean.
*
* @author z
* @version 2013-12-10
*/
@ManagedResource(objectName = Log4jManager.MBEAN_NAME, description = "Log4j Management Bean")
public class Log4jManager { /**
* Log4jManager的Mbean的注册名称.
*/
public static final String MBEAN_NAME = "log4j:name=Log4j"; private static org.slf4j.Logger managerLogger = LoggerFactory.getLogger(Log4jManager.class); private String projectLoggerName; @ManagedAttribute(description = "Level of the root logger")
public String getRootLoggerLevel() {
Logger logger = Logger.getRootLogger();
// MBeanServer server = MBeanServerFactory.createMBeanServer();
return logger.getEffectiveLevel().toString();
} @ManagedAttribute
public void setRootLoggerLevel(String newLevel) {
Logger logger = Logger.getRootLogger();
Level level = Level.toLevel(newLevel);
logger.setLevel(level);
managerLogger.info("设置Root Logger 级别为{}", newLevel);
} /**
* 获得项目默认logger的级别.
* 项目默认logger名称通过#setProjectLoggerName(String)配置.
*/
@ManagedAttribute(description = "Level of the project default package logger")
public String getProjectLoggerLevel() {
if (projectLoggerName != null) {
return getLoggerLevel(projectLoggerName);
} return null;
} /**
* 设置项目默认logger的级别.
* 项目默认logger名称通过#setProjectLoggerName(String)配置.
*/
@ManagedAttribute
public void setProjectLoggerLevel(String newLevel) {
if (projectLoggerName != null) {
setLoggerLevel(projectLoggerName, newLevel);
}
} /**
* 获取Logger的日志级别.
*/
@ManagedOperation(description = "Get logging level of the logger")
@ManagedOperationParameters({ @ManagedOperationParameter(name = "loggerName", description = "Logger name") })
public String getLoggerLevel(String loggerName) {
Logger logger = Logger.getLogger(loggerName);
return logger.getEffectiveLevel().toString();
} /**
* 设置Logger的日志级别.
* 如果日志级别名称错误, 设为DEBUG.
*/
@ManagedOperation(description = "Set new logging level to the logger")
@ManagedOperationParameters({ @ManagedOperationParameter(name = "loggerName", description = "Logger name"),
@ManagedOperationParameter(name = "newlevel", description = "New level") })
public void setLoggerLevel(String loggerName, String newLevel) {
Logger logger = Logger.getLogger(loggerName);
Level level = Level.toLevel(newLevel);
logger.setLevel(level);
managerLogger.info("设置{}级别为{}", loggerName, newLevel);
} /**
* 根据log4j.properties中的定义, 设置项目默认的logger名称
*/
public void setProjectLoggerName(String projectLoggerName) {
this.projectLoggerName = projectLoggerName;
} }

  

import static org.junit.Assert.assertEquals;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Test; import com.minxinloan.common.log.Log4jManager; public class Log4jManagerTest { @Test
public void testLoggerLevel() {
String loggerName = "org.springside.modules";
Log4jManager mbean = new Log4jManager();
String orgLevel = mbean.getLoggerLevel(loggerName); Logger.getLogger(loggerName).setLevel(Level.FATAL);
assertEquals("FATAL", mbean.getLoggerLevel(loggerName)); mbean.setLoggerLevel(loggerName, "TRACE");
assertEquals("TRACE", mbean.getLoggerLevel(loggerName)); mbean.setLoggerLevel(loggerName, "WRONG_LEVEL_NAME");
assertEquals("DEBUG", mbean.getLoggerLevel(loggerName)); mbean.setLoggerLevel(loggerName, orgLevel);
}
}

  

package com.minxinloan.common.log; import org.apache.log4j.Level;import org.apache.log4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jmx.export.annotation.ManagedAttribute;import org.springframework.jmx.export.annotation.ManagedOperation;import org.springframework.jmx.export.annotation.ManagedOperationParameter;import org.springframework.jmx.export.annotation.ManagedOperationParameters;import org.springframework.jmx.export.annotation.ManagedResource; /** * 基于JMX动态配置Log4J日志级别,并控制Trace开关的MBean. *  * @author z * @version 2013-12-10 */@ManagedResource(objectName = Log4jManager.MBEAN_NAME, description = "Log4j Management Bean")public class Log4jManager { /** * Log4jManager的Mbean的注册名称. */public static final String MBEAN_NAME = "log4j:name=Log4j"; private static org.slf4j.Logger managerLogger = LoggerFactory.getLogger(Log4jManager.class); private String projectLoggerName; @ManagedAttribute(description = "Level of the root logger")public String getRootLoggerLevel() {Logger logger = Logger.getRootLogger();//MBeanServer server = MBeanServerFactory.createMBeanServer();return logger.getEffectiveLevel().toString();} @ManagedAttributepublic void setRootLoggerLevel(String newLevel) {Logger logger = Logger.getRootLogger();Level level = Level.toLevel(newLevel);logger.setLevel(level);managerLogger.info("设置Root Logger 级别为{}", newLevel);} /** * 获得项目默认logger的级别. * 项目默认logger名称通过#setProjectLoggerName(String)配置. */@ManagedAttribute(description = "Level of the project default package logger")public String getProjectLoggerLevel() {if (projectLoggerName != null) {return getLoggerLevel(projectLoggerName);} return null;} /** * 设置项目默认logger的级别. * 项目默认logger名称通过#setProjectLoggerName(String)配置. */@ManagedAttributepublic void setProjectLoggerLevel(String newLevel) {if (projectLoggerName != null) {setLoggerLevel(projectLoggerName, newLevel);}} /** * 获取Logger的日志级别. */@ManagedOperation(description = "Get logging level of the logger")@ManagedOperationParameters({ @ManagedOperationParameter(name = "loggerName", description = "Logger name") })public String getLoggerLevel(String loggerName) {Logger logger = Logger.getLogger(loggerName);return logger.getEffectiveLevel().toString();} /** * 设置Logger的日志级别. * 如果日志级别名称错误, 设为DEBUG. */@ManagedOperation(description = "Set new logging level to the logger")@ManagedOperationParameters({ @ManagedOperationParameter(name = "loggerName", description = "Logger name"),@ManagedOperationParameter(name = "newlevel", description = "New level") })public void setLoggerLevel(String loggerName, String newLevel) {Logger logger = Logger.getLogger(loggerName);Level level = Level.toLevel(newLevel);logger.setLevel(level);managerLogger.info("设置{}级别为{}", loggerName, newLevel);} /** * 根据log4j.properties中的定义, 设置项目默认的logger名称 */public void setProjectLoggerName(String projectLoggerName) {this.projectLoggerName = projectLoggerName;} }