How to put logger in java?

One of the most important things in java is to put a logger. Very simple process for those who know how to do it but for the new users of java, it a bit tricky, as I have observed when working with some of fresher’s in my organization. This post is for them only or even some time for those who do occasional development of new product. In those cases as well I see people forget how we have done it in last project. So we’ll do it step wise.

Step 1: You need to import log4j-1.2.14.jar file in your libraries. Don’t ask me where you can find it. If it’s not on your machine, then search Google.

Step 2: put a log4j.properties file along with your other properties file. If you don’t have one, you can put it in your ‘src’ folder. The content of the properties file should be

# For the general syntax of property based configuration files see the

# documenation of org.apache.log4j.PropertyConfigurator.

log4j.rootLogger=DEBUG, FILE, CONSOLE

#log4j.appender.ROOT.File=C:/Program Files/Apache Software Foundation/Apache Tomcat 6.0.14/logs/ProfileServices.log

log4j.appender.FILE=org.apache.log4j.FileAppender

log4j.appender.FILE.file=/idm/bea/logs/ProfileServices.log

log4j.appender.FILE.layout=org.apache.log4j.PatternLayout

log4j.appender.FILE.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) – %m%n

log4j.appender.FILE.append=false

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern=[%d{MMM dd HH:mm:ss}] %-5p (%F:%L) – %m%n

 

This file can be modified according to your requirement. You can add or delete the content you want to.

Step 3:  In you java file, where you want to put the logger, import log4j.Logger in your import section, as shown below

Import  org.apache.log4j.Logger;

Step 4: In the variable deceleration section of your class, decelare the logger as shown below

private static Logger logger = Logger.getLogger(className.class);

Note:  *className = Name of your class in which you are using this logger.

Step 5:  And you are done with all the base preparation. All you have to do now is to just use logger in your code as shown below in example

try {

                userProfileAL = ldapSerachBO.getUserProfiles(userName, url, baseDN);

                userProfile = new UserProfileData[userProfileAL.size()];

                logger.debug(“LdapSearchServiceImpl:getUserProfile userProfileAL Size ” + userProfileAL.size());

                for (int i = 0; i < userProfileAL.size(); i++) {

                                userProfile[i] = (UserProfileData) userProfileAL.get(i);

                                logger.debug(“LdapSearchServiceImpl:getUserProfile getKey()>” + userProfile[i].getKey());

                                logger.debug(“LdapSearchServiceImpl:getUserProfile getValue()>” + userProfile[i].getValue());

                }

 

You can use logger for debug as shown above in the example or for error or for info whatever your requirement is.

Just drop me a message if you want to know anything else in this.

 

–Vikas

Advertisements

Leave a comment

Filed under Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s