If I have a class that I want to accept an optional logger to log debug information:
public class A {
private Logger logger ;
public A( Logger logger ) {
this.logger = logger ;
}
public A() {
this( null ) ;
}
public void hello() {
logger.debug( "hello" ) ;
}
public void goodbye() {
logger.warn( "goodbye" ) ;
}
}
Is there any way for me to avoid the need for constant null checks?
public void hello() {
if( logger != null ) {
logger.debug( "hello" ) ;
}
}
public void goodbye() {
if( logger != null ) {
logger.warn( "goodbye" ) ;
}
}
I thought maybe if I made a logger wrapper class, that could do the checks for me and then either do nothing, or log the message. But then I would need to wrap each method I would want to use.