Skip to content
SWP-Comp-Ch3ck3r edited this page Apr 27, 2013 · 9 revisions

Log4J is a logging framework for java applications.

Setting up eclipse to work with log4j

Add log4j to the build path of your project. Right click your project and choose Build Path -> Add External Jar. Navigate to ../lib and choose the log4j-1.2.17.jar and the log4jprops.jar.

Using Log4J

To use log4j inside your component you need to create a logger. The standard way of doing so is

public class SomeClass {
    private static Logger logger = Logger.getLogger(SomeClass.class);

You can then use the logger to print out messages as needed.

    public static void main(String[] args) {
        logger.trace("entering main");
        logger.info("Welcome to this main stub");
        logger.trace("leaving main");
    }
}

Log Levels

Supported log levels of log4j are

  • trace
  • debug
  • info
  • warn
  • error
  • fatal

You should always choose the correct level when logging your messages, here are some tips:

trace

Trace is the most verbose level. You should always print a trace message when entering oder leaving a method. Sometimes it is also useful to trace if / else branches. Often traces are not used because they are a lot of extra lines of codes but sometimes they can be very useful.

debug

Use this level to print debug message. Debug messages deliver optional informations that are useful for development. The productive application should never need to show debug messages.

info

Informative messages are used when you want to print out messages that aren't warnings or errors.

warn

The warn level is used to log warnings. Warnings indicate problems during the program execution. Warnings do not indicate errors. A warning does not interrupt the flow of your program.

error

The error level indicates an unexpected situation in your program. An error is unexpected behaviour but can be recovered during the program execution. You should handle the error. The program should not crash.

fatal

The fatal level is the most severe level available. A fatal message indicates an error that is not recoverable. If a fatal error occurs your program will crash immediately after the fatal error was log.

Configuration

Log4j is configured with a property file called log4j.properties. This file is located in the folder code/etc.

Clone this wiki locally