Logging with Log4j2


Log4j2 is a successor to the original Log4j — a Java utility library used to output log files from a program. It writes messages to various endpoints can include all sorts of destinations like emails, webpages, databases and more. I tried a few other newer loggers like SLF4J, and came back to this one because the others were so difficult to set up.  Log4j2 is simple, lightweight and powerful.  Here we will set up Log4j2 in a Java project in Eclipse.

Maven is the build configurator. Here are the lines from the Maven pom.xml file that are necessary for Log4j2 to work. You need both of these as a minimum. Be sure the check for the latest version and update yours accordingly.

Log4j2 Config File

The configuration or preferences file is usually an xml file, although you can also use JSON or a plain text properties file. This file is critical and can have a number of different names. I chose log4j2-test.xml. It needs to reside in the Java classpath. Ours today is here in src/test/resources. This is very bare-bones setup except for the RollingFile appender which gives files datestamps.

The Logger is the actor who is doing the logging. Each logger can have any number of appenders. You can have an number of loggers configured in many different ways and callled on at different times. In this case we only have one logger called Root. There is always at least the root logger, and it’s the top of the hierarchy. We’ll just use that one since we’re trying to keep this simple.

An Appender is actually the location that the information is going to – like the console. You can specify reports, files, emails, network locations, sockets, databases, etc. Within the Appender section is where you set up what levels of output you want and to which locations, as well and other settings. The first one we have set up is for the console.
The RollingFile appender sets up our log files so that when a new one is created, the last log file is renamed with a datestamp so it’s not overwritten. Notice each appender has a PatternLayout which is self-explanatory.

Here are the Java imports which won’t work unless your maven log4j statements are set up correctly.

Here is the Setup code in the the Java program where it actually connects to it’s xml configuration file. I call the root logger using a special method called “getRootLogger” – for all other loggers you would use the method “getLogger”.

And here’s where we actually use the logger. Just replace your normal System.out.println with yourLogger.level – as in this case it’s gLogger.info – for the info level (see levels below).

Log4j Log Levels

Each level prints itself plus the levels that came before it. So if you choose WARN you also get ERROR and FATAL messages. Each level enables a finer granularity of detail. DEBUG and TRACE are intended for development only.

OFF No logging
FATAL Severe errors
ERROR non-fatal errors
WARN misc warnings
INFO runtime events
DEBUG Detailed info
TRACE Most detailed
TTCC is a message format used by log4j -an acronym for Time Thread Category Component.

See the entire project at:
https://github.com/nmschorr/git2/tree/master/SMedia

Comments are closed.