Skip to content

Using the "LoggerFactory"

Wesley Levasseur edited this page May 7, 2021 · 3 revisions

Definition

LoggerFactory consists in managing all ILogger then managing the save.

This class is a default LoggerFactory if the user does not want to create one himself by implementing ILoggerFactory.

Use

Recall

/**
 * This constructor allows you to create a LoggerFactory.
 *
 * @param dir The folder where the message recording files will be saved.
 */
public LoggerFactory(@NotNull final File dir) throws InternalError {
    if (!dir.exists())
        if (!dir.mkdir())
            throw new InternalError("The Logger folder couldn't be created.\nThis could be due to a lack of permission or inaccurate path.");
    this.logsDir = dir;
}

Where ?

Mainly you are only going to have one LoggerFactory most of the time. So it makes sense to initialize it in your main class. As below.

public class MainClass {

    private final LoggerFactory loggerFactory;

    public LoggerFactory getLoggerFactory() {
        return this.loggerFactory;
    }

    public MainClass() {
        this.loggerFactory = new LoggerFactory(new File("path/to/logsDir"));
    }
}

One? Not several?

As said above, in the main cases it will not be necessary for you to use several LoggerFactory. You could totally create several in a case where you want to do folder "dispatching" according to your wishes, RemasteredLogger is only here to facilitate the task and leaves free to your imagination.

The Loggers?

final ILogger logger1 = this.loggerFactory.getLogger("Logger1");
final Logger logger1_ = (Logger) logger1;
final Logger mainClassLogger = (Logger) this.loggerFactory.getLogger(this.getClass());
final ILogger mainClassLogger_ = mainClassLogger;
final Logger _mainClassLogger = (Logger) this.loggerFactory.getLogger(MainClass.class);
final ILogger _mainClassLogger_ = _mainClassLogger;

Here is an example of proceeding above.

You should know that to retrieve a Logger where to create one is done from an ILoggerFactory to directly attach the Logger to the ILoggerFactory thus allowing the management of logs.

But you can also create a Logger but you will notice that it will obligatorily ask for an ILoggerFactory in parameter.

/**
 * Ce constructeur permet de créer un Logger. Il est généralement créer depuis le Factory lui-même.
 *
 * @param name          Le nom du Logger.
 * @param loggerFactory Le Factory du Logger.
 */
public Logger(String name, ILoggerFactory loggerFactory) {
    this.name = name;
    this.loggerFactory = loggerFactory;
}

The backup

For most users you will just use save() but there are for people who want to customize certain things, several other methods that you could use in the following way.

try {
    this.loggerFactory.save();
} catch (IOException e) {
    e.printStackTrace();
}
List<String> othersLogs = new ArrayList<>();
try {
    this.loggerFactory.save(othersLogs);
} catch (IOException e) {
    e.printStackTrace();
}
List<String> othersLogs2 = new ArrayList<>();
List<String> othersLogs3 = new ArrayList<>();
List<String> othersLogs4 = new ArrayList<>();
try {
    this.loggerFactory.save((List<String>[]) Arrays.asList(othersLogs2, othersLogs3, othersLogs4).toArray());
} catch (IOException e) {
    e.printStackTrace();
}