Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change slf4j level at runtime?

Tags:

java

slf4j

log4j

I've using SLF4j as my logging framework, backed by log4j. My problem is that I am looking for a way to change the logging level for my logger at runtime.

I understand that slf4j does not permit this directly through its own API, and hence, I have to access the logging provider directly. Personally, I find this to be a huge deficiency in slf4j. So now my question is how can I determine programatically through slf4j which provider I am using? The biggest purpose of using slf4j is that you become provider agnostic - you can easily switch between your favourite logging system without having to recode anything. But now, if I have to make direct calls to log4j, I am losing that ability.

At the very least, I would like to be able to determine if I am using log4j as the provider and if so, then allow the user to switch log levels.

If I do LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), the result is an instance of org.slf4j.impl.Log4jLoggerAdapter and not even org.apache.log4j.Logger (as I would have hoped/expected).

Is there any way to find this out?

Thanks, Eric

like image 790
Eric B. Avatar asked Jun 21 '12 19:06

Eric B.


1 Answers

SLF4J is designed as an abstraction for libraries, not applications (of course, you can and should still use SLF4J in your own app's logging calls, for consistency). In your own app, you choose the underlying logger framework, so it's fine to access the log4j API in the logging-config-specific parts.

No way should a library be mucking about with changing the logging config, IMHO, so it's not appropriate for it to be on the SLF4J API.

like image 196
artbristol Avatar answered Oct 21 '22 10:10

artbristol