Transferring a registrar between classes

I have two classes in my project that I would like to pass a logger from the main class to a subclass and have a subclass for using the parameters of the parent registrar.

A subclass is a separate general class (i.e. not tied to the main class) and should be able to create its own journal if it is not provided.

The main class creates a logger, adds a console handler, a file handler and a log formatter, and I would like the subclass to temporarily override the log formatter for log messages, and then when the main class resumes, return the log formatter to it.

I tried to bring the logger to a subclass or create a new one if necessary, but I get a few messages on the screen and in the log file (it seems this is adding handlers to the main class, than overloading it).

How should I do it?

+5
source share
2 answers

I assume that you are using the java.util.logging package. In this case, when and where you use Logger.getLogger (someString), you will get the same Logger instance.

For example: if the main class uses Logger log = Logger.getLogger ("myLogger");, you can get the same logger in any other class using Logger.getLogger ("myLogger");

+11
source

This is what I have so far:

public void setLogger(String loggerName) {
    MySubClass.logger = Logger.getLogger(loggerName);

    for (Handler handler : logger.getHandlers()) {
        handler.setFormatter(tmdbFormatter);
    }
    logger.setUseParentHandlers(true);
    logger.setLevel(Level.ALL);
}
0
source

All Articles