有人可以帮忙使用Jconsole配置SpringBoot应用程序以进行动态记录器配置。
下面是代码设置: 谢谢
答案 0 :(得分:0)
我能够配置Jconsole,能够通过此Jconsole设置动态日志记录
步骤1->在application.property文件中添加属性 spring.jmx.enabled = true
第2步->创建LogConfig类以将日志级别设置为LoggerContext。下面是代码
@Component @ManagedResource(objectName =“ OfferingService:name = MyCustomLogging”,描述=“ Logging”,日志= true,logFile =“ jmx.log”,currencyTimeLimit = 15,persistPolicy =“ OnUpdate”,persistPeriod = 200,persistLocation =“ logging” ,persistName =“ logging”)
公共类LoggerConfig {
private static final Logger logger = LoggerFactory.getLogger(LoggerConfig.class);
@ManagedOperation(description = "Set the logging level for a category")
@ManagedOperationParameters( { @ManagedOperationParameter(name = "category", description = "Logger category"),
@ManagedOperationParameter(name = "level", description = "Logging level") })
public void updatLoggerLevel(String category, String logLevel) {
logger.info("updatLoggerLevel Method ================ >>>>: " + category);
logger.info("LogLevel ================ >>>>: " + logLevel);
LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
if (logLevel.equalsIgnoreCase("DEBUG")) {
loggerContext.getLogger(category).setLevel(Level.DEBUG);
} else if (logLevel.equalsIgnoreCase("INFO")) {
loggerContext.getLogger(category).setLevel(Level.INFO);
} else if (logLevel.equalsIgnoreCase("TRACE")) {
loggerContext.getLogger(category).setLevel(Level.TRACE);
} else {
logger.error(" Unknown loglevel: " + logLevel);
}
}
@ManagedOperation(description = "Get the logging level for a category")
@ManagedOperationParameters( { @ManagedOperationParameter(name = "category", description = "Logger category") })
public String getLoggerLevel(String category) {
logger.info("getLoggerLevel Method ================ >>>>: " + category);
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
return loggerContext.getLogger(category).getLevel().toString();
} }
步骤3->运行springBoot应用程序
步骤4->转到C:\ Program Files \ Java \ jdk1.8.0_73 \ bin
第5步>打开JConsole,您可以在Jconsole中看到objectName =“ MyApp”。
步骤6->转到操作,在那里您可以看到操作名称–>
updatLoggerLevel(这是方法名称)
通过提供参数enter code here
之类的方法(例如一个是packaageName,另一个是loglevel),可以从Jconsole触发该方法。这将覆盖现有的日志配置。