Skip to main content

Logging

ant.WithLogger logs every statement through log/slog.

Log statements​

logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelDebug,
}))

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn, ant.WithLogger(logger))

Each line carries the SQL, the duration and the number of arguments. Statements log at debug, slow ones at warning, and failures at error.

Slow queries​

Statements at or over SlowThreshold log at warning. Without it, none do.

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn,
ant.WithLogger(logger, ant.SlowThreshold(200*time.Millisecond)),
)

Log values​

ant.LogValues adds the argument values to each line: what a pointer holds, or nil, and what a driver.Valuer gives. Use it in development only, since values can be passwords or personal data.

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn,
ant.WithLogger(logger, ant.LogValues()),
)

Keep a column out of the logs​

.Sensitive() marks a column so its value is never logged, even with LogValues, on inserts, updates, Set, UpdateMany, Values and conditions, however a condition is built: ant.Eq, ant.Col(&f).Eq, ant.Lower(&f).Eq, a value passed to ant.SQL with the column, InTuples, or the seek values of After and ChunkByKey. It logs as [redacted].

type Account struct {
ID int64
Password string
}

func (Account) Configure(b *ant.ModelBuilder[Account]) {
b.Column(&Accounts.Password).Sensitive()
}

var Accounts = ant.Fields[Account]()
Limits

ant.SQL arguments are not covered by .Sensitive(), because ant cannot tell which column they belong to. A database error's message can also quote a value, so do not return err.Error() to users.