aspose file tools*
The moose likes Blog around the Campfire and the fly likes Get Rid of Java Static Loggers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Blog around the Campfire
Bookmark "Get Rid of Java Static Loggers" Watch "Get Rid of Java Static Loggers" New topic
Author

Get Rid of Java Static Loggers

Yegor Bugayenko
Ranch Hand

Joined: Feb 11, 2011
Posts: 77
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...


SCEA, PMP, read my blog: www.yegor256.com
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Yegor Bugayenko
Ranch Hand

Joined: Feb 11, 2011
Posts: 77
You pass "this" or any other object to Logger.info() call
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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

Joined: Oct 27, 2005
Posts: 19722
    
  20

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

Joined: Feb 11, 2011
Posts: 77
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

Joined: Jan 10, 2002
Posts: 61434
    
  67

Yegor Bugayenko wrote:You pass "this" or any other object to Logger.info() call

I find that annoying. The static loggers, no so much.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Get Rid of Java Static Loggers