Logging

From Slick2D Wiki
Jump to: navigation, search

The logging package provided by slick (org.newdawn.slick.util.Log) is relatively straightforward. By default it logs all output to standard out, so everything is displayed in the console window that your game is launched from. This can be easily changed to dump info to a file. The logger is a zero setup utility so long as you want to print all messages and have them displayed in the console.

Basic Logging[edit]

The slick logger supports four “levels” of messages. These can be used to, for instance, separate out debugging information from more critical game state errors. The four available logging levels are info, debug, warn, and error. The first two of these can be turned on and off as needed. There is a static member function of the Log class that corresponds to each logging level. These functions all take a string that is the log message.

Here is an example using the logging class:

	// ... Some code
	Log.info("A logging message");

The basics of it are that simple. When Log.info is called it will generate a log entry similar to the following:

    Wed Mar 26 22:06:30 PDT 2008 INFO:A logging message.

Advanced Logging[edit]

Logging Exceptions[edit]

Although the basic logging concept provided above is useful, one area where it doesn't help much is documenting exceptions. There are two other version of the Log.error method. One accepts a string representing a logging message, as above, and an Exception object. The other takes just an Exception object. These will print the logging message (if provided) and the stack trace generated by the exception. Here's an example of each:

    try {	
    // ... Some code
    }
    catch (SlickException e1) {
        Log.error("A slick-specific problem occurred", e1);
    }
    catch (Exception e2) {
        Log.error(e2);
    }

Generally it is more useful to provide a logging message along with an exception stack trace, but if you need to log only an exception the ability is there. Here's a sample of the output from a log generated with an exception:

    // Assuming our exception has the message "A logged exception."
    Log.error("A logging message." e);
    
    // Results in the following console message:
    Wed Mar 26 22:14:50 PDT 2008 ERROR:A logging message.
    Wed Mar 26 22:14:50 PDT 2008 ERROR:A logged exception.
    java.lang.Exception: A logged exception.
    	at clojure.fns.clojure.make_ant__1122.invoke(Unknown Source)
    	at clojure.fns.clojure.TEST_create_ant__1120.invoke(main.clj:276)
    	at clojure.lang.Var.invoke(Var.java:268)
    	at com.finiteimprobability.Invasion.keyPressed(Unknown Source)
    	at org.newdawn.slick.Input.poll(Input.java:877)
    	at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:417)
    	at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:344)
	at com.finiteimprobability.Invasion.main(Unknown Source)

As you can see the logging message is printed before the stack trace, which includes the exception message.

Reducing Logger Output[edit]

By default the logger will print all logged messages to the log. The method Log.setVerbose takes a single boolean argument. If set to false info and debug messages will not be printed.