Intended behaviour. It could not possibly happen any other way - in your second example o.hashCode() is evaluated *before* logger.debug, so there is no way that SLF4J can know whether the log level is debug.
The fact that you avoid the NPE in the first case is luck - turn the log level to debug and you will get the NPE. The bug is in your code - it is a bug that o may be null and you are calling methods on it without checking for null. ----- Original Message ----- > From: [email protected] > To: [email protected] > Sent: Thursday, 26 January, 2012 8:28:04 AM > Subject: [slf4j-user] (no subject) > Hello, > > maybe I'm wrong, but I from the description > > http://www.slf4j.org/faq.html#logging_performance > > I understand, that the following two statements should be equivalent, > considering the current logging level is INFO or higher: > > // first > if (logger.isDebugEnabled()) { > logger.debug("Msg: " + someObject); > } > > // second > logger.debug("Msg: {}", someObject); > > But they aren't, if you want to call a method from the object: > > // Object might be null, for some reason in some logging levels > Object o = null; > > // Everythings fine > if (logger.isDebugEnabled()) { > logger.debug("Msg: " + o.hashCode()); > } > > // NullPointerException is thrown, since o is null for DEBUG level > logger.debug("Msg: {}", o.hashCode()); > > Is this a bug or intended behaviour? I couldn't find any hints in the > documentation. > > Greetings, > > Aaron Kunde > -- > Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir > belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de > _______________________________________________ > slf4j-user mailing list > [email protected] > http://mailman.qos.ch/mailman/listinfo/slf4j-user _______________________________________________ slf4j-user mailing list [email protected] http://mailman.qos.ch/mailman/listinfo/slf4j-user
