-
Notifications
You must be signed in to change notification settings - Fork 1
Using log4j
Log4J is a logging framework for java applications.
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");
}
}
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 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.
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.
Informative messages are used when you want to print out messages that aren't warnings or errors.
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.
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.
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.
Log4j is configured with a property file called log4j.properties
. This file is located in the folder code/etc
.
To use log4j in eclipse you need to do two things.
- Add log4j to the build path of your project. Right click your project and choose
Build Path -> Add External Jar
. Choose theLibrary
tab and add an external jar. Navigate to ../lib and choose the log4j-1.2.17.jar`. - No choose
run configurations
from the run menu.
Choose your launch configuration and go to the tab Classpath
. Select the User entries
line and click on Advanced
, Add External Folder
and select the ../etc
folder.
.