1

I have a game that I have been writing in java as a hobby for a number of years. I have very sparse logging, just some simple output when something goes really wrong. It's about 25,000 lines of code.

I just released a first beta, and I'm realizing it is going to be a nightmare to deal with bug reports without good logs. Whenever something goes wrong on my test machine, I have the luxury of looking at the code and adding print statements if I need to figure out what's going wrong. Obviously, that's not available once its released in the wild.

  1. 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?
  2. Do you have any recommendations on how to go about doing it? I honestly don't know where to start.
  3. Do you have a recommendation for a logging system for Java?
  4. Do you have any good articles / best practices recommendations on what to log and how to log it?

Thanks! Any and all information is greatly appreciated.

4

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.

1

I'd suggest reading up on Aspect-Oriented Programming, specifically as it applies to logging: http://www.yegor256.com/2014/06/01/aop-aspectj-java-method-logging.html

AOP is a technique for keeping your methods clean and focused on business logic while extracting "plumbing" code - such as security, validation or, yes, logging - to external classes, and having the infrastructure apply them automatically. In this case, you create an Aspect called "MethodLogger" and apply it to your classes, and it hooks into your code before and/or after each method execution and performs its code, which can log the method call and its parameters.

It's not perfect logging, especially if you have long and complicated methods that need logging their computation state in the middle, but it can give you a whole lot of logging info, throughout your program, in one go.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.