Can you even count the number of times you've setup Log4j in a web application? This is one of those tasks we find ourselves doing over and over again, yet each time, we have to dig up old code to remind ourselves how it's done. Hopefully, this can save you some of that hassle. This page provides a quick guide for setting up Log4j in a web application. It is intended for those who are already familiar with Log4j and just need a rapid reminder.
| We've been observing a trending towards SLF4J |
Step 1 - Download the Log4j library
Download the latest binary release from the Apache Logging Services site
. If you are using Maven 2 for your project, you do not need to download the binary. You can simply declare it as a dependency in your Maven 2 pom.xml and Maven will grab the library for you. Declare it as follows:
Step 2 - Import the jar file into your web project
If your using Maven 2, you can skip this step; after declaring the dependency, Maven will put the jar file in the proper place automatically when you build.
Import the log4j jar file into your project WEB-INF/lib directory so that it will be on the classpath for your web application.
You might instead put the jar file on your server in some globally recognizable location so that more than one application can have visibility to the same lib. For example, in WebSphere AppServer, you might put it under <WebSphere install>/AppServer/lib. You can also make it available to all portlets in a WebSphere Portal instance by putting it in <WebSphere install>/PortalServer/shared/app. In Tomcat, you can use the shared lib folder (<Tomcat>/lib). Always look for some existing version of the log4j library first. You may either already have it or you may wish to use a newer release; you do not want to end up with two versions of the log4j lib in the same directory, but with different names.
Step 3 - Import some existing (starter) Log4j XML configuration files
Yes, you can use a simple Java properties file to configure Log4j, but it's a lot less flexible than the XML configuration format; don't be a wimp!
Create a properties folder and make it recognizable by the IDE as a source folder
Skip this step for Maven 2 projects.
I like to do my Log4j configuration in a separate properties folder outside of the normal src folder in Eclipse. It keeps the project clean and it can also be useful to the formal build system. Here's how:
- In Eclipse, right click on the project node.
- Select New -> Folder and create a new folder called 'properties'. Make sure the folder is in the root of your project, not in the src folder.
- Now we want the properties folder to get recognized as a source folder. Right click the project node and select Properties.</li>
- In the Properties dialog, select Java Build Path -> Source tab
- Click Add Folder, choose the properties folder, and click OK.
Import Log4j DTD and example (starter) XML configuration files
Next, you need to import two files into your properties folder (for traditional dynamic web project). For a Maven 2 web project, import them into <project>/Java Resources/src.main/resources. I am including versions here that you can download and use to start with:
You may also want to grab this simple console test for quick confirmation that you've got the logging set-up properly. From within eclipse, you can right-click this java file and choose Run As.. > Java Application. That will print several log messages to the console to give you confirmation.
Step 4 - Customize the XML configuration just enough to test
Go ahead and open up the log4j.xml file. If you used the starter provided in the link above, you'll find the following:
Now, this is really simple even though it may look a bit daunting at first. We've got two console appenders (OUT and ERR) and we have one file appender that will write our log statements to disk. In this way, we get console logging for development and file logging for deployment. There's also an email appender, but it is commented out for now. I would keep it if I were you; you may later wish to receive email notifications if your application throws errors.
- Find SSB_ROLLING_FILE and change the name to reflect your own application's name. Change both the declaration and the reference to it in the logger that is defined later in the file.
- In the file appender, customize the File parameter so that the logs will get written to a path of your own choosing.
- The first logger defined is set to log on anything under the package com.burlesontech.ssb. Change that to reflect some parent package in your own application.
- Save the file (duh!)
Step 5 - Put logging code in your classes
The last thing you need to do is drop some logging code in a class and test this whole setup.
Add the following to the imports section of your java code:
Add the following at the top of your class in the global section (just under the line that declares your class public class Whatever extends Whatever {. Change the name of the class in the getLogger method call, of course. Name it the same as the class you're dropping this code into.
Apache Commons Logging
If you want to use Apache commons logging, instead add the following imports:
And then declare the logger like so:
Throw some logging statements in your code somewhere where you know they'll be fired right away when you run your app. For example:
Step 6 - Run your app and make sure it works
Finally, run your app and make sure it works. You should see log lines in your console and in the file where you setup your appender. If it doesn't work, just review these steps a little more carefully and fiddle with it.
Java Logging Standards and Guidelines
Make your logging output as consistent as possible across classes.
If you found this content useful, please let us know...
Add Comment