Get a dummy slf4j logger?
Asked Answered
D

1

10

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.

Drye answered 22/3, 2011 at 9:31 Comment(0)
U
19

Use NOPLogger:

return this.logger == null ? NOPLogger.NOP_LOGGER : this.logger; 
Upsetting answered 22/3, 2011 at 9:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.