The rule of thumb when it comes to logging: "it's never too late!"
Do you think it is advisable to try to add logging to this program
now? Or do you think that I'm too far along to try to fix this
problem?
You can start off with some generic logging, which is agnostic to your application as such:
ENTRY/EXIT logs: You can add a logging statement at the start of every method and at every exit point at the TRACE
level. (Logging the in/out arguments is optional)
public int myMethod(int input) {
logger.trace(String.format("+++ [%d]", input));
.
.
.
logger.trace(String.format("--- [%d]", output));
return output;
}
This may sound trivial and benign at first, but if you compare having zero logs v/a having a trail of TRACE
logs, you'd be far better with the latter. It also helps in encapsulating logs from a single method between the ENTRY
and EXIT
statements in the logfile.
It can be cumbersome to add such logs in every method ever, so you can choose to be a ninja and use Aspect-Oriented Programming to do it quicker: How to automatically log the entry/exit of methods in Java?
- EXCEPTIONS: You can quickly replace all your
e.printStackTrace()
statements with a logging statement that puts the stack trace in the log file.
Do you have any recommendations on how to go about doing it? I
honestly don't know where to start.
Assuming you don't want to become an expert in logging and are simply looking for a way to 'logify' your code before the beta release, you can take a look at this quick tutorial on adding log statements.
Do you have a recommendation for a logging system for Java?
If you want to start quick, use log4j with the PatternLayout
. All it takes is a simple .properties
file and the JAR included to your classpath to see the logs rolling.
If you want to evaluate several logging libraries, but don't have the time right now, then I would suggest starting off with log4j but use the slf4j wrapper on top of it. What slf4j does is, it abstracts out the logging library so that later, you can easily swap log4j for something else, like logback, without having to change imports and logging statement across your code.
Do you have any good articles / best practices recommendations on what
to log and how to log it?
A few suggestions off the top of my head:
Declare a static logger object for every class: this will ensure proper location reporting of where the log statements come from
final static Logger logger = Logger.getLogger(classname.class);
Log an INFO
message for every successful operation that a method has done. It helps to not have a bird's-eye view at the code and only focus on one method at once instead when adding logs for functionality (and is also quicker since you can warp through your code base adding logs like a machine!)
- Make sure you log the EXCEPTION in the
ERROR
message in your catch() block - this will log the stack trace in the logfile instead of just the message
- Log a
TRACE
or an INFO
just before starting time-intensive operations, so that if a pedantic user is staring at the logs while your program is running, he/she will be tranquilized by the fact that something time-intensive is happening and the program hasn't crashed just because the logging paused for a while
Happy logging! Be assured that your efforts will pay off.