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.
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.
2. Click on Java Build Path, then select Libraries Tab
We hope, that you have thoroughly understood how to configure log4j project.
Here we go!
Create a Java project in Eclipse IDE. In this example, the structure of the project is shown below.
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
# 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
The content of the Test.java file
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");
}
private void testMethod(String msg) {
if (logger.isDebugEnabled())
logger.debug("This is debug : " + msg);
if (logger.isInfoEnabled())
logger.info("This is info : " + msg);
logger.warn("This is warn : " + msg);
logger.error("This is error : " + msg);
logger.fatal("This is fatal : " + msg);
}
}
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
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.
5. Select the jar file and click on Ok.
6. After returning to the previous window, click on Apply and Close.
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.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.