The Kat's Work - Blog
Main | Blog | Registration | Login
Thursday
2024-03-28
2:55 PM
Welcome Guest | RSS
Main » 2010 » November » 30

I recently implemented error logging in a Java Struts application. The benefits of this are obvious but as this was an internal application, the customer has always accepted the error messages. Recently however its become harder to problem solve as users aren't capturing displayed stack traces correctly. SO to that end i implemented the code below to allow for the programatic logging of errors. The user is presented with a styled "Whoops" page and asked to try again.

The basic functionality is that the errorlog logs the time adn stack trace of the error. Also it logs the user (as the system requires login.) , the action they were performing and the parameters they(or the system) had specified.

In a later blog post i will adapt this to Java Spring.

Database structure.

Id    PK, int, not null
Error_time    Datetime, not null
Error_by    Int, null (FK to user table)
Exception_report    Text, not null
Action    Varchar(50), not null
parameters    Varchar(200), null)


import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.sql.Timestamp;

import com.project.objects.User;

public class ErrorLog {
    private Integer id;
    private Timestamp errorTime;
    private User errorBy;
    private String exceptionReport;
    private String action;
    private String parameters;
   
   
    /**
     * @return the id
     */
    public Integer getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(Integer id) {
        this.id = id;
    }
    /**
     * @return the errorTime
     */
    public Timestamp getErrorTime() {
        return errorTime;
    }
    /**
     * @param errorTime the errorTime to set
     */
    public void setErrorTime(Timestamp errorTime) {
        this.errorTime = errorTime;
    }
    /**
     * @return the errorBy
     */
    public User getErrorBy() {
        return errorBy;
    }
    /**
     * @param errorBy the errorBy to set
     */
    public void setErrorBy(User errorBy) {
        this.errorBy = errorBy;
    }
    /**
     * @return the exceptionReport
     */
    public String getExceptionReport() {
        return exceptionReport;
    }
    /**
     * @param exceptionReport the exceptionReport to set
     */
    public void setExceptionReport(String exceptionReport) {
        this.exceptionReport = exceptionReport;
    }
    /**
     * @return the action
     */
    public String getAction() {
        return action;
    }
    /**
     * @param action the action to set
     */
    public void setAction(String action) {
        this.action = action;
    }
    /**
     * @return the parameters
     */
    public String getParameters() {
        return parameters;
    }
    /**
     * @param parameters the parameters to set
     */
    public void setParameters(String parameters) {
        this.parameters = parameters;
    }

   
     public void fillMessage(Throwable aThrowable) {
            final Writer result = new StringWriter();
            final PrintWriter printWriter = new
PrintWriter(result);
            aThrowable.printStackTrace(printWriter);
            this.setExceptionReport(result.toString());
     }

}

<hibernate-mapping>
<class name="com.projects.objects.errorLog.ErrorLog" table="ErrorLog">
  <id column="ID" name="id" type="int">
   <generator class="native"/>
  </id>
 <many-to-one class="com.project.objects.User"
   column="error_by" name="errorBy" />
    
    <property column="error_time" generated="never" lazy="false"
   name="errorTime" type="java.sql.Timestamp"/>
  <property column="exception_report" generated="never" lazy="false"
   name="exceptionReport" type="java.lang.String"/> 
 <property column="action" generated="never" lazy="false"
   name="action" type="java.lang.String"/> 
   <property column="parameters" generated="never" lazy="false"
   name="parameters" type="java.lang.String"/> 
</class>

</hibernate-mapping>

import java.util.List;

import com.project.objects.errorLog.ErrorLog;

public interface ErrorLogDao {

    public Integer saveObject(ErrorLog errorLog);
       
    public List<ErrorLog> getAllErrorLogs();
   
    public List<ErrorLog> getErrorLog(Integer id);
   

   
}

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.project.hibernate.HibernateUtil;
import com.project.dao.ErrorLogDao;
import com.project.objects.errorLog.ErrorLog;

public class ErrorLogDaoHibernate implements ErrorLogDao{
    /**
     *
     */
    public ErrorLogDaoHibernate() {
        session = HibernateUtil.getSessionFactory().getCurrentSession();
    }

    public Session session = null;
    public Transaction tx = null;
    public String hql = null;
    /* (non-Javadoc)
     * @see com.sthelens.pmf.dao.ErrorLogDao#getAllErrorLogs()
     */
    public List<ErrorLog> getAllErrorLogs() {
        // TODO Auto-generated method stub
        return null;
    }

    /* (non-Javadoc)
     * @see com.sthelens.pmf.dao.ErrorLogDao#getErrorLog(java.lang.Integer)
     */
    public List<ErrorLog> getErrorLog(Integer id) {
    Criterion objectId = Restrictions.eq("ErrorLog.id", id);

       
        Criteria crit_a = session.createCriteria(
                ErrorLog.class, "ErrorLog").setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).add(objectId);
       
   
        //System.out.println("size "+crit_a.list().size());
   
        return crit_a.list();
    }

    /* (non-Javadoc)
     * @see com.sthelens.pmf.dao.ErrorLogDao#saveObject(com.sthelens.pmf.objects.errorLog.ErrorLog)
     */
    public Integer saveObject(ErrorLog errorLog) {
        Integer genID = (Integer)session.save( errorLog);
       
       
        return genID ;
    }

}

import com.project.objects.user;
import com.project.objects.errorLog.ErrorLog;

public interface ErrorLogService {

    public Integer createErrorLog(String action, Throwable exception, User off, String parameters);
   
   
    public ErrorLog getErrorLog(Integer id);
}

import java.util.List;

import com.project.dao.ErrorLogDao;
import com.project.dao.hibernate.ErrorLogDaoHibernate;
import com.project.objects.Officers;
import com.project.objects.errorLog.ErrorLog;
import com.project.service.ErrorLogService;

public class ErrorLogServiceImpl implements ErrorLogService{
/* (non-Javadoc)
     * @see com.sthelens.pmf.service.ErrorLogService#getErrorLog(java.lang.Integer)
     */
    public ErrorLog getErrorLog(Integer id) {
        List<ErrorLog> errLogs = this.errorLogDao.getErrorLog(id);
        if(errLogs.size() >0){
            return errLogs.get(0);
        }
        return null;
    }


/**
     *
     */
    public ErrorLogServiceImpl() {
        errorLogDao = new ErrorLogDaoHibernate();
    }


private ErrorLogDao errorLogDao;


    /* (non-Javadoc)
     * @see com.sthelens.pmf.service.ErrorLogService#createErrorLog(java.lang.String, java.lang.String, com.sthelens.pmf.objects.Officers, java.lang.String)
     */
    public Integer createErrorLog(String action, Throwable exception,
            User off, String parameters) {
        ErrorLog errorLog = new ErrorLog();
        errorLog.setAction(action);
        errorLog.fillMessage(exception);
        errorLog.setErrorBy(off);
        java.util.Date today =
            new java.util.Date();
        java.sql.Timestamp sqlToday =
       new java.sql.Timestamp(today.getTime());
        errorLog.setErrorTime(sqlToday);
       
        errorLog.setParameters(parameters);
       
       
        return this.errorLogDao.saveObject(errorLog);
    }

   
}


Usage
   
}catch (Exception e){
            ErrorLogService errorLogService = new ErrorLogServiceImpl();
       
           
            response.sendRedirect("./errorPage.do?id=" +
                    errorLogService.createErrorLog(this.getClass().getCanonicalName(),
                            e,
                            (Officers)request.getSession().getAttribute("LogIn"),
                            request.getQueryString()));
            return null;
    }
Views: 772 | Added by: The_Kat | Date: 2010-11-30 | Comments (0)

Login form
Adverts
Search
Calendar
«  November 2010  »
SuMoTuWeThFrSa
 123456
78910111213
14151617181920
21222324252627
282930
Entries archive
Site friends
  • Create your own site
  • Spree4.com
  • My Blog
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    spree4
    Copyright MyCorp © 2024
    Website builderuCoz