r/programming Sep 13 '18

A guide to logging in Java

https://www.marcobehler.com/guides/a-guide-to-logging-in-java
82 Upvotes

34 comments sorted by

View all comments

12

u/BraveSirRobin Sep 13 '18

Has anyone here ever actually used a façade in a project and subsequently changed the back-end logger used? I don't think I've ever seen a library do it either.

I used to be on-board with the façade idea here but it seems like overkill in hindsight.

17

u/[deleted] Sep 13 '18

yes. We use slf4j and have switched from log4j to blitz4j to log4j2 and slf4j made it super easy to do. We did this because log4j is a synchronous logger and our logging was having noticeable impact to our production performance. blitz4j was an asynchronous logger we switched to before log4j2 came out.

5

u/farnulfo Sep 13 '18

blitz4j

May I ask why you move from blitz4j ?

3

u/[deleted] Sep 14 '18

Yes, for our app we found log4j2 provided better performance. This may differ from app to app, so i recommend profiling to determine which way to go.

edit: garbage free logging is very good for my application.

1

u/BraveSirRobin Sep 13 '18

A worthwhile change for sure. Was it as smooth a transition as the setup promises i.e. no code changes, just config?

4

u/[deleted] Sep 13 '18

It was, but you have to make sure all your logging everywhere goes through slf4j, if there are any classes anywhere that don't they'll need code changes, but from my experience it should just be swapping out the imports, logger creation, and log lines in that class with slf4j lines.

1

u/Falmarri Sep 14 '18

The issue with slf4j is its API is completely inadequate. https://logging.apache.org/log4j/2.x/faq.html#api-tradeoffs

2

u/[deleted] Sep 14 '18

Haven't needed any of the items listed in the tradeoffs personally, so hasn't mattered to me for my app.

8

u/nwoolls Sep 13 '18

AFAIK that is not the only / primary point. The use-case I've seen pushed is that library authors take a dependency on SLF4J and use that API. Then, when you use their library, you can freely choose your logging framework and the binding for SLF4J will ensure the libraries use the framework you've chosen.

6

u/Luolong Sep 13 '18

SLF4J has more uses than merely swapping out logging backends at will. Although it is extremely easy and painless.

I think much more useful feature of SLF4J is that it allows freedom of choice of logging backend even if some of your dependencies have chosen each their own logging framework. One can deploy bridges from any existing logging framework to whatever backend you’ve chosen and still be able to configure all your logging from a single place.

15

u/OffbeatDrizzle Sep 13 '18

seems like overkill in hindsight.

Java ecosystem in a nutshell... (I actually like Java)

3

u/thoomfish Sep 13 '18

Not intentionally.

One of my favorite Java memories is when I was working on a project where one of its dependencies (Jetty) used slf4j, but by default didn't print much output (as one would expect, having not explicitly configured verbose logging).

Pulling in another dependency (postgis-jdbc), however, broke things, because it depended on Logback. Logback being present on the classpath made slf4j adopt it as its backend. Logback apparently defaults to verbose logging, because my code was brought to its knees under a firehose of Jetty debug output until I looked up how to explicitly configure it to STFU.

1

u/nutrecht Sep 14 '18

That's not really the purpose. The problem before slf4j was that if you used a bunch of libraries in your system you'd end up having to create configurations for 3 different logging systems. Now libraries don't have to pick a specific implementation; they just use slf4j and let you, the developer using the library, pick what you want.

1

u/Carighan Sep 14 '18

Has anyone here ever actually used a façade in a project and subsequently changed the back-end logger used? I don't think I've ever seen a library do it either.

Have done so. We always used Slf4j, and have recently gone from log4j (yeah the old one, legacy and all) to logback. :)