0

I created a library using Java & Maven that contains some common Spring functionality for reuse in various Spring-Based projects.

The library will obviously need to log error/information messages. Spring allows the developer to use whichever logging library they prefer, how do I make my library do the same?

Samantha Catania
  • 5,116
  • 5
  • 39
  • 69

1 Answers1

0

Spring uses commons-logging which is a logging facade that allows you to write logging code without knowing what is the actual logging framework.

Depending on what you have configured, the commons-logging will then channel the logging messages to the actual implementation. This allows you to write your code using commons-logging, and the users to use any logging framework supported by commons-logging.

Personally I would go with slf4j (Simple Logging Facade 4 Java) which is similar to commons-logging, but newer and functions in pretty much the same way.

It's also possible to bridge different libraries or facades, so that even if libraries use different logging frameworks they will always end up in the logger of your choice. Looking to the monitor to my right there are bridge libraries as follows: jcl-over-slf4j (commons-logging to slf4j), log4j-over-slf4j and jul-to-slf4j (java.util.logging to slf4j).

Note: See the link in comments for more information about how slf4j = good, JCL = bad ;)

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • SLF4J and commons-logging works in a very different way afaik, although their intention is similar. Anyway, just a side note, by using SLF4j with appropriate bridge library, logs from commons-logging (e.g. from Spring) can be redirected to SLF4J, hence allow you to centralize and make use of SLF4J backends that is not supported by commons-logging. (This shouldn't be something controlled by the library written by OP anyway) – Adrian Shum Nov 05 '15 at 08:55
  • I don't have much clue about the internal workings of `commons-logging`, but I wouldn't be surprised if they differ. I'll add some info about the bridge libraries. – Kayaman Nov 05 '15 at 08:57
  • 1
    in case you are interested :) http://stackoverflow.com/questions/3222895/what-is-the-issue-with-the-runtime-discovery-algorithm-of-apache-commons-logging – Adrian Shum Nov 05 '15 at 09:00
  • Ah thanks, it's always good to have a valid reason to want to avoid a particular framework :) – Kayaman Nov 05 '15 at 09:04