-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
13 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |