Following the Great Relogging activity that took place recently, a revised format for logging in Java code is established as follows:
Logging.LOGGING
compile-time flag has been removed. Do not use it.org.apache.log4j.Category
is deprecated in favor oforg.apache.log4j.Logger
. Example:
private static final Category log = Logging.LOGGING ? Category.getInstance(POD.class.getName()) : null;
is replaced withprivate static final Logger log = Logger.getLogger(POD.class
.getName()
);
- A class should never add "implements Logging" to its declaration statement.
- The following new runtime logging level checks are available:
log.isFatalEnabled() // currently unused
log.isErrorEnabled()
log.isWarnEnabled()
log.isInfoEnabled()
log.isDebugEnabled()
log.isTraceEnabled() // currently unused
Example:
if (log.isInfoEnabled())
{
log.info("restoreRecordings: Storage not ready - deferring load of recording database");
}
Please note that curly braces are required around thelog.info(...);
call. The following should not be used:
if (log.isInfoEnabled())
{
log.info("restoreRecordings: Storage not ready - deferring load of recording database");
}
or
if (log.isInfoEnabled()) log.info("restoreRecordings: Storage not ready - deferring load of recording database");
- Every single log message should have an
if (log.isXXXEnabled())
check, even where there are two consecutive log messages in the source. Example:
if (log.isDebugEnabled()
{
log.debug("Destroy called. Ok.");
}
if (log.isDebugEnabled()
{
log.debug("Received Tuning Over failed event");
}
The following should not be used:
if (log.isDebugEnabled()
{
log.debug("Destroy called. Ok.");
log.debug("Received Tuning Over failed event");
}
- Mixing business logic with logging level checks should be avoided. Example:
if (low)
{
if (log.isWarnEnabled())
{
log.warn("VM memory low even after reclamation");
}
The following should not be used:
if (log.isWarnEnabled() && low)
{
log.warn("VM memory low even after reclamation");
}
- Also, please note that the business logic checks should be nested outside rather than inside of the logging statements. Using the previous example, the following should not be used:
if (log.isWarnEnabled())
{
if (low)
{
log.warn("VM memory low even after reclamation");
}
- Do not use inherited loggers. Don't make a logger protected in a parent class and use that logger from a subclass. Every class that needs to log should just have its own logger.
- The logger should always be named
log
. - Any logging-specific logic that is not part of the actual log call should also be wrapped in a log level check. Use:
For reference, here is the link to our coding guidelines. Refer to section 2.4: Logging Guidelines.