Skip to content

Commit

Permalink
Abstracted logger to fall back to native if Log4J isn't present
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePixelbrain committed Jul 30, 2023
1 parent c1805b9 commit 5e3315c
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id "com.github.johnrengelman.shadow" version "8.1.1" apply false
}

subprojects {
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apply plugin: 'java-library'
apply plugin: 'com.github.johnrengelman.shadow'

dependencies {
implementation 'com.google.code.gson:gson:2.10.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import com.google.gson.Gson;
import io.dogboy.serializationisbad.core.config.SIBConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import io.dogboy.serializationisbad.core.logger.ILogger;
import io.dogboy.serializationisbad.core.logger.Log4JLogger;
import io.dogboy.serializationisbad.core.logger.NativeLogger;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

public class SerializationIsBad {
public static final Logger logger = LogManager.getLogger(SerializationIsBad.class);
public static final ILogger logger = initLogger("SerializationIsBad");
private static SerializationIsBad instance;
private static boolean agentActive = false;

Expand All @@ -35,6 +36,18 @@ public static void init(File minecraftDir) {
SerializationIsBad.instance = new SerializationIsBad(minecraftDir);
}

private static ILogger initLogger(String name) {
try {
// Check if needed Log4J classes are available
Class.forName("org.apache.logging.log4j.LogManager");
Class.forName("org.apache.logging.log4j.Logger");
return new Log4JLogger(name);
} catch (ClassNotFoundException e) {
// Fallback to Java native logger
return new NativeLogger(name);
}
}

private final SIBConfig config;

private SerializationIsBad(File minecraftDir) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.dogboy.serializationisbad.core.logger;

public interface ILogger {
void debug(String message);
void info(String message);
void warn(String message);
void error(String message, Throwable throwable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package io.dogboy.serializationisbad.core.logger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4JLogger implements ILogger {
private final Logger logger;

public Log4JLogger(String name) {
this.logger = LogManager.getLogger(name);
}
@Override
public void debug(String message) {
logger.debug(message);
}

@Override
public void info(String message) {
logger.info(message);
}

@Override
public void warn(String message) {
logger.warn(message);
}

@Override
public void error(String message, Throwable throwable) {
logger.error(message, throwable);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.dogboy.serializationisbad.core.logger;

import java.util.logging.Logger;

public class NativeLogger implements ILogger {
private final Logger logger;

public NativeLogger(String name) {
this.logger = Logger.getLogger(name);
}

@Override
public void debug(String message) {
logger.finest(message);
}

@Override
public void info(String message) {
logger.info(message);
}

@Override
public void warn(String message) {
logger.warning(message);
}

@Override
public void error(String message, Throwable throwable) {
logger.severe(message);
throwable.printStackTrace();
}
}

0 comments on commit 5e3315c

Please sign in to comment.