Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Get Rid of Java Static Loggers

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Sheriff
Posts: 22057
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You pass "this" or any other object to Logger.info() call
 
Rob Spoor
Sheriff
Posts: 22057
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22057
113
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 67497
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic