Custom Levels (Logging) - MeAlam1/BlueLib GitHub Wiki


Custom Levels

Overview

The BaseLogLevel class in BlueLib provides a set of custom log levels that extend the standard Java Level class. This allows developers to implement tailored log messages for specific scenarios, enhancing readability and organization within both library and mod contexts.

BlueLib introduces the following custom levels:

  • INFO: Standard informational messages.
  • ERROR: Indicates error messages.
  • WARNING: Used for warning messages.
  • SUCCESS: Custom level for marking successful operations.
  • BLUELIB: Reserved for internal BlueLib development logs (intended only for BlueLib library logs, not for mod usage).

Note: Mod developers are encouraged to use all levels except BLUELIB, which is intended specifically for internal BlueLib logging.

Getting Started

Adding Custom Log Levels

To add a new custom log level in your project:

  1. Extend the BaseLogLevel Class: Since the BaseLogLevel class is part of the BlueLib library and cannot be modified directly, you should create your own subclass. This allows you to add new log levels while keeping the original class intact.

    public class CustomLogLevel extends BaseLogLevel {
        public static final Level CUSTOM = new Level("CUSTOM_LEVEL", Level.INFO.intValue() + 100) {};
    }
    
  2. Use Your Custom Level: After defining your custom log level in the subclass, you can log messages at this level using BaseLogger.log:

    BaseLogger.log(CustomLogLevel.CUSTOM, "This is a custom log message.");
    
  3. Assigning Colors: If you want to assign a specific ANSI color to your custom log level, implement the software.bluelib.interfaces.logging.ILogColorProvider interface and override the getColor method. This enables you to customize the appearance of log messages with custom colors.

    public class CustomColorProvider implements ILogColorProvider {
        @Override
        public String getColor(Level pLevel) {
            if (pLevel.equals(CustomLogLevel.CUSTOM)) {
                return "\u001B[32m"; // Example ANSI color code for green
            }
            return "\u001B[0m"; // Default color
        }
    }
    

    Note: If ILogColorProvider is not implemented, logs will display in the default color \u001B[0m.

  4. Custom Level Values: Ensure that your custom levels are assigned distinct integer values to prevent conflicts with existing log levels in BaseLogLevel. For example, if using BlueLib, SUCCESS and BLUELIB are already set to Level.INFO.intValue() + 50, so avoid reusing that value.

Key Points

  • Custom Logging Levels: Extend logging levels to categorize specific log types, allowing better differentiation and filtering.
  • Color Configuration: Use ILogColorProvider to assign custom ANSI colors for each log level for added visual clarity.
  • Reserved Levels: BLUELIB is designed specifically for BlueLib library logs and is not recommended for mod usage.
  • Flexible Configuration: You can create as many custom levels as needed by extending BaseLogLevel, assigning unique names and priority values for clarity.

This setup offers a robust and customizable logging system for BlueLib, providing clarity and consistency across both library and mod development contexts.

For further information and advanced customization options, refer to the official Discord community where you can seek help, share experiences, and learn from other developers.