How to Create and Configure log4j Project using Eclipse IDE - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

How to Create and Configure log4j Project using Eclipse IDE

Share This
In this tutorial, we are going to demonstrate how you can create and configure the log4j project with Eclipse. In case, you are not that aware of log4j, you can check this out.
Here we go!


Create a Java project in Eclipse IDE. In this example, the structure of the project is shown below.


project_structure
In this project, TestLog4j, we have created a directory lib. We have downloaded log4j-1.2.17.jar and kept it in the lib directory. We have also created a file, log4j.properties, and kept it in the project.


The content of log4j.properties file


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# Root logger option
log4j.rootLogger=DEBUG, stdout, file
# The following properties are set to redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# The following properties are set to redirect log messages to a file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\log4j.log
log4j.appender.file.MaxFileSize=20MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


The content of the Test.java file

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.t4b.log.test;
import org.apache.log4j.Logger;
public class Test {
final static Logger logger = Logger.getLogger(Test.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
Test obj = new Test();
obj.testMethod("Totorials4Beginner");
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The jar file has been configured as a required library of the project. If you are not familiar with this configuration, you may go through the following steps.


Java Build Path Configuration

1. Right-click on the project in Project Explorer and select Properties.

2. Click on Java Build Path, then select Libraries Tab

project_properties_add_jar

3. Click on Add Jars, and a new window will be opened.

4. In the Jar Selection window expand the project, then expand the lib directory where the jar file is kept.

project_jar_selection

5. Select the jar file and click on Ok.

6. After returning to the previous window, click on Apply and Close.

project_properties_add_jar_complete

Now, you may run the project. To run this project, right-click on the Java file or Project, go to Run as and select Java Application.

We hope, that you have thoroughly understood how to configure log4j project.

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.