简体   繁体   中英

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). This project is used by other Java NetBeans projects in order to read and write to the database.

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. 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. 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). 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:

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? 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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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