Class EventLogManager

java.lang.Object
co.mindus.ip2location.EventLogManager

public final class EventLogManager extends Object
Manages the IP2Location event log file.

This class provides a simple, thread-safe mechanism for logging events and errors to a flat text file. The log file is stored in the server's root directory by default.

File Format

Each line in the log file follows this format:

TIMESTAMP|LEVEL|TYPE|MESSAGE

Example:

2026-02-04T12:30:45.123Z|INFO|SERVICE_STARTED|Service initialized successfully
2026-02-04T12:31:00.456Z|ERROR|DOWNLOAD_FAILED|HTTP 403 - Token expired

Usage Example

EventLogManager log = EventLogManager.getInstance();

// Configure (optional - defaults to "ip2location-events.log" in current dir)
log.setLogFilePath(Paths.get("/server/root/ip2location-events.log"));
log.initialize();

// Log events
log.logInfo(EventType.SERVICE_STARTED, "Service initialized");
log.logWarning(EventType.RECOVERY_RECOMMENDED, "2 consecutive init failures");
log.logError(EventType.DOWNLOAD_FAILED, "HTTP 403 - Token expired", httpException);

// Retrieve events
List<EventEntry> recent = log.getRecentEvents(50);
List<EventEntry> errors = log.getErrorsOnly(20);

// Clear log
log.clearLog();

Thread Safety

This class is fully thread-safe. Multiple threads can log events and read the log concurrently.

Author:
Christopher Mindus
  • Field Details

    • DEFAULT_LOG_FILE

      public static final String DEFAULT_LOG_FILE
      Default log file name.
      See Also:
    • MAX_LOG_ENTRIES

      public static final int MAX_LOG_ENTRIES
      Maximum entries to keep in the log (to prevent unbounded growth).
      See Also:
  • Method Details

    • getInstance

      public static EventLogManager getInstance()
      Returns the singleton instance.
      Returns:
      The EventLogManager instance
    • setLogFilePath

      public void setLogFilePath(Path path)
      Sets the log file path.

      This should be called before any logging occurs.

      Parameters:
      path - The path to the log file
    • setLogFilePath

      public void setLogFilePath(String path)
      Sets the log file path from a string.
      Parameters:
      path - The path string
    • getLogFilePath

      public Path getLogFilePath()
      Returns the current log file path.
      Returns:
      The log file path
    • setEnabled

      public void setEnabled(boolean enabled)
      Enables or disables logging.
      Parameters:
      enabled - true to enable logging
    • isEnabled

      public boolean isEnabled()
      Returns whether logging is enabled.
      Returns:
      true if logging is enabled
    • initialize

      public boolean initialize()
      Initializes the log file.

      Creates the parent directories if necessary. Safe to call multiple times.

      Returns:
      true if initialization succeeded
    • logInfo

      public void logInfo(EventLogManager.EventType type, String message)
      Logs an informational event.
      Parameters:
      type - The event type
      message - The message
    • logWarning

      public void logWarning(EventLogManager.EventType type, String message)
      Logs a warning event.
      Parameters:
      type - The event type
      message - The message
    • logError

      public void logError(EventLogManager.EventType type, String message)
      Logs an error event.
      Parameters:
      type - The event type
      message - The message
    • logError

      public void logError(EventLogManager.EventType type, String message, Throwable error)
      Logs an error event with exception details.
      Parameters:
      type - The event type
      message - The message
      error - The exception (may be null)
    • log

      public void log(EventLogManager.Level level, EventLogManager.EventType type, String message)
      Logs an event.
      Parameters:
      level - The log level
      type - The event type
      message - The message
    • logFromErrorType

      public void logFromErrorType(IServiceStatusListener.ErrorType errorType, String message, Throwable error)
      Logs an event from an IServiceStatusListener.ErrorType.
      Parameters:
      errorType - The error type
      message - The message
      error - The exception (may be null)
    • getAllEvents

      public List<EventLogManager.EventEntry> getAllEvents()
      Returns all events in the log.
      Returns:
      List of all events (most recent first)
    • getRecentEvents

      public List<EventLogManager.EventEntry> getRecentEvents(int maxCount)
      Returns the most recent events.
      Parameters:
      maxCount - Maximum number of events to return
      Returns:
      List of events (most recent first)
    • getErrorsOnly

      public List<EventLogManager.EventEntry> getErrorsOnly(int maxCount)
      Returns only error events.
      Parameters:
      maxCount - Maximum number of errors to return
      Returns:
      List of error events (most recent first)
    • getWarningsOnly

      public List<EventLogManager.EventEntry> getWarningsOnly(int maxCount)
      Returns only warning events.
      Parameters:
      maxCount - Maximum number of warnings to return
      Returns:
      List of warning events (most recent first)
    • getEventsByLevel

      public List<EventLogManager.EventEntry> getEventsByLevel(EventLogManager.Level level, int maxCount)
      Returns events of a specific level.
      Parameters:
      level - The level to filter by
      maxCount - Maximum number of events to return
      Returns:
      List of events (most recent first)
    • getEventsByType

      public List<EventLogManager.EventEntry> getEventsByType(EventLogManager.EventType type, int maxCount)
      Returns events of a specific type.
      Parameters:
      type - The event type to filter by
      maxCount - Maximum number of events to return
      Returns:
      List of events (most recent first)
    • getEventCount

      public int getEventCount()
      Returns the total number of events in the log.
      Returns:
      Event count
    • getStatistics

      public EventLogManager.LogStatistics getStatistics()
      Returns statistics about the log.
      Returns:
      Log statistics
    • clearLog

      public boolean clearLog()
      Clears all events from the log.
      Returns:
      true if cleared successfully
    • logFileExists

      public boolean logFileExists()
      Returns whether the log file exists.
      Returns:
      true if the log file exists