简体   繁体   English

如何在多个Java项目中使用同一记录器

[英]How to use the same logger in multiple java projects

I am looking for a solution to the following system setup. 我正在寻找以下系统设置的解决方案。

We have a Java NetBeans project that handle the Oracle database with all the entities and the required functionality for a database layer (we cannot replace it with hibernate). 我们有一个Java NetBeans项目,该项目处理具有所有实体的Oracle数据库以及数据库层所需的功能(我们无法将其替换为休眠)。 This project is used by other Java NetBeans projects in order to read and write to the database. 其他Java NetBeans项目使用此项目来读取和写入数据库。

I would like to be able to set up the Logger (we use the standard java.util.logging.Logger) in the DB project to depend from the one of the application using it. 我希望能够在DB项目中设置Logger(我们使用标准的java.util.logging.Logger),以依赖于使用它的应用程序之一。 This is required so that all my logging is in a single file and it makes sense to read it; 这是必需的,这样我的所有日​​志记录都在一个文件中,并且可以读取它; the chain of events is impossible to understand in a log split in multiple files. 在多个文件的日志拆分中无法理解事件链。

I have a simple solution, that I do not like, which is to inject the logger in every class of my db project. 我有一个我不喜欢的简单解决方案,它是将logger注入到我的db项目的每个类中。 This makes all my constructors more complex and I will need to modify a lot of code if I was to use it for every entity (it is useful to log what the db layer is actually doing). 这使我所有的构造函数都更加复杂,如果要对每个实体使用它,我将需要修改很多代码(记录db层实际在做什么很有用)。 I would like a solution where I pass a simple parameter to the whole db project so that all my usual static final logger could write in the right place. 我想要一个将简单参数传递给整个数据库项目的解决方案,以便所有我惯用的静态最终记录器都可以在正确的位置编写。

Here is an example just to clarify what I would like to get 这是一个例子,只是为了阐明我想要得到的

In the Db Project: 在Db项目中:

public class Table{
  private final static Logger logger = Logger.XXXXXXX; <--the method I need to accomplish my goal
}

In the application Project 在应用项目中

public class TableInteractor{
  private Table table;
  private static final Logger logger = Logger.getLogger("MyApplication");
}

The solution I mentioned would need something like this 我提到的解决方案需要这样的东西

public class Table{
  private final static Logger logger;
  public Table(Logger logger){
    this.logger = logger;
  }     
}

and

public class TableInteractor{
  private Table table;
  private static final Logger logger = Logger.getLogger("MyApplication");
  ...
  table = new Table(logger);
}

Is there any way to pass "MyApplication" to the Db Project so that the Logger in there are instantiated in the same way as those in the application? 有什么方法可以将“ MyApplication”传递给Db项目,以便以与应用程序中相同的方式实例化其中的Logger? If you need more information on my setup in order to give me an answer I can add more information. 如果您需要有关我的设置的更多信息以便给我答复,我可以添加更多信息。

EDIT: 编辑:

I just noticed that a logger has a method called setParent(Logger l) If I create a logger in the DB project, can I pass the application logger into the DB layer and set it as the parent of the DB logger? 我只是注意到一个记录器有一个名为setParent(Logger l)的方法,如果我在DB项目中创建一个记录器,是否可以将应用程序记录器传递到DB层并将其设置为DB记录器的父级?

As an alternative I was thinking of passing the file handler of the application logger into the db logger and use it so that the same logging file is used by both projects. 作为替代方案,我正在考虑将应用程序记录器的文件处理程序传递到db记录器中并使用它,以便两个项目都使用相同的记录文件。

Any suggestion on which one is the best solution? 关于哪个是最佳解决方案的任何建议?

Common pattern is to use one logger per class using class name when calling their factory method. 常见的模式是在调用其工厂方法时,使用类名称为每个类使用一个记录器。 That creates a hierarchy of loggers and you can customize what logger logs at what level and what handler is used to process log records. 这样就创建了一个记录器层次结构,您可以自定义哪个记录器日志处于哪个级别以及使用哪个处理程序来处理日志记录。 Of course it is possible to send output form more loggers into one file. 当然,可以将更多记录器的输出发送到一个文件中。 Most of that is of course written in Java logging overview - http://docs.oracle.com/javase/6/docs/technotes/guides/logging/overview.html 当然,大多数内容都是用Java日志记录概述编写的-http: //docs.oracle.com/javase/6/docs/technotes/guides/logging/overview.html

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

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