Friday, September 10, 2010

log4j implementation in Web application .



Prerequisites:

1) Create a log4j.properties file inside the src folder of your project as shown in the screen below.



2) Copy the log4j-1.2.11.jar inside the WEB-INF/lib folder also displayed in the screen below.



3) Application Server used in this case is Tomcat 6.0.


Log4j.properties:



### direct log messages to a file ###
log4j.appender.file=org.apache.log4j.FileAppender

log4j.appender.file.File=C:/apache-tomcat-6.0.18/logs/ideapokebeta.log

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

log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=info, file
log4j.logger.javax.faces =debug


Explanation:

The file (log4j.properties) created earlier needs to be pasted with the above lines of code. After pasting, you have to check for your Tomcat’s location in your computer, copy it and paste it against “log4j.appender.file.File“. Beforehand you need to create a .log file and paste it in the logs folder of Tomcat.

Implementation :

Code Snippet(java class):




public class ForgotPassword
{
private String fullname;
private String email;
private boolean exist;
private UserLogin userLogin;
private static Log log = LogFactory.getLog(ForgotPassword.class);
// Setter & Getter Methods Starts //
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public boolean isExist() {
return exist;}

public void setExist(boolean exist) {
this.exist = exist;
}
// Setter & Getter Methods ends here //

// forgot Method is called for sending the password to email provided //
public String forgot() throws Exception
{
String status="failure";
exist=false;
String[] to = new String[1];
HibernateSpringDAO dao = (HibernateSpringDAO)ServiceFinder.findBean("SpringHibernateDao");
dao1=(HibernateSpringDAO)ServiceFinder.findBean("mailbean");
if(!this.getEmail().equals("") && (dao.validateEmailOnly(this.getEmail())!=null))
{
exist=true;
userLogin=dao.validateEmailOnly(this.getEmail());
String pass = userLogin.getPassword();
String fullname = userLogin.getFullName();
String userEmail = userLogin.getLoginId();
to[0] = userEmail;
String from="admin@ideapoke.com";
String subject="Your User Id And Password !!!";
String message="Hi,"+fullname;

message+="\n Your password is "+pass+".";
message+="\n Please login to the web site with your password given.";
message+="\n \n Thanks";
message+="\n \n \n Regards";
System.out.println(message);
try{
(new SendMail()).sendMail(to, subject, message, from);
}
catch(Exception ex)
{
log.info(ex.getMessage());
}
status="success";
}
return status;
}
}




Explanation:

The above code represents a forgot password process whose objective is to mail the password information to the user’s registered mail box.
The log4j implementation starts with the initialization process which takes an argument of the class name as a parameter. The getLog() method is responsible for taking an input i.e ForgotPassword.class in my case. The statement responsible for sending mail is enclosed within a try catch block which we assume will be throwing an exception. The exception is caught and channelized to the log file with the help of log.info(message) method. Now we can verify the log inputs by cross checking the log file. We have some methods like debug(), warn(),trace() etc which we can make use according to situations. The screenshot for the log file result is given below.



These steps can be followed for implementing log4j in your web application. I think this will suffice your requirements and if not, just post comments regarding the difficulties you face, I will try to make things happen for you.
Looking forward to blog regarding Scheduler in my next session.

No comments:

Post a Comment