0

经过多次尝试,我终于能够提出一个问题。我希望它有意义并且易于理解。在使用 Spring 和 Jersey 的 Web 应用程序上工作。我需要实现一个应用程序范围的记录器,该记录器应该将应用程序上执行的所有活动记录到数据库中。目前,我已经使用了该HAS-A实现,并在执行 CRUD 操作的任何地方调用了日志记录方法。像这样的东西:

LogBean lBean=new LogBean("rickesh@email.com","update","address","127.0.0.1");
logToDatabase(lBean);

但这会导致大量重复的代码行,并且我必须在执行 CRUD 操作的每个部分反复实例化和调用 log 方法。有什么方法可以从控制器层、REST 层中提取日志记录吗?Spring 或 Jersey 中是否有任何特定功能,我可以使用这些功能在单独的层上执行日志记录,而不必在任何地方重复相同的代码行。请指教。

4

2 回答 2

6

如果您在编写日志时变得一团糟,您应该查看AOP。如果您已经在使用 spring,您应该阅读Spring AOP,对于日志记录本身,请阅读使用 Spring AOP 进行日志记录

于 2013-02-08T13:58:11.027 回答
1

AOP is a better way but if getting to learn it to resolve this issue is going to take longer then here is a simple alternative.

  1. Add log4j JAR to your WebApp. Make sure, log4j version bundled in your WebApp doesn't conflict with the version already with your Application Server
  2. Place a log4j.xml for FILE and CONSOLE appendars in your class path (src/main/resources)
  3. There is a Log4JConfigureListener provided by Spring which you can declare under section in your web.xml
  4. Additionally, if too much log is generated, you can limit the output of the loggers via either log4j.xml in your application to certain packages only or via using Root logger configuration for your project specific packages. For example, in JBoss you can add a "category" for a package at a specific LOG level

Your specific "separate layer" should have a separate and distinct package name to target the log appendars for that layer Hope this helps!

于 2013-02-08T15:58:56.737 回答