Get a dummy slf4j logger?

南楼画角 提交于 2021-02-07 12:27:25

问题


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.


回答1:


Use NOPLogger:

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


来源:https://stackoverflow.com/questions/5389137/get-a-dummy-slf4j-logger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!