开发者

Get a dummy slf4j logger?

开发者 https://www.devze.com 2023-02-19 09:24 出处:网络
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?

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 ? nu开发者_如何学JAVAllLogger : 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.


Use NOPLogger:

return this.logger == null ? NOPLogger.NOP_LOGGER : this.logger; 
0

精彩评论

暂无评论...
验证码 换一张
取 消