title: struts - logging for each layer (Action, DAO)
body: <p>I am new to struts and I would like to understand how do I handle logging for each layer(JSP/ActionForm, Action, DAO) of the application. </p>
  
  <p>I notice as time goes by the error log file maybe too cluttered and unfriendly to view. How can I improve on the logging patterns to separate the errors thrown by each layer of the application?</p>
  
  <p>and I couldnt find a way to handle exceptions encounter on jsp page as well. </p>
  
  <p>Code Template for Action</p>
  
  <pre><code>public ActionForward perform(ActionMapping mapping, 
                                ActionForm form, 
                                HttpServletRequest request, 
                                HttpServletResponse response) 
     throws IOException, ServletException { 
  
     try {
           // calls the DAO
     }
     catch (CustomDAOException e) {
       log.error(e);
       // inform application there is an error
     }
     catch (Exception e) {
       log.error(e)
       // inform application there is an error
     }
  }
  </code></pre>
  
  <p>Code Template for DAO</p>
  
  <pre><code>public void update() throw CustomDAOException {
  try {
      // Prepare a statement to insert a record
      String sql = "INSERT INTO my_table (col_string) VALUES(?)";
      PreparedStatement pstmt = connection.prepareStatement(sql);
  
      // Insert 10 rows
      for (int i=0; i&lt;10; i++) {
          // Set the value
          pstmt.setString(1, "row "+i);
  
          // Insert the row
          pstmt.executeUpdate();
      }
  } catch (SQLException e) {
      throw new CustomDAOException(e);
  }
  </code></pre>