dzikoysk / reposilite

Lightweight and easy-to-use repository management software dedicated for the Maven based artifacts in the JVM ecosystem 📦
https://reposilite.com
Apache License 2.0
1.36k stars 179 forks source link

When in debug mode, log all sql statements #2119

Closed solonovamax closed 4 months ago

solonovamax commented 4 months ago

Request details

Currently, debug mode will log all requests, but not all sql statements. It would be nice if they could be logged as well.

I'm currently attempting to debug an issue with some sql stuff, and I had to make a custom build to get this. It would be nice to have this enabled with debug mode.

you can add a debug logger to a transaction using:

addLogger(StdOutSqlLogger)

this will log to stdout using the format:

SQL: [sql statement to be executed here]

Alternatively, if you want to implement a custom logger that uses your logging framework (bc ik you use a custom one), you can do so by copying the implementation from StdOutSqlLogger:

object StdOutSqlLogger : SqlLogger {
    /** Prints a log message containing the string representation of a complete SQL statement. */
    override fun log(context: StatementContext, transaction: Transaction) {
        println("SQL: ${context.expandArgs(transaction)}")
    }
}

the reason I'm having to debug some issues is bc I went and deleted a bunch of shit with

DELETE FROM statistics_identifier WHERE identifier_id IN (SELECT identifier_id FROM statistics_resolved_identifier WHERE date = 1709874000000 OR date = 1709787600000 OR date = 1709960400000 OR date = 1709701200000);
DELETE FROM statistics_resolved_identifier WHERE date = 1709874000000 OR date = 1709787600000 OR date = 1709960400000 OR date = 1709701200000;

in order to remove some testing things that were making my stats out of proportion, but that's a whole other thing lol

solonovamax commented 4 months ago

It seems that you can add a logger to all transactions by doing the following:

// An example with current available settings and their defaults
val dbConfig = DatabaseConfig {
    sqlLogger = Slf4jSqlDebugLogger
    useNestedTransactions = false
    defaultFetchSize = null // unlimited
    defaultIsolationLevel = -1 // DB specific
    defaultRepetitionAttempts = 3
    warnLongQueriesDuration = null // no long query tracing
    maxEntitiesToStoreInCachePerEntity = Int.MAX_VALUE // unlimited 
    keepLoadedReferencesOutOfTransaction = false
}
// databaseConfig available on each connect/connectPool functions
val database = Database.connect(datasource, databaseConfig = databaseConfig)

see: 0.35.1 Release Notes

dzikoysk commented 4 months ago

I don' think we need this :thinking: If you only care about a development mode, you can just comment out this line:

And you'll get all statements from Exposed in debug mode.

solonovamax commented 3 months ago

@dzikoysk it was more for diagnosing an issue

but it'd be nice to not have to recompile it in order to see the debug logs