Skip to content

Commit

Permalink
Fixed Logger Util
Browse files Browse the repository at this point in the history
  • Loading branch information
XDPXI committed Dec 28, 2024
1 parent 862a161 commit 6cfa287
Showing 1 changed file with 13 additions and 66 deletions.
79 changes: 13 additions & 66 deletions common/src/main/java/dev/xdpxi/xdlib/util/Logger.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,29 @@
package dev.xdpxi.xdlib.util;

import org.apache.logging.log4j.LogManager;
import dev.xdpxi.xdlib.Constants;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* A utility class for logging various log levels using Log4j.
* Provides methods for logging messages at different severity levels: debug, info, warn, and error.
*/
public class Logger {
/**
* A map to hold loggers for each package name.
*/
private static final Map<String, org.apache.logging.log4j.Logger> PACKAGE_LOGGERS = new ConcurrentHashMap<>();
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(Constants.MOD_ID);

/**
* The logger instance used for logging messages.
* This is initialized with an empty string as the logger name.
*/
public static org.apache.logging.log4j.Logger LOGGER = LogManager.getLogger("default");

/**
* Sets up the logger for a specific package with a custom modID.
* This method should be called before any log messages are logged to initialize the logger for the package.
*
* @param packageName The name of the package.
* @param modID The mod ID to be used as the logger name.
*/
public static void setup(String packageName, String modID) {
PACKAGE_LOGGERS.put(packageName, LogManager.getLogger(modID));
}

/**
* Gets the logger for the calling class's package.
* If no specific logger is set up, a default logger for the root package is returned.
*
* @return The logger instance for the calling class's package.
*/
private static Logger getLogger() {
String callingPackage = new Throwable().getStackTrace()[2].getClassName();
String packageName = callingPackage.substring(0, callingPackage.lastIndexOf('.'));
return (Logger) PACKAGE_LOGGERS.getOrDefault(packageName, LogManager.getLogger("default"));
public static void debug(String message, Object... args) {
LOGGER.debug(message, args);
}

/**
* Logs a message at the DEBUG level.
*
* @param log The message to be logged.
*/
public static void debug(String log) {
LOGGER.debug(log);
public static void info(String message, Object... args) {
LOGGER.info(message, args);
}

/**
* Logs a message at the INFO level.
*
* @param log The message to be logged.
*/
public static void info(String log) {
LOGGER.info(log);
public static void warn(String message, Object... args) {
LOGGER.warn(message, args);
}

/**
* Logs a message at the WARN level.
*
* @param log The message to be logged.
*/
public static void warn(String log) {
LOGGER.warn(log);
public static void error(String message, Object... args) {
LOGGER.error(message, args);
}

/**
* Logs a message at the ERROR level.
*
* @param log The message to be logged.
*/
public static void error(String log) {
LOGGER.error(log);
public static void error(String message, Throwable throwable) {
LOGGER.error(message, throwable);
}
}

0 comments on commit 6cfa287

Please sign in to comment.