Class LogEnricherPostgreSql
java.lang.Object
com.github.isuhorukov.log.watcher.LogEnricherPostgreSql
- All Implemented Interfaces:
LogEnricher
,Closeable
,AutoCloseable
The
LogEnricherPostgreSql
class implements the LogEnricher
interface
to provide log enrichment from a PostgreSQL database. It uses the pg_stat_statements
extension to fetch SQL query statements based on query IDs.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
Represents the identifier for the log watcher enricher. -
Constructor Summary
ConstructorsConstructorDescriptionLogEnricherPostgreSql
(String host, int port, String database, String user, String password, int maximumSize) Constructs a newLogEnricherPostgreSql
instance for enriching PostgreSQL logs. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the database connection and releases any resources held by this log enricher.Returns the application name associated with this log enricher.getStatement
(String queryId) Retrieves the SQL query corresponding to the provided query ID from the cache or the PostgreSQL database.
-
Field Details
-
LOG_WATCHER_ENRICHER
Represents the identifier for the log watcher enricher.- See Also:
-
-
Constructor Details
-
LogEnricherPostgreSql
public LogEnricherPostgreSql(String host, int port, String database, String user, String password, int maximumSize) Constructs a newLogEnricherPostgreSql
instance for enriching PostgreSQL logs. Initializes the database connection, prepared statement, and cache for storing SQL queries text.- Parameters:
host
- the hostname of the PostgreSQL serverport
- the port number of the PostgreSQL serverdatabase
- the name of the PostgreSQL databaseuser
- the username for accessing the PostgreSQL databasepassword
- the password for accessing the PostgreSQL databasemaximumSize
- the maximum size of the cache for storing recently used SQL queries
-
-
Method Details
-
getStatement
Retrieves the SQL query corresponding to the provided query ID from the cache or the PostgreSQL database. If the query ID isnull
, empty, or not a valid number, this method returnsnull
.- Specified by:
getStatement
in interfaceLogEnricher
- Parameters:
queryId
- the ID of the query to retrieve- Returns:
- the SQL query associated with the provided query ID, or
null
if the query ID is invalid
-
enricherApplicationName
Returns the application name associated with this log enricher.- Specified by:
enricherApplicationName
in interfaceLogEnricher
- Returns:
- the application name as a string
-
close
Closes the database connection and releases any resources held by this log enricher.This method is annotated with
SneakyThrows
to rethrow any thrownIOException
as a runtime exception. It ensures that the underlyingConnection
is closed properly when this enricher is no longer needed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- if an I/O error occurs while closing the database connection.
-