Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
445 views
in Technique[技术] by (71.8m points)

java - How to dynamically change log level in SLF4j OR Log4J

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 choosen be him application logging must be changed.

I am sure loggers support this scenario, but not sure how can I achive this. If any of you have idea/thoughts on this please let me know.

Thanks in advance for your help.

-Narendra

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is not possible to change the log level dynamically in slf4j, but some backends for slf4j support it, including log4j.

This solution worked for me:

org.apache.log4j.Logger logger4j = org.apache.log4j.Logger.getRootLogger();
logger4j.setLevel(org.apache.log4j.Level.toLevel("ERROR"));

(Source: http://prateep.info/2015/12/12/Dynamically-change-log-level-in-SLF4j-Log4J-with-Standalone-Java-Class/)

The disadvantage of this solution is that it uses the backend directly, which you're not supposed to do when using slf4j because the point of slf4j is to provide an abstraction away from the specific backend you're using.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...