Class LogEnricherPostgreSql

java.lang.Object
com.github.isuhorukov.log.watcher.LogEnricherPostgreSql
All Implemented Interfaces:
LogEnricher, Closeable, AutoCloseable

public class LogEnricherPostgreSql extends Object implements LogEnricher
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 Details

    • LOG_WATCHER_ENRICHER

      public static final String 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 new LogEnricherPostgreSql 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 server
      port - the port number of the PostgreSQL server
      database - the name of the PostgreSQL database
      user - the username for accessing the PostgreSQL database
      password - the password for accessing the PostgreSQL database
      maximumSize - the maximum size of the cache for storing recently used SQL queries
  • Method Details

    • getStatement

      public String getStatement(String queryId)
      Retrieves the SQL query corresponding to the provided query ID from the cache or the PostgreSQL database. If the query ID is null, empty, or not a valid number, this method returns null.
      Specified by:
      getStatement in interface LogEnricher
      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

      public String enricherApplicationName()
      Returns the application name associated with this log enricher.
      Specified by:
      enricherApplicationName in interface LogEnricher
      Returns:
      the application name as a string
    • close

      public void close() throws IOException
      Closes the database connection and releases any resources held by this log enricher.

      This method is annotated with SneakyThrows to rethrow any thrown IOException as a runtime exception. It ensures that the underlying Connection is closed properly when this enricher is no longer needed.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - if an I/O error occurs while closing the database connection.