Search code examples
javalogginglog4jclient-library

How to control logging level in client library?


We'd like clients to be able to control logging levels in our client JAR. What is the best way to do this?

Currently we just have a handful of log statements that write to System.out. I realize that using Log4J would solve this problem although one of our biggest clients doesn't use Log4J and uses their own custom logging implementation. Is there a clean approach to let them control logging in our client jar?

Options we've thought of: clients could explicitly set properties on client jar classes to set logging level (don't like this), our client jar could read an optional .properties file that clients can put on their classpath (better but still a bit of a pain).


Solution

  • Don't use a concrete logging framework, use SLF4J, so you can exchange logging if you need to. I'd start at first converting your own System.out's to the included java.util.logging. It's pretty straight forward and convenient for most needs.

    If your client uses another logging framework, there either exists a bridge to slf4j or you can write your own.

    EDIT: We've used it to streamline the logging of external libraries which used LOG4J into java.util.logging which we use.