added logging via log4j2

This commit is contained in:
UnrealValentin
2021-05-10 16:09:36 +02:00
parent 4fc7db7516
commit a6cbf39485
5 changed files with 37 additions and 5 deletions

View File

@@ -19,6 +19,8 @@ repositories {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'org.apache.logging.log4j:log4j-api:2.14.1'
implementation 'org.apache.logging.log4j:log4j-core:2.14.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

View File

@@ -1,5 +1,7 @@
package org.hmcore;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hmcore.modules.Module;
import org.hmcore.modules.ModuleLoader;
import org.hmcore.modules.ModuleManager;
@@ -10,8 +12,12 @@ import java.util.HashMap;
public class HMCore {
public static final HashMap<String, Module> modules = new HashMap<>();
public static final Logger logger = LogManager.getLogger("HMCore");
public static void main(String[] args) {
ModuleManager.loadModules();
ModuleManager.initModules();
ModuleManager.hookModules();

View File

@@ -27,7 +27,7 @@ public class ModuleLoader {
}
private void loadModule(String path) throws IOException, ClassNotFoundException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
System.out.println("Loading module at: " + path);
HMCore.logger.debug("Loading module at: " + path);
JarFile jarFile = new JarFile(path);
JarEntry jarEntry = jarFile.getJarEntry("module.json");
String content = getClassPath(jarFile.getInputStream(jarEntry));
@@ -40,7 +40,7 @@ public class ModuleLoader {
Class<?> classToLoad = Class.forName(readable.getClassPath(), true, child);
Module instance = (Module) classToLoad.getDeclaredConstructor().newInstance();
HMCore.modules.put(instance.getName(), instance);
System.out.println("Loaded " + instance.getName() + " v" + readable.getVersion() + " by " + readable.getCreator());
HMCore.logger.info("Loaded " + instance.getName() + " v" + readable.getVersion() + " by " + readable.getCreator());
}
private String getClassPath(InputStream jarEntry) {

View File

@@ -47,8 +47,8 @@ public class ObjectInfoConfigHandler {
if(registryModule.objectAndInfoExist(data.objectInfoName, data.objectInfoChosen)) {
registryModule.forceObjectInfoForObject(data.objectInfoName, data.objectInfoChosen);
} else {
System.out.println("[!] Either Object Info " + data.objectInfoChosen + " doesn't exist for " + data.objectInfoName + " or " + data.objectInfoName + " doesn't exist!\n" +
"Please stop the Server, delete objetc-infos.json and let the server regenertate a new config [!]");
HMCore.logger.fatal("[!] Either Object Info " + data.objectInfoChosen + " doesn't exist for " + data.objectInfoName + " or " + data.objectInfoName + " doesn't exist!\n" +
"Please stop the Server, delete objetc-infos.json and let the server regenerate a new config [!]");
return false;
}
}
@@ -89,7 +89,7 @@ public class ObjectInfoConfigHandler {
try {
object = new GsonBuilder().create().fromJson(content, ObjectInfoConfig.class);
} catch (Exception e) {
System.out.println("[!] Object Info Config not valid!");
HMCore.logger.fatal("[!] Object Info Config not valid!");
return null;
}

View File

@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss} [%t | %-5level] %msg%n"/>
</Console>
<RollingRandomAccessFile name="File" fileName="logs/latest.log" filePattern="logs/%d{yyyy-MM-dd}-%i.log.gz">
<PatternLayout pattern="%d{HH:mm:ss} [%t | %-5level] %msg%n" />
<Policies>
<TimeBasedTriggeringPolicy />
<OnStartupTriggeringPolicy />
</Policies>
<DefaultRolloverStrategy max="1000"/>
</RollingRandomAccessFile>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="Console" level="debug"/>
<appender-ref ref="File" level="info"/>
</root>
</loggers>
</configuration>