๐Ÿ“‹ Advanced Features

Pika ORM Logger

Pika ORM includes a flexible logging system that helps you monitor and debug SQL queries executed by the ORM. The logger captures INSERT, UPDATE, DELETE, and SELECT statements along with their parameters.

๐Ÿ”ดERROR
๐ŸŸ WARN
๐Ÿ”ตINFO
๐ŸŸขDEBUG
โšชTRACE

The PikaLogger Interface

interface PikaLogger {
    enum Level {
        ERROR, WARN, INFO, DEBUG, TRACE
    }

    void log(Level level, String msg, Object... args);
}

Default Logging Behavior

By default, Pika ORM logs SQL queries to System.out when logging is enabled.

// Enable default logging
orm.logQueries();

SampleModel model = new SampleModel("example", 42, true, new Date());
long id = orm.insert(model);

// Output:
// INSERT SQL: INSERT INTO sample_models (bool_val, date_val, int_val, str_val)
// VALUES (?, ?, ?, ?)
// Args: [true, 2021-02-01, 42, example]

Custom Logger Integration (SLF4J)

The most common enterprise use case is tying Pika's output into SLF4J.

Logger logger = LoggerFactory.getLogger(LoggingTest.class);

// Set up SLF4J logger adapter
orm.withLogger((level, msg, args) -> {
    switch (level) {
        case TRACE -> logger.trace(msg, args);
        case DEBUG -> logger.debug(msg, args);
        case INFO  -> logger.info(msg, args);
        case WARN  -> logger.warn(msg, args);
        case ERROR -> logger.error(msg, args);
    }
});

Production Best Practices

In production environments, it's highly recommended to only log warnings or slow queries to prevent log bloat.

orm.withLogger((level, msg, args) -> {
    // Only log warnings and errors
    if (level == PikaLogger.Level.ERROR || level == PikaLogger.Level.WARN) {
        prodLogger.error("Database issue: {} - Args: {}", msg, Arrays.toString(args));
    }

    // Monitor complex queries
    if (msg.contains("SELECT") && args.length > 10) {
        prodLogger.info("Complex query detected: {}", msg);
    }
});

Tip: Use TRACE level during development for full SQL visibility, and switch to WARN or ERROR only in production to keep your logs clean.