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 within private methods?Is it efficiently?

Thank you.

Answers


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.


Need Your Help

How to avoid Name-itis in Windows Workflow (WF)?

c# .net workflow-foundation

I just started working with WF and what I find really annoying is that you have to name

Cannot programmatically combine AND and OR conditions using Arel

ruby-on-rails-3.2 rails-activerecord arel

Given the SQL conditions cond1, cond2 and cond3 generated using Arel operators (.eq for example), I cannot seem to use Arel to produce the SQL:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.