Log4j

Prerequisites

Development environment

  1. JDK 1.8 Downloads
  2. Eclipse Mars Download

Required jar files

log4j-1.2.17.jar

Maven dependencies

In order to run the example; simply follow following steps:

Step 1 Create Java Project

Launch Eclipse IDE; Create new Java Project by going to File -> New -> Others… -> Java Project. Choose any project name you want then click Finish.

Step 2 Create Java Class

Copy the code below to clipboard; Select src folder in your project; Press CTRL + V; Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.

Step 3 Create log4j.properties

Create an empty file named log4j.properties under src folder; and paste the content below:

Step 4 Configure Build Path…

  1. Download Apache log4j 1.2.17; and then to unpack it.
  2. Create lib folder by right click to the project select New -> Folder. Copy the log4j-1.2.17.jar file from the unpacked folder; and paste to the lib folder.
  3. Right click to the project; select Build Path -> Configure Build Path… Select Libraries tab; click Add Jars… Select the jar file then click Ok.  Click Ok to finish the configuration.

Step 5 Run the application

Right click to the class; select Run As -> Java Application.

Output

Log information will be logged in Console window as well as the configured file in log4j.properties. In this example, it’s C:/log/test.log.