A lightweight, singleton-based logging utility for Java applications with support for multiple log levels, file and console output, and server-specific logging methods.
This logger implementation is NOT thread-safe. It's designed for single-threaded applications or scenarios where thread safety is handled externally. For multi-threaded applications, consider using established logging frameworks like Logback, Log4j2, or java.util.logging.
- Singleton Pattern: Ensures only one logger instance throughout the application
- Multiple Log Levels: DEBUG, INFO, WARN, ERROR with configurable filtering
- Dual Output: Simultaneous logging to console and file
- Configurable: Log level and console output can be configured via ServerProperties
- Server-Specific Methods: Built-in methods for common server operations logging
- Automatic File Management: Creates log directories if they don't exist
- Timestamp Support: All log entries include formatted timestamps
The logger supports four log levels in ascending order of severity:
- DEBUG - Detailed information for debugging purposes
- INFO - General information about application flow
- WARN - Warning messages for potentially harmful situations
- ERROR - Error messages for serious problems
Logger logger = Logger.getInstance();
logger.debug("Debug information");
logger.info("Application started");
logger.warn("This is a warning");
logger.error("An error occurred");
logger.error("Database connection failed", exception);
Logger logger = Logger.getInstance();
// Server lifecycle
logger.logServerStart("localhost", 8080);
logger.logServerStop();
// Client connections
logger.logClientConnection("192.168.1.100");
logger.logClientDisconnection("192.168.1.100");
// HTTP requests/responses
logger.logRequest("192.168.1.100", "GET", "/api/users");
logger.logResponse("192.168.1.100", 200, "/api/users");
// File operations
logger.logFileOperation("UPLOAD", "document.pdf", true);
logger.logFileOperation("DELETE", "temp.txt", false);
// Always close the logger when your application shuts down
logger.close();
The logger expects a ServerProperties
class with the following methods:
public class ServerProperties {
public static ServerProperties getInstance() { /* implementation */ }
public String getLogLevel() { /* return "DEBUG", "INFO", "WARN", or "ERROR" */ }
public String isLogConsoleEnabled() { /* return "true" or "false" */ }
public String getLogFilePath() { /* return path to log file */ }
}
Log entries follow this format:
[DD/MM/YYYY HH:MM:SS] LEVEL: Message
Example:
[08/09/2025 14:30:25] INFO: Server started on localhost:8080
[08/09/2025 14:30:26] INFO: REQUEST - 192.168.1.100 | GET /api/users
[08/09/2025 14:30:26] INFO: RESPONSE - 192.168.1.100 | 200 /api/users
- Not Thread-Safe: Multiple threads accessing the logger simultaneously may cause issues
- Single File Output: Only supports logging to one file at a time
- No Log Rotation: Files grow indefinitely without automatic rotation
- Basic Error Handling: Limited error recovery mechanisms
This logger is suitable for:
- Simple single-threaded applications
- Prototypes and educational projects
- Small server applications with controlled threading
- Learning purposes and understanding logging concepts
Avoid this logger for:
- Multi-threaded production applications
- High-volume logging scenarios
- Applications requiring advanced features (log rotation, multiple appenders, etc.)
- Enterprise applications (use Log4j2, Logback, or similar instead)
For production applications, consider these thread-safe alternatives:
- Logback - Successor to Log4j, excellent performance
- Log4j2 - High-performance, feature-rich
- java.util.logging - Built into Java, thread-safe
This is a simple educational/prototype logger. If you find it useful and want to improve it:
- Fork the repository
- Create a feature branch
- Make your changes (consider thread-safety improvements!)
- Submit a pull request
This project is open source. Feel free to use, modify, and distribute as needed.
Originally developed for a personal server project. While replaced due to thread-safety requirements, this code demonstrates solid fundamentals in logging system design and configuration management.
Note: This is a complete, working system that's perfect for single-threaded applications, prototypes, or educational purposes. For production multi-threaded environments, consider established logging frameworks!