The PajamaLogger class provides a lightweight logging mechanism for the Pajama framework. It is used to log information, warnings, and errors to the console during the application lifecycle.
- Console Logging: Logs messages with consistent formatting.
- Ease of Use: Simplified logging interface suitable for small-scale applications.
Logs a message to the console.
message: The message to be logged.
PajamaLogger.log("Application started successfully.");PajamaLogger.error("Configuration file not found. Using defaults.");Logs an error message to the console with an "ERROR" prefix.
message: The error message to be logged.
PajamaLogger.error("Failed to initialize the rendering engine.");Basic Logging:
PajamaLogger.log("Engine initialized.");
PajamaLogger.error("Could not find component.");The PajamaResponses enum centralizes predefined application messages, improving maintainability and consistency in logs and exceptions.
- Centralized Messaging: Keeps messages reusable and consistent across the framework.
- Readability: Makes code more readable by replacing hardcoded strings with meaningful enum values.
- Message:
"Application started successfully." - Description: Indicates that the application and context have been initialized without errors.
- Message:
"No component found for: " - Description: Used when dependency injection fails due to a missing component.
- Message:
"No GameLoop class found in the context." - Description: Indicates that no class annotated with
@GameLoopis registered in the context.
- Message:
"The GameLoop class does not implement Runnable." - Description: Used when the
@GameLoopannotated class does not implement theRunnableinterface.
- Message:
"Component injection completed successfully." - Description: Indicates that dependency injection was completed without errors.
Returns the message associated with the enum value.
String message = PajamaResponses.APPLICATION_STARTED_SUCCESSFULLY.getMessage();
System.out.println(message); // Output: Application started successfully.Logging Responses:
PajamaLogger.log(PajamaResponses.APPLICATION_STARTED_SUCCESSFULLY.getMessage());
PajamaLogger.error(PajamaResponses.NO_GAME_LOOP_FOUND.getMessage());Exception Handling:
if (gameLoop == null) {
throw new RuntimeException(PajamaResponses.NO_GAME_LOOP_FOUND.getMessage());
}- Use
PajamaResponses:- Replace hardcoded strings with predefined messages to improve maintainability.
- Use
PajamaLoggerfor all logs:- Ensures consistent log formatting and simplifies debugging.
@GameLoop
public class MyGame implements Runnable {
@Override
public void run() {
PajamaLogger.log(PajamaResponses.APPLICATION_STARTED_SUCCESSFULLY.getMessage());
// Game loop logic...
}
}
public static void main(String[] args) throws Exception {
try {
PajamaApplication.run(MyGame.class);
} catch (RuntimeException e) {
PajamaLogger.error(e.getMessage());
}
}