Sample log4j project creation in Eclipse IDE - BunksAllowed

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

Community

demo-image

Sample log4j project creation in Eclipse IDE

Share This

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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);
}
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 
  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.




Happy Exploring!



Comment Using!!

No comments:

Post a Comment

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