Class EventLogManager
java.lang.Object
co.mindus.ip2location.EventLogManager
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
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classRepresents a single event entry in the log.static enumTypes of events that can be logged.static enumLog levels for events.static final classStatistics about the event log.Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringDefault log file name.static final intMaximum entries to keep in the log (to prevent unbounded growth).Method Summary
Modifier and TypeMethodDescriptionbooleanclearLog()Clears all events from the log.Returns all events in the log.getErrorsOnly(int maxCount) Returns only error events.intReturns the total number of events in the log.getEventsByLevel(EventLogManager.Level level, int maxCount) Returns events of a specific level.getEventsByType(EventLogManager.EventType type, int maxCount) Returns events of a specific type.static EventLogManagerReturns the singleton instance.Returns the current log file path.getRecentEvents(int maxCount) Returns the most recent events.Returns statistics about the log.getWarningsOnly(int maxCount) Returns only warning events.booleanInitializes the log file.booleanReturns whether logging is enabled.voidlog(EventLogManager.Level level, EventLogManager.EventType type, String message) Logs an event.voidlogError(EventLogManager.EventType type, String message) Logs an error event.voidlogError(EventLogManager.EventType type, String message, Throwable error) Logs an error event with exception details.booleanReturns whether the log file exists.voidlogFromErrorType(IServiceStatusListener.ErrorType errorType, String message, Throwable error) Logs an event from an IServiceStatusListener.ErrorType.voidlogInfo(EventLogManager.EventType type, String message) Logs an informational event.voidlogWarning(EventLogManager.EventType type, String message) Logs a warning event.voidsetEnabled(boolean enabled) Enables or disables logging.voidsetLogFilePath(String path) Sets the log file path from a string.voidsetLogFilePath(Path path) Sets the log file path.
Field Details
DEFAULT_LOG_FILE
MAX_LOG_ENTRIES
public static final int MAX_LOG_ENTRIESMaximum entries to keep in the log (to prevent unbounded growth).- See Also:
Method Details
getInstance
Returns the singleton instance.- Returns:
- The EventLogManager instance
setLogFilePath
Sets the log file path.This should be called before any logging occurs.
- Parameters:
path- The path to the log file
setLogFilePath
Sets the log file path from a string.- Parameters:
path- The path string
getLogFilePath
setEnabled
public void setEnabled(boolean enabled) Enables or disables logging.- Parameters:
enabled-trueto enable logging
isEnabled
public boolean isEnabled()Returns whether logging is enabled.- Returns:
trueif logging is enabled
initialize
public boolean initialize()Initializes the log file.Creates the parent directories if necessary. Safe to call multiple times.
- Returns:
trueif initialization succeeded
logInfo
Logs an informational event.- Parameters:
type- The event typemessage- The message
logWarning
Logs a warning event.- Parameters:
type- The event typemessage- The message
logError
Logs an error event.- Parameters:
type- The event typemessage- The message
logError
Logs an error event with exception details.- Parameters:
type- The event typemessage- The messageerror- The exception (may be null)
log
Logs an event.- Parameters:
level- The log leveltype- The event typemessage- The message
logFromErrorType
public void logFromErrorType(IServiceStatusListener.ErrorType errorType, String message, Throwable error) Logs an event from an IServiceStatusListener.ErrorType.- Parameters:
errorType- The error typemessage- The messageerror- The exception (may be null)
getAllEvents
Returns all events in the log.- Returns:
- List of all events (most recent first)
getRecentEvents
Returns the most recent events.- Parameters:
maxCount- Maximum number of events to return- Returns:
- List of events (most recent first)
getErrorsOnly
Returns only error events.- Parameters:
maxCount- Maximum number of errors to return- Returns:
- List of error events (most recent first)
getWarningsOnly
Returns only warning events.- Parameters:
maxCount- Maximum number of warnings to return- Returns:
- List of warning events (most recent first)
getEventsByLevel
Returns events of a specific level.- Parameters:
level- The level to filter bymaxCount- 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 bymaxCount- 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
Returns statistics about the log.- Returns:
- Log statistics
clearLog
public boolean clearLog()Clears all events from the log.- Returns:
trueif cleared successfully
logFileExists
public boolean logFileExists()Returns whether the log file exists.- Returns:
trueif the log file exists