The logger package in NDCore provides enhanced logging capabilities beyond what is available in Bukkit's standard logging system. It offers multiple logging levels, customizable formatting, and both console and file output options.
The primary purpose of the logger package is to:
- Provide more granular logging levels (INFO, WARN, ERROR, FATAL)
- Enable color-coded console output for better readability
- Support file logging with automatic rotation
- Allow for silent logging (file-only) for verbose information
- Provide a consistent logging interface across plugins
The logger package includes the following key classes:
- NDLogger - The main logging utility that provides different logging levels with customizable format and behavior
The logger package also contains a strategy subpackage that defines the logging strategy interface and implementations.
Here's an example of how to use the NDLogger in your plugin:
import dev.nelmin.ndcore.logger.NDLogger;
public class MyPlugin extends NDPlugin {
private NDLogger logger;
@Override
public void enable() {
// Initialize logger with your plugin's name
this.logger = new NDLogger("MyPlugin", this);
// Basic logging
logger.info("Plugin initialized successfully");
// Logging with multiple parameters
logger.info("Player", player.getName(), "joined the game");
// Warning messages
logger.warn("Low memory detected:", Runtime.getRuntime().freeMemory() / 1024 / 1024, "MB available");
// Error logging
try {
// Some risky operation
loadConfiguration();
} catch (Exception e) {
logger.error("Failed to load configuration:", e.getMessage());
e.printStackTrace();
}
// Fatal errors (critical issues that might require shutdown)
if (!databaseConnected) {
logger.fatal("Database connection failed - plugin cannot function!");
getServer().getPluginManager().disablePlugin(this);
}
// Silent logging (writes to file but not console)
logger.infoSilent("This message only goes to the log file");
}
}When extending NDPlugin, you get access to a logger instance through the logger() method:
public class MyPlugin extends NDPlugin {
@Override
public void enable() {
// Use the logger provided by NDPlugin
logger().info("Plugin enabled!");
// Log with multiple parameters
logger().info("Server version:", getServer().getVersion());
}
@Override
public void disable() {
logger().info("Plugin disabled!");
}
}NDLogger provides the following log levels:
- INFO - General information messages
- WARN - Warning messages for potential issues
- ERROR - Error messages for recoverable errors
- FATAL - Critical error messages that prevent functionality
Each level has both a standard and silent variant (e.g., info() and infoSilent()).
- Use the appropriate log level for different types of messages
- Include relevant context in log messages
- Use silent logging for verbose information that should be recorded but not displayed
- Create a dedicated logger for each major component of your plugin
- Take advantage of the multi-parameter logging methods for cleaner code
- Consider using custom logging strategies for specialized logging needs