Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java logging interface

Tags:

java

logging

If I've to distribute a certain framework like a customized validation framework which can be used in many projects, how do I handle logging within this framework so that it uses the same logging method as the project in which it is being used? i.e I've written a logger interface with the usual debug,warn,info... methods and the implementation of this interface can implement those methods using log4j or any other logging methods. If I distribute this as a jar, how can different projects use this jar with their logging framework?

like image 266
Rnet Avatar asked Mar 05 '26 17:03

Rnet


2 Answers

I would strongly suggest that instead of writing yet another logging framework you recode your application to use the SLF4J logging API instead. All the work of interfacing to other logging implementations have been done, and is well known in the industry.

An added benefit is that you get {}-placeholders, which allow you to just do

 log.debug("a={}, b={}", a, b);

and the a.toString() and b.toString() are only called when the string will actually be logged. This allows for a lot of log statements which will not actually be executed unless you need them to run in a debug setting.

like image 94
Thorbjørn Ravn Andersen Avatar answered Mar 07 '26 07:03

Thorbjørn Ravn Andersen


There are some libraries that provide an abstraction layer over log4j, java.util.logging etc. They act as the 'interface' and then the person using your project can use the implementation of their choosing. Have a look at Apache Commons Logging and SLF4J.

like image 25
Gyan aka Gary Buyn Avatar answered Mar 07 '26 06:03

Gyan aka Gary Buyn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!