Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the overhead of creating a SLF4J loggers in static vs. non-static contexts?

I've always used the following pattern to construct (SLF4J) loggers:

private static final Logger log = LoggerFactory.getLogger(MyClass.class); 

This has worked so far, but I was wondering about the static context at some point and the need to pass in the concrete class literal all the time instead of just using a non-static logger like

private final Logger log = LoggerFactory.getLogger(getClass()); 

This has basically been asked (and answered) before here for LOG4J

Should logger be private static or not

and here

Should be logger always final and static?

I realize final is basically mandatory, so I'm left wondering how high the overhead of using SLF4J's in non-static context actually is.

Q:

Is there any significant practical overhead of using

private final Logger log = LoggerFactory.getLogger(getClass()); 

over

private static final Logger log = LoggerFactory.getLogger(MyClass.class); 

in the average (web) app? (no need to "discuss" high-end, heavy-load webapps here)


Note, I'm ultimately planning to use an even nicer approach using CDI to obtain an SLF4J logger like

@Inject private final Logger log; 

as described here http://www.seamframework.org/Weld/PortableExtensionsPackage#H-TtLoggerttInjection, but I need to know about the logger caching first.

Sub question: is it even possible to use?:

@Inject private static final Logger log; 

(just beginning with CDI to be honest)

like image 982
Kawu Avatar asked Apr 27 '12 05:04

Kawu


People also ask

Should loggers be static?

Loggers should be declared to be static and final. It is good programming practice to share a single logger object between all of the instances of a particular class and to use the same logger for the duration of the program.

What is the advantage of SLF4J?

This is the main purpose of SLF4J (Simple Logging Facade for Java) – a logging abstraction which helps to decouple your application from the underlying logger by allowing it to be plugged in – at runtime. Of course, the flexibility that such an abstraction provides is the main reason to use SLF4J.

Does SLF4J use Commons Logging?

The underlying logging framework can be any of the frameworks supported by SLF4J. Often times, replacing commons-logging. jar with jcl-over-slf4j. jar will immediately and permanently solve class loader issues related to commons logging.

What logger does SLF4J use?

SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP. The logback project supports SLF4J natively.


1 Answers

The overhead for non-static (instance) logger variables should be negligible unless many, say 10000 or more, instantiations occur. The key word here is negligible. If many (>10000) objects are instantiated, the impact will probably be measurable but still be low.

More specifically, an instance logger increases the memory footprint by one reference (64 bits) per object instance. On the CPU side, the cost is one hash look up per instance, i.e. the cost of looking up the appropriate logger in a hash table (small). Again, both costs should be negligible unless many many objects are created.

This question is also discussed in the SLF4J FAQ.

like image 69
Ceki Avatar answered Sep 29 '22 02:09

Ceki