-
Notifications
You must be signed in to change notification settings - Fork 0
Using the "LoggerFactory"
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.
/**
* 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;
}
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"));
}
}
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.
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;
}
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();
}
RemasteredLogger is a logger as simple as it is complex, it uses SLF4j as an API base. This logger provides the developer with ease of use for a wide variety of utility. This also allows the simplicity of saving under several loggers but also the complexity of being able to create “Class” or single event loggers.
Class | Version | Security |
---|---|---|
Logger | ||
ILogger | ||
LoggerFactory | ||
ILoggerFactory |
// https://mvnrepository.com/artifact/codes.wesley-dev/remasteredlogger
implementation group: 'codes.wesley-dev', name: 'remasteredlogger', version: '1.1.4'
Jetbrains Annotations
SLF4j
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha1'
// https://mvnrepository.com/artifact/org.jetbrains/annotations
implementation group: 'org.jetbrains', name: 'annotations', version: '20.1.0'
Version | Java Compatibility |
---|---|
1.1.4 | Java 8 and Upper |
1.1.3 | Java 9 and Upper |
<= 1.1.2 | ❌ (Security and Compatibility) |
Wesley Levasseur — Creator, Initial Work - KanekiReal