java.lang.Object
com.github.eventmanager.EventManager
The EventManager class is responsible for managing and logging events.
It provides methods to log messages with different log levels and formats.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected BlockingQueue<String>
protected Thread
protected LogHandler
-
Constructor Summary
ConstructorsConstructorDescriptionEventManager
(LogHandler logHandler) Constructs an EventManager with the specified LogHandler.EventManager
(String configPath) Constructs an EventManager with the specified configuration file path. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Checks if informational logs are enabled.Creates a new Thread to write events to the log file.void
logDebugMessage
(KeyValueWrapper... args) Logs a debug message with key-value pairs.void
logDebugMessage
(Object exception) Logs a debug message.void
logErrorMessage
(KeyValueWrapper... args) Logs an error message with key-value pairs.void
logErrorMessage
(Object exception) Logs an error message.void
logFatalMessage
(KeyValueWrapper... args) Logs a fatal message with key-value pairs.void
logFatalMessage
(Object exception) Logs a fatal message.void
logInfoMessage
(KeyValueWrapper... args) Logs an informational message with key-value pairs.void
logInfoMessage
(Object exception) Logs an informational message.protected void
logMessage
(String level, Object message) Logs a message to the destination file.void
logWarningMessage
(KeyValueWrapper... args) Logs a warning message with key-value pairs.void
logWarningMessage
(Object exception) Logs a warning message.static String
setCorrectOSSeperator
(String path) Sets the correct OS-specific file separator in the given path.setMetaDataFields
(String level) Sets the metadata fields for the event.void
Stops the event thread by interrupting it and waiting for it to finish.protected void
writeEventToLogFile
(String event) Writes the given event to the log file.protected void
writeEventToQueue
(String event) Writes the given event to the event queue.
-
Field Details
-
logHandler
-
eventThread
-
eventQueue
-
-
Constructor Details
-
EventManager
Constructs an EventManager with the specified LogHandler.- Parameters:
logHandler
- the LogHandler to use for logging events.
-
EventManager
Constructs an EventManager with the specified configuration file path.- Parameters:
configPath
- the path to the configuration file. Passing an empty string or invalid path will force the EventManager to fall back to the default configuration.
-
-
Method Details
-
stopEventThread
public void stopEventThread()Stops the event thread by interrupting it and waiting for it to finish. This method should be called before shutting down the application to ensure that all events are written to the log file. -
setCorrectOSSeperator
Sets the correct OS-specific file separator in the given path.- Parameters:
path
- the file path to adjust.- Returns:
- the adjusted file path with the correct OS-specific separator.
-
writeEventToLogFile
Writes the given event to the log file. If the event is not empty, it will be written to the log file specified in the configuration file. If the log file does not exist, it will be created. If the log file cannot be created or written to, an error message will be printed to the console.- Parameters:
event
- the event to write to the log file.
-
logFatalMessage
Logs a fatal message.- Parameters:
exception
- the exception to log.
-
logFatalMessage
Logs a fatal message with key-value pairs.- Parameters:
args
- the key-value pairs to log.
-
logErrorMessage
Logs an error message.- Parameters:
exception
- the exception to log.
-
logErrorMessage
Logs an error message with key-value pairs.- Parameters:
args
- the key-value pairs to log.
-
logWarningMessage
Logs a warning message.- Parameters:
exception
- the exception to log.
-
logWarningMessage
Logs a warning message with key-value pairs.- Parameters:
args
- the key-value pairs to log.
-
areInfoLogsEnabled
public boolean areInfoLogsEnabled()Checks if informational logs are enabled.- Returns:
- true if informational logs are enabled, false otherwise.
-
logInfoMessage
Logs an informational message.- Parameters:
exception
- the exception to log.
-
logInfoMessage
Logs an informational message with key-value pairs.- Parameters:
args
- the key-value pairs to log.
-
logDebugMessage
Logs a debug message.- Parameters:
exception
- the exception to log.
-
logDebugMessage
Logs a debug message with key-value pairs.- Parameters:
args
- the key-value pairs to log.
-
initiatEventThread
Creates a new Thread to write events to the log file. It will run indefinitely until it is interrupted (either by calling theManagerBase.stopEventThread()
method or by the JVM shutting down). The thread will write events to the log file if the queue is not empty and internal events are enabled. -
setMetaDataFields
Sets the metadata fields for the event.- Parameters:
level
- the log level of the event.- Returns:
- a map containing the metadata fields.
-
logMessage
Logs a message to the destination file.- Parameters:
level
- the log level of the message.message
- the object to log.
-
writeEventToQueue
Writes the given event to the event queue. This method is thread-safe and can be called from multiple threads.- Parameters:
event
- the event to write to the queue.
-