Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove usage of fabric internals #145

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,97 +17,78 @@

package net.legacyfabric.fabric.impl.logger;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import net.fabricmc.loader.impl.util.log.Log;
import net.fabricmc.loader.impl.util.log.LogCategory;
import org.apache.logging.log4j.LogManager;

import net.legacyfabric.fabric.api.logger.v1.Logger;

public class LoggerImpl implements Logger {
public static final String API = "LegacyFabricAPI";
private LogCategory category;
private static final String SEPARATOR = "/";
private final org.apache.logging.log4j.Logger log;

public LoggerImpl(String context, String... subs) {
try { // Loader 0.14.3+
tryCreatingLogger(context, subs);
} catch (NoSuchMethodError e) { // Loader 0.13+
List<String> parts = new ArrayList<>();
parts.add(context);
Collections.addAll(parts, subs);

try {
this.category = LogCategory.class.getDeclaredConstructor(String[].class).newInstance(parts.toArray(new String[0]));
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
}
String sub = String.join(SEPARATOR, subs);

private void tryCreatingLogger(String context, String... subs) throws NoSuchMethodError {
this.category = LogCategory.createCustom(context, subs);
log = LogManager.getLogger("(" + context + (sub.isEmpty() ? SEPARATOR + sub : "") + ")");
}

public void info(String format) {
Log.info(this.category, format);
log.info(format);
}

public void info(String format, Object... args) {
Log.info(this.category, format, args);
log.info(format, args);
}

public void info(String format, Throwable exc) {
Log.info(this.category, format, exc);
log.info(format, exc);
}

public void error(String format) {
Log.error(this.category, format);
log.error(format);
}

public void error(String format, Object... args) {
Log.error(this.category, format, args);
log.error(format, args);
}

public void error(String format, Throwable exc) {
Log.error(this.category, format, exc);
log.error(format, exc);
}

public void warn(String format) {
Log.warn(this.category, format);
log.warn(format);
}

public void warn(String format, Object... args) {
Log.warn(this.category, format, args);
log.warn(format, args);
}

public void warn(String format, Throwable exc) {
Log.warn(this.category, format, exc);
log.warn(format, exc);
}

public void debug(String format) {
Log.debug(this.category, format);
log.debug(format);
}

public void debug(String format, Object... args) {
Log.debug(this.category, format, args);
log.debug(format, args);
}

public void debug(String format, Throwable exc) {
Log.debug(this.category, format, exc);
log.debug(format, exc);
}

public void trace(String format) {
Log.trace(this.category, format);
log.trace(format);
}

public void trace(String format, Object... args) {
Log.trace(this.category, format, args);
log.trace(format, args);
}

public void trace(String format, Throwable exc) {
Log.trace(this.category, format, exc);
log.trace(format, exc);
}
}