Skip to content

genesshoan/java-simple-logger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

java-simple-logger

Simple Java Logger

A lightweight, singleton-based logging utility for Java applications with support for multiple log levels, file and console output, and server-specific logging methods.

⚠️ Important Notice

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.

Features

  • 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

Log Levels

The logger supports four log levels in ascending order of severity:

  1. DEBUG - Detailed information for debugging purposes
  2. INFO - General information about application flow
  3. WARN - Warning messages for potentially harmful situations
  4. ERROR - Error messages for serious problems

Usage

Basic Logging

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);

Server-Specific Logging

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);

Closing the Logger

// Always close the logger when your application shuts down
logger.close();

Configuration

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 */ }
}

Output Format

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

Limitations

  • 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

When to Use

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

When NOT to Use

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)

Alternative Recommendations

For production applications, consider these thread-safe alternatives:

Contributing

This is a simple educational/prototype logger. If you find it useful and want to improve it:

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes (consider thread-safety improvements!)
  4. Submit a pull request

License

This project is open source. Feel free to use, modify, and distribute as needed.

Author

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!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages