Class IP2LocationEventLog
java.lang.Object
co.mindus.ip2location.IP2LocationEventLog
Manages event and error logging for IP2Location service.
Events are persisted to a JSON-lines file for durability across restarts. The log file uses a simple format with one JSON object per line, making it easy to append and parse incrementally.
File Format
Each line is a JSON object:
{"ts":"2026-02-04T10:30:00Z","type":"ERROR","cat":"INIT_FAILURE","msg":"Database file not found","detail":"..."}
{"ts":"2026-02-04T10:31:00Z","type":"INFO","cat":"RECOVERY_STARTED","msg":"Manual recovery triggered"}
Thread Safety
All operations are thread-safe using read-write locks.
Usage Example
IP2LocationEventLog log = IP2LocationEventLog.getInstance();
// Log an error
log.logError(EventCategory.INIT_FAILURE, "Database corrupted", exception);
// Log an info event
log.logInfo(EventCategory.RECOVERY_COMPLETED, "Service restored successfully");
// Retrieve recent events
List<LogEntry> entries = log.getEntries(50);
// Clear the log
log.clear();
- Author:
- Christopher Mindus
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumEvent category for classification.static enumEvent severity level.static classRepresents a single log entry.Field Summary
FieldsMethod Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all entries from cache and file.intGets the current download failure count (consecutive failures this session).Gets all cached entries.getEntries(int maxCount) Gets the most recent entries.Gets entries of a specific type.intGets the number of cached entries.Gets only error entries.intGets the current init failure count (consecutive failures this session).static IP2LocationEventLogGets the singleton instance.intGets the current lookup error count (consecutive errors this session).intGets the total recovery attempt count for this session.intGets the total error count across all categories.booleanReturns whether the log has been initialized with a file path.voidlog(IP2LocationEventLog.EventType type, IP2LocationEventLog.EventCategory category, String message, String detail) Logs an event.voidlogError(IP2LocationEventLog.EventCategory category, String message) Logs an error event.voidlogError(IP2LocationEventLog.EventCategory category, String message, Throwable throwable) Logs an error event with exception detail.voidlogInfo(IP2LocationEventLog.EventCategory category, String message) Logs an informational event.voidlogWarning(IP2LocationEventLog.EventCategory category, String message) Logs a warning event.voidrefresh()Refreshes the cache from the file.voidResets the download failure counter.voidResets all error counters.voidResets the init failure counter.voidResets the lookup error counter.voidsetLogDirectory(Path directory) Sets the log file using a directory path.voidsetLogFile(Path path) Sets the log file path and loads existing entries.
Field Details
DEFAULT_LOG_FILE
Method Details
getInstance
Gets the singleton instance.- Returns:
- The event log instance
setLogFile
Sets the log file path and loads existing entries.This should be called during application startup, typically with a path in the server's root directory.
- Parameters:
path- The path to the log file
setLogDirectory
Sets the log file using a directory path.- Parameters:
directory- The directory containing the log file
isInitialized
public boolean isInitialized()Returns whether the log has been initialized with a file path.- Returns:
trueifsetLogFile(Path)has been called
logInfo
Logs an informational event.- Parameters:
category- The event categorymessage- The event message
logWarning
Logs a warning event.- Parameters:
category- The event categorymessage- The event message
logError
Logs an error event.- Parameters:
category- The event categorymessage- The event message
logError
public void logError(IP2LocationEventLog.EventCategory category, String message, Throwable throwable) Logs an error event with exception detail.- Parameters:
category- The event categorymessage- The event messagethrowable- The exception (may be null)
log
public void log(IP2LocationEventLog.EventType type, IP2LocationEventLog.EventCategory category, String message, String detail) Logs an event.- Parameters:
type- The event typecategory- The event categorymessage- The event messagedetail- Optional detail text
getInitFailureCount
public int getInitFailureCount()Gets the current init failure count (consecutive failures this session).- Returns:
- The number of consecutive initialization failures
getLookupErrorCount
public int getLookupErrorCount()Gets the current lookup error count (consecutive errors this session).- Returns:
- The number of consecutive lookup errors
getDownloadFailureCount
public int getDownloadFailureCount()Gets the current download failure count (consecutive failures this session).- Returns:
- The number of consecutive download failures
getRecoveryAttemptCount
public int getRecoveryAttemptCount()Gets the total recovery attempt count for this session.- Returns:
- The number of recovery attempts made
getTotalErrorCount
public int getTotalErrorCount()Gets the total error count across all categories.- Returns:
- Sum of init failures, lookup errors, and download failures
resetErrorCounters
public void resetErrorCounters()Resets all error counters.This also logs a RECOVERY_CLEARED event.
resetInitFailureCount
public void resetInitFailureCount()Resets the init failure counter.resetLookupErrorCount
public void resetLookupErrorCount()Resets the lookup error counter.resetDownloadFailureCount
public void resetDownloadFailureCount()Resets the download failure counter.getEntries
Gets all cached entries.- Returns:
- Unmodifiable list of entries (most recent last)
getEntries
Gets the most recent entries.- Parameters:
maxCount- Maximum number of entries to return- Returns:
- List of entries (most recent last)
getEntriesByType
Gets entries of a specific type.- Parameters:
type- The event type to filter by- Returns:
- List of matching entries
getErrors
Gets only error entries.- Returns:
- List of error entries
getEntryCount
public int getEntryCount()Gets the number of cached entries.- Returns:
- The number of log entries currently in cache
refresh
public void refresh()Refreshes the cache from the file.Use this to pick up entries written by other processes.
clear
public void clear()Clears all entries from cache and file.