简体   繁体   English

如何将log4j日志记录实现到现有的J2EE Struts Web应用程序?

[英]How can I implement log4j logging to an existing J2EE Struts web application?

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(). 我最近继承了2002年编写的J2EE Struts Web应用程序。除了奇怪的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. 我已经添加了log4j日志记录,以便我可以向控制台写出一些信息,但我担心如何最好地解决这个问题。 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? 我目前的方法是只添加一些日志记录到我一直在寻找的几个类来理解代码,但是有几个关键的地方我可以添加日志记录以最大化我添加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. 我扩展了Struts(v.1) ExceptionHandler ,并将struts-config.xml配置为使用我的CustomExceptionHandler而不是Struts版本。 Then by overriding the execute() method, I added some logic to log the exception using log4j . 然后通过重写execute()方法,我添加了一些逻辑来使用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: struts-config.xml文件也必须更新:

<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);
}

The most important thing is, I would set up a Struts exception handler in the struts-config.xml to catch everything that was thrown from the actions and log it. 最重要的是,我将在struts-config.xml中设置一个Struts异常处理程序来捕获从操作中抛出的所有内容并将其记录下来。 Then I'd check all the exception handling to see what exceptions are getting eaten, written to stdout, or otherwise not making it into the log, and make changes so that every exception that can't be usefully handled gets propagated (wrapped in unchecked exceptions if necessary) so that it makes it to the exception handler. 然后我会检查所有的异常处理,看看有什么异常被吃掉,写入stdout,或者没有进入日志,并进行更改,以便每个无法有效处理的异常都被传播(包装在未经检查中)必要时的异常),以便它成为异常处理程序。

Besides that, I'd do like you describe, adding logging to the parts where it is directly helpful. 除此之外,我会像你描述的那样,将记录添加到直接有用的部分。 As long as exceptions are getting thrown and making their way to the exception handler there shouldn't be much more to do. 只要异常被抛出并进入异常处理程序,就不应该做更多的事情。

If the only thing that is going to System.out (System.err) is the old logging and you are looking for a simple solution, you could always redirect System.out to a wrapper class around a logger in the startup of your app's lifecycle: 如果进入System.out(System.err)的唯一事情是旧的日志记录并且您正在寻找一个简单的解决方案,您可以始终将System.out重定向到应用程序生命周期启动时围绕记录器的包装类:

public class LoggerStream extends PrintStream {
  Logger legacyLogger = Logger.getLogger("app.legacyLogger");

  ...

  public void println(String s){
    legacyLogger.log(s);
  }
}

overriding any other methods like print, etc. It might not generate the prettiest output, but you could use it as a "quick-fix" start and then replace the legacy logging with more appropriate logging as you find items of greater interest. 覆盖任何其他方法,如print等。它可能不会生成最漂亮的输出,但您可以将其用作“快速修复”启动,然后在找到更感兴趣的项目时用更合适的日志记录替换旧版日志记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM