This is a quick guide on how to use log4j in your Java project. This task requires only five simple steps listed below. Each of these step i...
This is a quick guide on how to use log4j in your Java project. This task requires only five simple steps listed below. Each of these step is explained in details below.
Let's create a new project named say log4j-helloworld and copy the log4j-1.2.17.jar into a folder named lib inside the project.
If you are using an IDE like Eclipse, you can simply add log4j jar file into the build path before compiling. When running the Main class, you need to give the path to log4j.properties file in run configuration.
Console output as as below.
Console has received both INFO and ERROR log messages, but not DEBUG.
However messages.log file has received only the ERROR level log messages as below.
This is because the Threshold level of the file logging appender is set to ERROR which overrides the rootLogger configuration.
Hope this helps you to start using log4j in your next project. I have added this project here for your references.
- Set up the project with log4j
- Create log4j.properties file
- Write a class to record log messages
- Compile and run program
- Check the log messages
1. Set up the project with log4j
As the first step, let's download the log4j 1.2.17 latest archive (tar or zip). When you extract the archive, log4j-1.2.17.jar file is available in the root of the directory.Let's create a new project named say log4j-helloworld and copy the log4j-1.2.17.jar into a folder named lib inside the project.
2. Create log4j.properties file
Next task is to create a log4j.properties file (shown below) inside the project folder. This file is the configuration file that provides how, where, which etc instructions on generating log messages.
# root level configurations
log4j.rootLogger=INFO,console,file
# configuration for console outputs
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
# configuration for file output (into a file named messages.log)
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=messages.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
# threshold for file output
log4j.appender.file.Threshold=ERROR
3. Write a class to record log messages
Let's write a simple class named Main (in src folder) with a main method to generate some log statements. Logger is added to the class as an attribute and used to write logs of different levels like debug, info and error.package com.digizol.log4j.helloworld;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
public class Main {
private static Logger log = Logger.getLogger(Main.class);
public static void main(String[] args) {
log.debug("This is debug method");
log.info("This is info method");
log.error("This is error method");
log.log(Level.DEBUG, "This is debug from Level.DEBUG");
log.log(Level.INFO, "This is info from Level.INFO");
log.log(Level.ERROR, "This is error from Level.ERROR");
}
}
4. Compile and run program
If you compile and run from command line, followings are the commands. Make sure to create classes folder inside project for generated class files before executing the commands.
# compile
javac -cp lib/log4j-1.2.17.jar -d classes
-sourcepath src src/com/digizol/log4j/helloworld/Main.java
# run
java -cp lib/log4j-1.2.17.jar:./classes:./
com.digizol.log4j.helloworld.Main
If you are using an IDE like Eclipse, you can simply add log4j jar file into the build path before compiling. When running the Main class, you need to give the path to log4j.properties file in run configuration.
5. Check the log messages
In the Main class, we have added two debug level messages. However the rootLogger is configured in INFO level in log4j.properties file, so none of the debug level log messages are expected as outputs.Console output as as below.
This is info method
This is error method
This is info from Level.INFO
This is error from Level.ERROR
However messages.log file has received only the ERROR level log messages as below.
This is error method
This is error from Level.ERROR
This is because the Threshold level of the file logging appender is set to ERROR which overrides the rootLogger configuration.
Hope this helps you to start using log4j in your next project. I have added this project here for your references.
COMMENTS