开发者

Do you provide DEBUG logging level for private methods?

I add logging for Java classes.In particularly, logger.debug level (for development mode).

As far private method called within public ones, is there sense to provide logging wi开发者_高级运维thin private methods?

Is it efficiently?

Thank you.


It's not necessarily a matter of efficiency or not. You have to ask yourself: "Does that private method do something worthy of logging?"

In order to avoid performance issues, you can always check the logger whether it will log debug messages or not, before actually logging those messages. In log4j, you'd write

if (logger.isDebugEnabled()) {
  logger.debug("complicated" + "innefficient" + "string concatenation");
}

I imagine, other loggers provide similar concepts


yeah, if you want to "debug" your private methods in maintance mode or development, why not? in production you'll set your logging level either warn or error! that's upto you...


Of course. Private methods usually hide implementation detail, and that's where most bugs lurk. Put your logging statements where you feel the need, and disable them in production.


Logging might seem wasteful when you don't need it, but it is indispensable when you do. Always provide meaningful log messages in your classes and functions. The log levels are there for you to be able to filter out the noise, and increase verbosity when you need it. Good loggers provide capabilities to change log levels at runtime.

Still, if you are worried about performance for whatever reason, you can use some simple guards to reduce the overhead of logging, especially debug logging, which is rarely on. With log4j/slf4j for example, you can wrap log debug statements in:

if(logger.isDebugEnabled()) {
    logger.debug("something");
}

slf4j additionally has a printf-like syntax which only does string formatting if the log level is correct.

logger.debug("Object {} is not valid!", obj);

Like darioo commented, this second form removed the need to check the log level before the log statement.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜