Log4J 4 You

June 9, 2014 at 8:42 am (Computer Science, Java, Processing, Programming, Teaching) (, , , , , , )


Last weekend I added logging to my game engine since I finally got fed up with print logging to console. I’ve waited this long because while it was on my To-Do list, it kept getting pushed back because it isn’t as interesting of a task as rendering a new HUD for example. It is definitely worth investing in though! If you don’t have logging in your game, I suggest you do it now!

In this post, I will talk about setting up Log4J to use in either Java or Processing programs. Furthermore, I will show you how to set up loggers programmatically. Typically you will see guides that reference config files or XML for setting up loggers. I chose to do it programmatically because I wanted to set it up early in my engine code, have it trickle down into all of the other layers, and not have to change the configurations very often. Perhaps this isn’t a good enough reason to do what I did, but at least for my situation I didn’t mind using it.

Before I get into how to do it this way, I will warn you of one of the major drawbacks of doing this: you can’t change the default logging levels without recompiling your code. This is actually a big deal for many projects, but for some not so much. One of the advantages of doing it programmatically however, especially with Processing sketches, is that once you set up a Log4J library you can copy and paste code and it will just work. No configuration files required.

Enough talk! Lets see some how-to!

 

Add Library (For Processing sketches only)

If you are setting up Log4J for use in Processing sketches, you first need to create a Library. There are many guides on how to do this, so I will just give an overview. After you download a log4j JAR file, navigate to your “libraries” folder (I forget where the default spot is at the moment; I keep mine in the same folder as my sketches). Create a new folder named “Log4J” or if you’d like, put in the version numbers. NOTE: The folder MUST have the same name as the JAR file. Within that folder, create a folder named “library” and dump that JAR file in there. After you restart the Processing IDE, you will see a new entry when you go to import a library.

EDIT: I’ve been informed that you can actually just drop the JAR into the sketch folder! That’s very handy. It makes it even easier to integrate Log4J. The only downside to doing this is that if you work with many sketches, you have to remember to drop the JAR into each folder instead of just referencing your library. Thanks to Nacho Cossio for pointing this out.

Use Log4J

Now it’s time to use it! Here is some code that will set up a logger.

String masterPattern = “[%c{1}], %d{HH:mm:ss}, %-5p, {%C}, %m%n”;
fa = new FileAppender();
fa.setName(“Master”);
fa.setFile(sketchPath(“./Logs/” + “Master.log”));
fa.setLayout(new PatternLayout(masterPattern));
fa.setThreshold(Level.DEBUG);
fa.setAppend(false);
fa.activateOptions();
Logger.getRootLogger().addAppender(fa);

This will give you a logger that will dump out everything to file. The “masterPattern” variable holds the format that each log message will have. This specific line of gibberish tells the logger that I want it to look like this:

[categoryName], time(hour:minute:seconds), Priority (if any), {Class name where message was logged from}, message

The category name in my example will be “Master”. I’ve also put commas in there so that I can optionally open the logs in Excel and have it put each value in its own column. This is very handy as you could then use searching and sorting features with zero effort.

Note that in the last line of code, I add the appender to the root logger. Do this only if you want all log messages to go to one file. In my game engine, I’ve set up multiple loggers for different categories to go to different files, and then I use a master logger to consolidate them to one file.

Log Messages

Logger logger = Logger.getLogger("Master");
logger.debug("Log my message");

This will get the logger (which you only need to do once if you store it in a variable) and output a debug message.

There you have it; easy logging for Processing sketches or Java programs. There are likely better and more in-depth tutorials for using Log4J, however I just wanted to give an introduction to get it to pay off quickly without having to browse to nine different sites like I did.

 

Advertisements

2 Comments

  1. nacho cossio (@kosowski_) said,

    Hi Jake, that’s handy, thanks.
    About including the Log4J jar, it is easier to just drag drop the .jar file into the Processing sketch window, wich copies the file in a ‘code’ folder inside the sketch’s folder. All jars in this folder are automatically imported by Processing.
    Regards.

    • jestermax said,

      Thanks for the tip! I didn’t actually realize it did that; for my own game engine, I’ve set up my engine JAR as its own library and build to that directory.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: