Technology
Building a Custom Logger in OOPM/Java: A Simple Mini-Project with Code
Building a Custom Logger in OOPM/Java: A Simple Mini-Project with Code
Object-Oriented Programming (OOP) in Java offers a robust foundation for building modular and maintainable applications. One practical project to enhance your understanding of OOP principles is to create your own logger. This mini-project not only helps in understanding design patterns but also provides a valuable tool that you can reuse in numerous other projects. In this article, we'll walk through the process of building a custom logger in Java, focusing on log levels and file management.
Project Overview
The objective is to create a logger class that can handle different levels of log messages and write them into a single file. We'll utilize design patterns to make the code more flexible and reusable. This project is ideal for beginners and experienced developers alike, as it covers fundamental OOP concepts and introduces common design patterns.
Objectives
Create a logger class in Java Implement different log levels (e.g., DEBUG, INFO, ERROR) Write log messages to a file based on their level Utilize design patterns for better code organizationStep 1: Define the Logger Class
The first step is to define the logger class. This class will be responsible for handling log messages and writing them to a file based on their severity level. We can use the Singleton design pattern to ensure that only one instance of the logger exists throughout the application.
import ; import ; public class CustomLogger { private static CustomLogger instance; private FileWriter writer; private String logFilePath "app.log"; private CustomLogger() { try { writer new FileWriter(logFilePath, true); } catch (IOException e) { (); } } public static CustomLogger getInstance() { if (instance null) { instance new CustomLogger(); } return instance; } public void log(String message, LogLevel level) { if (() 0) { try { synchronized (this) { writer.write(() " - " message " "); writer.flush(); } } catch (IOException e) { (); } } } }
Step 2: Add Log Levels
Next, we define the log levels. We'll create an enum to handle the different levels of severity. This approach makes it easier to manage and extend log levels in the future.
public enum LogLevel { DEBUG, INFO, WARN, ERROR; public static LogLevel parse(String level) { switch (()) { case "DEBUG": return DEBUG; case "INFO": return INFO; case "WARN": return WARN; case "ERROR": return ERROR; default: throw new IllegalArgumentException("Unknown log level: " level); } } public int compareTo(LogLevel other) { switch (this) { case DEBUG: return -1; case INFO: return 0; case WARN: return 1; case ERROR: return 2; default: throw new IllegalArgumentException("Unknown log level: " this); } } }
Step 3: Use the Logger in Your Application
Now that we have a custom logger and log levels, we can use it in our application. We'll add some example code to demonstrate how to create an instance of the logger and use it to log messages at different levels.
import ; public class Main { public static void main(String[] args) { CustomLogger logger (); Scanner scanner new Scanner(); logger.log("Starting application", ); while (true) { ("Enter message: "); String message (); ("Enter log level (DEBUG, INFO, WARN, ERROR): "); String levelStr (); LogLevel level (levelStr); logger.log(message, level); if (level ) { break; } } logger.log("Ending application", ); } }
Step 4: Enhance with Design Patterns
To further enhance the logger, consider implementing the Factory pattern to create different types of loggers for different purposes, such as for debugging or production environments. Additionally, the Strategy pattern can be used to change how logs are written, such as to a file, a database, or a remote server.
Conclusion
Creating a custom logger in Java is an excellent mini-project that reinforces your understanding of object-oriented programming principles and design patterns. By implementing different log levels and file management, you can create a powerful tool that is reusable across multiple projects. This project not only enhances your coding skills but also prepares you for more complex logging and debugging scenarios in real-world applications.