I have recently inherited a J2EE Struts web app that was written back in 2002. There is no logging in the application other than the odd System.out.println().
I have added log4j logging so that I can write out some info to the console, but I'm concerned on how best to approach this. Any suggestions, tips, best practices would be welcome as I don't want to spend too much time adding logging to every method or class (or trying to find what places where logging would be best - ie. bad code blocks/defects).
My current approach is to just add some logging to the few classes that I have been looking at to understand the code, but are there a few key places where I can add logging to maximize my use of adding log4j?
Edit:
Progress update:
I extended the Struts (v.1) ExceptionHandler
and configured the struts-config.xml
to use my CustomExceptionHandler
instead of the Struts version. Then by overriding the execute()
method, I added some logic to log the exception using 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 // i.e. logger.error("Exception is: " + ex.getMessage()); }
The struts-config.xml file had to be updated as well:
<global-exceptions>
<exception key="" type="java.lang.Throwable"
handler="com.mycompany.CustomExceptionHandler" />
</global-exceptions>
Now I can be sure that I will always appropiately log any exceptions.
For the exceptions that were getting eaten, I wrap them in unchecked exceptions so that they make it to the top:
} catch (Exception e) { //nothing here }
Changed to:
} catch (Exception e) { throw new RuntimeException(e); }