Search code examples
javajar

What's the best approach in jar/library creation?


I need to create jar/library that does some stuff and writes some logs. For logs I was thinking about java.util.logging.Logger vs Logback. So the question is: Should I prefer using java out-of-the-box logging or it's totally fine to include some third-party tools?

Or it should be purely the matter of my needs and which one to use doesn't really matter?


Solution

  • Use a facade, like slf4j. Think of it from the point of view of the person using your jar in their application. They will want all the logging going to one place, whether it is logging for your jar or for the rest of the application, they will not want to configure your library's logging separately.

    Using a logging facade means your library writes to whatever log the rest of the application uses. The application should be in charge of logging, not your jar.

    For which facade to use, slf4j seems like a better choice, commons logging can get in trouble where multiple classloaders are involved. See Difference between Simple Logging Facade for Java and Apache Commons Logging