Get the dummy slf4j logger?

Can I get a dummy logger from slf4j? (Think of a null object design pattern.) If so, can anyone give an example? Or will I need to run my own journal if I want to do this?

I hope to write a function line by line

private Logger logger; static Logger nullLogger; static { nullLogger = getMeADummyLogger(); } public Logger getLogger() { return this.logger == null ? nullLogger : this.logger; } // then, elsewhere: this.getLogger().info("something just happened"); 

and do not get a NullPointerException on this last line unless a logger is set.

+8
java null-object-pattern slf4j
source share
1 answer

Use NOPLogger :

 return this.logger == null ? NOPLogger.NOP_LOGGER : this.logger; 
+15
source share

All Articles