-
Download log4j http://logging.apache.org/site/binindex.cgi
-
Unpack the .zip file in your Java folder (In Windows it is usually C:\\Program Files\\Java\\)
-
In Eclipse: Window > Preferences… > Java > Build Path > User Libraries > New: "log4j" > OK Add JARs… > navigate to find your log4j .jar you just unpacked in the Java Folder > OK
-
Right click on your project in the Package Explorer > New > Folder In “Folder name” write "log4j", click Advanced. Select “Link to a folder in the file system” Create a new folder "log4j" in your project folder in the file system.
-
Place this file you are reading right now in in the folder you just created. Name the file as log4j.properties
-
In Eclipse: Run > Run… In the navigation bar on the left select either the server, or the runnable class, or the JUnit test you want to log with log4j
Select the Classpath tab > User Entries > Advanced… > Add folders > OK
Select the "log4j" folder you created in your project and click OK
-
Repeat step 6 for other servers, runnable classes or JUnit tests you want to log
-
Change in the following line the "org.example.foo.bar" with whatever you want:
log4j.category.org.example.foo.bar=DEBUG
-
Add the following import to the Java Class you want to log: import org.apache.log4j.Logger;
-
Add this lines to the Java Class you want to log:
/**
- Log4j logger */ static Logger log4j = Logger.getLogger("org.example.foo.bar");
-
Substitute in the code above "org.example.foo.bar" with whatever your wrote in the step 8.
-
Add something like the following code in your Class whenever you want to log:
log4j.debug("WTF?");
-
Repeat steps 9, 10, 11 and 12 for every Java Class you want to log
-
Enjoy!