Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a dummy slf4j logger?

Can I get a dummy logger from slf4j? (Think the null object design pattern.) If so, can someone provide an example? Or will I have to implement a custom logger if I want to do that?

I'm hoping to write a function along the lines of

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 not get a NullPointerException on that last line if no logger has been set.

like image 954
user Avatar asked Mar 22 '11 09:03

user


People also ask

What logger does SLF4J use?

If SLF4J cannot find a binding on the class path it will emit a single warning message and default to no-operation implementation. SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP.

Is SLF4J logger safe?

Thus, if your SLF4J provider/binding is slf4j-log4j12. jar, you are safe regarding CVE-2021-44228. If you are using log4j-over-slf4j. jar in conjunction with the SLF4J API, you are safe unless the underlying implementation is log4j 2.

What is SLF4J Loggerfactory?

The org. slf4j. Logger interface is the main user entry point of SLF4J API. It is expected that logging takes place through concrete implementations of this interface.


1 Answers

Use NOPLogger:

return this.logger == null ? NOPLogger.NOP_LOGGER : this.logger; 
like image 155
axtavt Avatar answered Sep 20 '22 20:09

axtavt