I recently inherited the J2EE Struts web application, which was written back in 2002. There is no logging in the application, except for the odd System.out.println ().
I added log4j logging so that I can write some information to the console, but I'm worried about how best to approach this. Any suggestions, tips and tricks will be welcome, as I don’t want to spend too much time adding logging to each method or class (or trying to find in which places it is better to keep a log, i.e. Block / code defects).
My current approach is to simply add a few protocols to the several classes that I was looking to understand the code, but are there a few key places where I can add logging to maximize my use of adding log4j?
Edit:
Update progress:
I expanded Struts (v.1) ExceptionHandlerand configured struts-config.xmlto use mine CustomExceptionHandlerinstead of the Struts version. Then, overriding the method execute(), I added some logic to register the exception with log4j. See below:
public class CustomExceptionHandler extends ExceptionHandler {
static Logger logger = Logger.getLogger (CustomExceptionHandler.class);
public ActionForward execute (Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException {
logException (ex);
return super.execute (ex, ae, mapping, formInstance, request, response);
}
private void logException (Throwable thr) {
// Add code here to log the exception appropiately
// ie logger.error ("Exception is:" + ex.getMessage ());
}
You must also update the struts-config.xml file:
<global-exceptions>
<exception key="" type="java.lang.Throwable"
handler="com.mycompany.CustomExceptionHandler" />
</global-exceptions>
Now I can be sure that I will always correctly record any exceptions.
With the exception of the exceptions that were eaten, I wrap them in unchecked exceptions so that they get to the top:
} catch (Exception e) {
// nothing here
}
Changed to:
} catch (Exception e) {
throw new RuntimeException (e);
}