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.
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.
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.
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.
Use NOPLogger
:
return this.logger == null ? NOPLogger.NOP_LOGGER : this.logger;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With