CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-mysql--mysql-connector-j

JDBC Type 4 driver for MySQL with X DevAPI support for document store operations

Overview
Eval results
Files

logging-monitoring.mddocs/

Logging and Monitoring

Logging interfaces and implementations, plus profiling capabilities for monitoring driver behavior and query execution.

Capabilities

Log Interface

Unified logging interface supporting multiple logging frameworks.

package com.mysql.cj.log;

public interface Log {
    // Check log levels
    boolean isDebugEnabled();
    boolean isErrorEnabled();
    boolean isFatalEnabled();
    boolean isInfoEnabled();
    boolean isTraceEnabled();
    boolean isWarnEnabled();

    // Debug logging
    void logDebug(Object msg);
    void logDebug(Object msg, Throwable thrown);

    // Error logging
    void logError(Object msg);
    void logError(Object msg, Throwable thrown);

    // Fatal logging
    void logFatal(Object msg);
    void logFatal(Object msg, Throwable thrown);

    // Info logging
    void logInfo(Object msg);
    void logInfo(Object msg, Throwable thrown);

    // Trace logging
    void logTrace(Object msg);
    void logTrace(Object msg, Throwable thrown);

    // Warn logging
    void logWarn(Object msg);
    void logWarn(Object msg, Throwable thrown);
}

Standard Logger

Built-in logger that writes to STDERR.

package com.mysql.cj.log;

public class StandardLogger implements Log {
    // Constructor
    public StandardLogger(String name);
    public StandardLogger(String name, boolean logLocationInfo);

    // Implements all Log methods
}

Usage:

// Enable standard logger
String url = "jdbc:mysql://localhost:3306/mydb?logger=StandardLogger&profileSQL=true";
Connection conn = DriverManager.getConnection(url, "user", "pass");

// Logger will output to System.err
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM users");
// Logs query execution details

SLF4J Logger

Logger implementation using SLF4J.

package com.mysql.cj.log;

public class Slf4JLogger implements Log {
    // Constructor
    public Slf4JLogger(String name);

    // Implements all Log methods, delegates to SLF4J
}

Usage:

// Use SLF4J logger (requires slf4j-api on classpath)
String url = "jdbc:mysql://localhost:3306/mydb?logger=Slf4JLogger&profileSQL=true";
Connection conn = DriverManager.getConnection(url, "user", "pass");

// Logging goes through SLF4J to configured backend (logback, log4j, etc.)

JDK 1.4 Logger

Logger implementation using java.util.logging.

package com.mysql.cj.log;

public class Jdk14Logger implements Log {
    // Constructor
    public Jdk14Logger(String name);

    // Implements all Log methods, delegates to java.util.logging
}

Usage:

// Use JDK logger
String url = "jdbc:mysql://localhost:3306/mydb?logger=Jdk14Logger&profileSQL=true";
Connection conn = DriverManager.getConnection(url, "user", "pass");

// Configure java.util.logging as usual

Null Logger

No-op logger that discards all log messages.

package com.mysql.cj.log;

public class NullLogger implements Log {
    // Constructor
    public NullLogger(String instanceName);

    // Implements all Log methods as no-ops
}

Profiler Event Interface

Interface for profiling events.

package com.mysql.cj.log;

public interface ProfilerEvent {
    // Event type
    byte getEventType();

    // Event type constants
    byte TYPE_EXECUTE = 1;
    byte TYPE_FETCH = 2;
    byte TYPE_OBJECT_CREATION = 3;
    byte TYPE_PREPARE = 4;
    byte TYPE_QUERY = 5;
    byte TYPE_WARN = 6;
    byte TYPE_SLOW_QUERY = 7;

    // Event details
    String getCatalog();
    long getConnectionId();
    int getResultSetColumnCount();
    long getResultSetRowsCount();
    String getMessage();

    // Timing information
    long getEventCreationTime();
    long getEventDuration();
    String getDurationUnits();

    // Stack trace
    String getEventCreationPointAsString();
}

Profiler Event Handler

Interface for handling profiler events.

package com.mysql.cj.log;

public interface ProfilerEventHandler {
    // Initialize handler
    void init(Log log);

    // Destroy handler
    void destroy();

    // Consume profiler event
    void consumeEvent(ProfilerEvent evt);
}

Usage:

// Implement custom profiler event handler
public class MyProfilerEventHandler implements ProfilerEventHandler {
    private Log log;

    public void init(Log log) {
        this.log = log;
    }

    public void destroy() {
        // Cleanup
    }

    public void consumeEvent(ProfilerEvent evt) {
        if (evt.getEventType() == ProfilerEvent.TYPE_SLOW_QUERY) {
            log.logWarn("Slow query detected: " + evt.getMessage() +
                       ", Duration: " + evt.getEventDuration() + evt.getDurationUnits());
        }
    }
}

// Enable profiling with custom handler
String url = "jdbc:mysql://localhost:3306/mydb" +
             "?profileSQL=true" +
             "&profilerEventHandler=com.mycompany.MyProfilerEventHandler";
Connection conn = DriverManager.getConnection(url, "user", "pass");

// Or use built-in slow query logging
String url2 = "jdbc:mysql://localhost:3306/mydb" +
              "?profileSQL=true" +
              "&logSlowQueries=true" +
              "&slowQueryThresholdMillis=1000";  // Log queries slower than 1 second

Log Utilities

Utility methods for logging.

package com.mysql.cj.log;

public class LogUtils {
    // Convenience logging methods
    public static void logInfo(Log logger, Object message);
    public static void logDebug(Log logger, Object message);
    public static void logWarn(Log logger, Object message);
    public static void logError(Log logger, Object message);
    public static void logFatal(Log logger, Object message);
    public static void logTrace(Log logger, Object message);

    // Expand exception stack trace
    public static String stackTraceToString(Throwable t);
}

Configuration Properties

Logging and monitoring related configuration properties:

// Enable SQL profiling
profileSQL=true

// Enable query logging
logger=StandardLogger  // or Slf4JLogger, Jdk14Logger

// Log slow queries
logSlowQueries=true
slowQueryThresholdMillis=2000

// Enable usage advisor (warns about inefficient usage patterns)
useUsageAdvisor=true

// Gather performance metrics
gatherPerfMetrics=true

// Report metrics interval (milliseconds)
reportMetricsIntervalMillis=30000

// Enable statement timing
maintainTimeStats=true

// Enable automatic reconnection logging
autoReconnectForPools=true

Complete usage example:

// Comprehensive logging and monitoring configuration
String url = "jdbc:mysql://localhost:3306/mydb" +
             "?logger=Slf4JLogger" +
             "&profileSQL=true" +
             "&logSlowQueries=true" +
             "&slowQueryThresholdMillis=1000" +
             "&useUsageAdvisor=true" +
             "&gatherPerfMetrics=true" +
             "&maintainTimeStats=true";

Connection conn = DriverManager.getConnection(url, "user", "pass");

// Execute queries - will be logged and profiled
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM large_table");

// Slow queries will be logged
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM users WHERE id = ?");
pstmt.setInt(1, 1);
pstmt.executeQuery();

// Usage advisor warnings will be logged for inefficient patterns
stmt.executeQuery("SELECT * FROM users");
// Iterating without calling rs.next() properly will trigger warning

conn.close();

Install with Tessl CLI

npx tessl i tessl/maven-com-mysql--mysql-connector-j

docs

authentication.md

configuration.md

exceptions.md

index.md

interceptors.md

jdbc-advanced.md

jdbc-core.md

jdbc-high-availability.md

logging-monitoring.md

type-system.md

utilities.md

xdevapi-core.md

xdevapi-crud.md

xdevapi-sql.md

tile.json