|
Lightweight 0.20261105.1
|
Lightweight routes every interesting database event — connections opening and closing, statements being prepared, parameters being bound, rows being fetched, warnings and errors — through a single SqlLogger interface. Installing a logger is how you see what the library is actually sending to the database, and it is the first tool to reach for when a query misbehaves on one DBMS but not another.
There is exactly one active logger process-wide. It is retrieved with SqlLogger::GetLogger() and replaced with SqlLogger::SetLogger().
| Logger | Use it for |
|---|---|
SqlLogger::NullLogger() | Discards everything. The default — logging costs nothing unless you opt in. |
SqlLogger::StandardLogger() | Warnings and errors only. A sensible production choice. |
SqlLogger::TraceLogger() | Everything: prepare, bind, execute, fetch, connection lifecycle, scoped timers. Use while diagnosing. |
Switching to full tracing is a single call:
The test binary exposes this as --trace-sql (see src/tests/Utils.hpp), which is the same switch behind a command-line flag.
Both built-in loggers write through a MessageWriter sink, which you can replace to route messages into your own logging framework instead of the default output:
Call SetLoggingSink({}) to restore the default sink.
For anything beyond redirecting text — counting queries, feeding metrics, failing a test when an unexpected statement runs — derive from SqlLogger::Null and override just the hooks you care about. Null supplies empty implementations for all of them, so you only write what you need:
Deriving from SqlLogger directly is also possible, but almost every hook is pure virtual — you would have to implement all of them.
The hooks available are:
| Hook | Fires when |
|---|---|
OnWarning / OnError | A warning or an ODBC error is raised. Two OnError overloads: one for a bare SqlError, one for a full SqlErrorInfo with SQL state and native code. |
OnStatementError | An operation on a statement failed, with that statement's own query. Forwards to OnError by default. |
OnConnectionOpened / OnConnectionClosed | A connection is established or torn down. |
OnConnectionIdle / OnConnectionReuse | A pooled connection goes idle or is handed out again. |
OnExecuteDirect | A statement is executed without preparation. |
OnPrepare / OnExecute / OnExecuteBatch | A statement is prepared, executed, or executed as a batch. |
OnExecuteNativeBatch | A prepared query is executed as one native row-wise batch, with that query. Forwards to OnExecuteBatch by default. |
OnBind | A parameter is bound. Only called when the logger was constructed with SupportBindLogging::Yes. |
OnFetchRow / OnFetchBlock / OnFetchEnd | Rows are fetched one at a time, in a prefetch block, or the result set is exhausted. |
OnScopedTimerStart / OnScopedTimerStop | A scoped timing region opens or closes. |
OnFetchBlock, OnStatementError and OnExecuteNativeBatch have default implementations, so an existing logger needs no change.
A logger is shared by every statement of the process, so a query it remembers from OnPrepare or OnExecute may belong to another statement by the time something fails. TraceLogger therefore prints Query: only for a failure it was told the query of, and Last logged query (not necessarily the failed one): otherwise. SqlException carries no query, and throwing one does not call the logger, so a failed Execute() shows up in the trace only through the OnExecute line before it.
SetLogger does not take ownership, so the logger must outlive its installation. In tests, where loggers are swapped repeatedly, an RAII guard keeps this honest:
SqlLogger hooks above, so installing a collector does not displace your logger.