Java Dependencies – Choosing a Logging Dependency for Low-Level Libraries

dependenciesjava

I inherited a lower-level open source Java network library. The library is intended to be used by higher-level application protocol libraries which are in turn used by application code. My library, as left by the previous maintainer, has no dependencies and is built via Ant. The build artifact is a simple jar that others can drop into their projects.

I am in the process of converting the library to be built with Maven. As I'm doing this, I'm considering possibly adding slf4j as a dependency. Currently, when something goes wrong, stack traces or other information is spit out to System.out or System.err. I know that that's not always very helpful, but these outputs only happen at 10 to 15 places in the code and are rarely executed.

So what do you think? Keep the library simple with zero dependencies or introduce slf4j?

Best Answer

If you use the Maven Assembly Plugin, you can include the slf4j jar (along with whichever implementation you prefer) inside your deployed jar. There are more complicated options if you want to allow users to choose the logging library, but it will run perfectly well if you make a fat jar with or without the slf4j implementation.

Users are much more likely to be annoyed - unless hard drive space is really (and I mean really) at a premium - if they have to deal with System.out and System.err output than if there's a provided logging framework, even if it's just the default no-op implementation and the have to add log4j or logback if they want to see the logs.