Logging and Monitoring

Logging Like a Newb

Many developers just use System.out.println() to print diagnostic strings to the terminal. The problem with that is that before the release, you'd have to go through all your code and make certain you removed all these println() calls. You do not want your users to see them and worry about ominous strings babbling about old development diagnostics.

Logging Like a Pro

Instead of println(), you use the standard Java logger from java.util.logging. It has many advantages for professional game development:

  • You "tag" each message with a log level: Severe error, informative warning, etc.
  • You can switch off printing of all messages up to certain log level with just one line of code.
    • During development, you would set the log level to fine, because you want all warnings printed.
    • For the release, you set the log level to only report severe errors, and no informative diagnostics.
  • The logger string is localizable, since it contains variables. You may want to localize all errors.

So to print comments like a pro, you use the following logger syntax. The variables a, b, c, can be any printable Java object, e.g. Vector3f a = cam.getLocation(). They are numbered {0},{1},{2},etc for use in the string, in the order you put them in the Object array.

private static final Logger logger = Logger.getLogger(HelloWorld.class.getName());

Replace HelloWorld by the name of the class where you are using this line.

logger.log(Level.WARNING, "ok seriously wtf somebody check why {0} is {1} again?!", 
                      new Object[]{a , b});

or

logger.log(Level.SEVERE, "Game error: {0} must not be {1} after {2}! Please check your flux generator.", 
                      new Object[]{a , b , c});

As you see in the example, you should phrase potentially "customer facing" errors in a neutral way and offer a reason and a solution. If you use WARNINGs as replacement for casual printlns, make sure you deactivate them for the release.

More details about Java log levels here.

Switching the Logger on and off

In the release version you will deactivate the logging output to the terminal.

To deactivate the default logger for a release, you set the log level to only report severe messages:

Logger.getLogger(””).setLevel(Level.SEVERE);

During development, you can tune down the default logger, and set the log level to only report warnings:

Logger.getLogger(””).setLevel(Level.WARNING);

To reactivate full logging, e.g. for debugging and testing:

Logger.getLogger(””).setLevel(Level.FINE);
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution 3.0 Unported