How to dynamically change log level in SLF4j OR Log4J

前端 未结 5 1198
春和景丽
春和景丽 2020-12-10 12:19

Recently I encountered a situtation where Application Loglevel changes dynamically. Application Admin can set it to INFO/DEBUG/ WARN from front end. Based on the log level c

5条回答
  •  我在风中等你
    2020-12-10 12:52

    I had to do this once with log4j. The only way I could figure out how to do it was to call getAllAppenders on the Logger object. Then, loop through the appenders. If they extend the AppenderSkeleton class (they should), they will have the setThreshold method. Call this method with your new Level as the parameter. Subsequent calls to the logger should use the new level. This will set the level in memory, but not in your log4j configuration file. You may want to do this, too, unless it gets changed automatically when the admin changes the level via the front end. If it's an option, you may want to consider following Evgeniy Dorofeev's advice and use logback. It sounds like it would be easier.

提交回复
热议问题