Logging within Business Logic
The following describes how to implement logging when adding a class that implements business logic:
- Add a constant referencing the logger for the class. By convention, logger should be named
"logger" and should pass the declaring class as its argument. For example, a logger
in the Adjustment_CHandler class would be declared as follows:
private static final Logger logger = LoggerFactory.getLogger(Adjustment_CHandler.class);
- Add entries with the appropriate logging level. The levels are: debug, info, warn, error, and
fatal. The following will log a warning entry to the log:
logger.warn("Unexpected status for frozen adjustment: " + status);
- In general, we expect entries of level info or more severe to be rare, and therefore,
to not impose a substantial performance penalty. However, we can expect debug
entries to be very fine grained, and they usually will not find their way to actual
logs, but will be filtered out via runtime configuration. To lessen the performance
impact of debug logging, the logging statement should be wrapped as follows:
if (logger.isDebugEnabled()) { logger.debug("Processing adjustment " + adjustment.getId()); }
- There are times when you want to know how long a code block takes to execute. In general, the
logging provides the time each log statement is issued. However, it is clearer to
see an actual elapsed time of some process being investigated. In this case, there
are some additional methods on the logger:
debugStart(message) or infoStart(message) debugTime(message, start) or infoTime(message, start)
- These should be used in the pairs given, as follows:
long start = debugStart("Starting process"); //... code for process debugTime("End process", start);
- This will cause each statement to be logged, plus the final End Process
statement will give the elapsed time since
debugStart
was called.
- These should be used in the pairs given, as follows:
Please refer to the JavaDocs on the com.splwg.shared.logging.Logger
class for
more details.