public final class EventLogManager extends Object
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.
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
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();
This class is fully thread-safe. Multiple threads can log events and read the log concurrently.
| Modifier and Type | Class and Description |
|---|---|
static class |
EventLogManager.EventEntry
Represents a single event entry in the log.
|
static class |
EventLogManager.EventType
Types of events that can be logged.
|
static class |
EventLogManager.Level
Log levels for events.
|
static class |
EventLogManager.LogStatistics
Statistics about the event log.
|
| Modifier and Type | Field and Description |
|---|---|
static String |
DEFAULT_LOG_FILE
Default log file name.
|
static int |
MAX_LOG_ENTRIES
Maximum entries to keep in the log (to prevent unbounded growth).
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
clearLog()
Clears all events from the log.
|
List<EventLogManager.EventEntry> |
getAllEvents()
Returns all events in the log.
|
List<EventLogManager.EventEntry> |
getErrorsOnly(int maxCount)
Returns only error events.
|
int |
getEventCount()
Returns the total number of events in the log.
|
List<EventLogManager.EventEntry> |
getEventsByLevel(EventLogManager.Level level,
int maxCount)
Returns events of a specific level.
|
List<EventLogManager.EventEntry> |
getEventsByType(EventLogManager.EventType type,
int maxCount)
Returns events of a specific type.
|
static EventLogManager |
getInstance()
Returns the singleton instance.
|
Path |
getLogFilePath()
Returns the current log file path.
|
List<EventLogManager.EventEntry> |
getRecentEvents(int maxCount)
Returns the most recent events.
|
EventLogManager.LogStatistics |
getStatistics()
Returns statistics about the log.
|
List<EventLogManager.EventEntry> |
getWarningsOnly(int maxCount)
Returns only warning events.
|
boolean |
initialize()
Initializes the log file.
|
boolean |
isEnabled()
Returns whether logging is enabled.
|
void |
log(EventLogManager.Level level,
EventLogManager.EventType type,
String message)
Logs an event.
|
void |
logError(EventLogManager.EventType type,
String message)
Logs an error event.
|
void |
logError(EventLogManager.EventType type,
String message,
Throwable error)
Logs an error event with exception details.
|
boolean |
logFileExists()
Returns whether the log file exists.
|
void |
logFromErrorType(IServiceStatusListener.ErrorType errorType,
String message,
Throwable error)
Logs an event from an IServiceStatusListener.ErrorType.
|
void |
logInfo(EventLogManager.EventType type,
String message)
Logs an informational event.
|
void |
logWarning(EventLogManager.EventType type,
String message)
Logs a warning event.
|
void |
setEnabled(boolean enabled)
Enables or disables logging.
|
void |
setLogFilePath(Path path)
Sets the log file path.
|
void |
setLogFilePath(String path)
Sets the log file path from a string.
|
public static final String DEFAULT_LOG_FILE
public static final int MAX_LOG_ENTRIES
public static EventLogManager getInstance()
public void setLogFilePath(Path path)
This should be called before any logging occurs.
path - The path to the log filepublic void setLogFilePath(String path)
path - The path stringpublic Path getLogFilePath()
public void setEnabled(boolean enabled)
enabled - true to enable loggingpublic boolean isEnabled()
true if logging is enabledpublic boolean initialize()
Creates the parent directories if necessary. Safe to call multiple times.
true if initialization succeededpublic void logInfo(EventLogManager.EventType type, String message)
type - The event typemessage - The messagepublic void logWarning(EventLogManager.EventType type, String message)
type - The event typemessage - The messagepublic void logError(EventLogManager.EventType type, String message)
type - The event typemessage - The messagepublic void logError(EventLogManager.EventType type, String message, Throwable error)
type - The event typemessage - The messageerror - The exception (may be null)public void log(EventLogManager.Level level, EventLogManager.EventType type, String message)
level - The log leveltype - The event typemessage - The messagepublic void logFromErrorType(IServiceStatusListener.ErrorType errorType, String message, Throwable error)
errorType - The error typemessage - The messageerror - The exception (may be null)public List<EventLogManager.EventEntry> getAllEvents()
public List<EventLogManager.EventEntry> getRecentEvents(int maxCount)
maxCount - Maximum number of events to returnpublic List<EventLogManager.EventEntry> getErrorsOnly(int maxCount)
maxCount - Maximum number of errors to returnpublic List<EventLogManager.EventEntry> getWarningsOnly(int maxCount)
maxCount - Maximum number of warnings to returnpublic List<EventLogManager.EventEntry> getEventsByLevel(EventLogManager.Level level, int maxCount)
level - The level to filter bymaxCount - Maximum number of events to returnpublic List<EventLogManager.EventEntry> getEventsByType(EventLogManager.EventType type, int maxCount)
type - The event type to filter bymaxCount - Maximum number of events to returnpublic int getEventCount()
public EventLogManager.LogStatistics getStatistics()
public boolean clearLog()
true if cleared successfullypublic boolean logFileExists()
true if the log file existsPhantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.