Tuesday, January 17, 2023
HomeProgrammingMethods to log all SQL statements executed by R2DBC

Methods to log all SQL statements executed by R2DBC


jOOQ already has a LoggingConnection (see additionally the handbook), which acts as a JDBC proxy Connection to log all SQL statements which can be executed by any JDBC consumer (together with Hibernate, MyBatis, JdbcTemplate, native JDBC, and so forth.).

Ranging from jOOQ 3.18.0, 3.17.7, and three.16.13, a LoggingConnection is now additionally accessible for R2DBC purchasers to log all reactive queries. Whereas some R2DBC drivers already do their very own DEBUG logging, a few of them don’t, so this utility will likely be very helpful to jOOQ customers or anybody else working with R2DBC.

When you don’t need to add the jOOQ dependency, you may merely use the LoggingConnection code accessible from github. To provide you an concept of what it does:

// The jOOQ DefaultConnection simply delegates all calls 
// to a delegate Connection
public class LoggingConnection extends DefaultConnection {

    // Use your personal logger, alternatively
    non-public static last JooqLogger log = 
        JooqLogger.getLogger(LoggingConnection.class);

    public LoggingConnection(Connection delegate) {
        tremendous(delegate);
    }

    @Override
    public Writer<Void> shut() {
        return s -> {
            if (log.isDebugEnabled())
                log.debug("Connection::shut");

            getDelegate().shut().subscribe(s);
        };
    }

    @Override
    public Assertion createStatement(String sql) {
        if (log.isDebugEnabled())
            log.debug("Connection::createStatement", sql);

        return new LoggingStatement(getDelegate().createStatement(sql));
    }

    // [...]
}

And the identical factor is completed with a wrapper for Assertion or Batch:

// The jOOQ DefaultStatement simply delegates all calls 
// to a delegate Assertion
public class LoggingStatement extends DefaultStatement {

    // Use your personal logger, alternatively
    non-public static last JooqLogger log = 
        JooqLogger.getLogger(LoggingStatement.class);

    public LoggingStatement(Assertion delegate) {
        tremendous(delegate);
    }

    @Override
    public Assertion add() {
        if (log.isDebugEnabled())
            log.debug("Assertion::add");

        getDelegate().add();
        return this;
    }

    @Override
    public Writer<? extends Consequence> execute() {
        return s -> {
            if (log.isDebugEnabled())
                log.debug("Assertion::execute");

            getDelegate().execute().subscribe(s);
        };
    }
}

That’s it!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments