• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Get Rid of Java Static Loggers

 
Yegor Bugayenko
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Here is how you can get rid of that annoying static loggers: http://www.yegor256.com/2014/05/23/avoid-java-static-logger.html

Instead of



You simply write



Let me know what do you think...
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks like what Dotcms uses. I'm not sure about the performance though. How do you resolve the logger for the passed instance? Look it up each and every time? If you do a lot of logging, that's going to have a (small) performance hit. I'd rather have one single static variable (which I personally have a template for to create quickly) than suffer lookups per call.
 
Yegor Bugayenko
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You pass "this" or any other object to Logger.info() call
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got that. But then, how does it work internally? LoggerFactory.getLogger(object.getClass()) for each time you need a backing SLF4J logger?
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, how do you handle errors in combination with formatted messages? For example, how to do it with SLF4J:
The exception is then logged after the message, which includes the file. How does your code handle situations like this?
 
Yegor Bugayenko
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, we're calling LoggerFactory.getLogger(object.getClass()) every time we need a logger.

We don't support exception logging the way it works with slf4j. However, we support "decors", which help you to render exception's stacktrace (your example):



We're using the same formatting as in String.format(), which is much more powerful than markers in slf4j.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yegor Bugayenko wrote:You pass "this" or any other object to Logger.info() call

I find that annoying. The static loggers, no so much.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic