Documentation
¶
Overview ¶
sqlslog is a logger for Go SQL database drivers without modifying existing *sql.DB stdlib usage. sqlslog uses *slog.Logger to log SQL database driver operations.
How to use ¶
db, logger, err := sqlslog.Open(ctx, "mysql", dsn)
You can also use options to customize the logger's behavior.
Open takes Option s to customize the logging behavior. Option is created by using functions like HandlerFunc, ConnPrepareContext, StmtQueryContext, etc.
HandlerFunc / Handler ¶
HandlerFunc sets the function to create a slog.Handler for the logger. slogslog provides NewTextHandler and NewJSONHandler to create a slog.Handler for sqlslog.
You can also use your own slog.Handler by using Handler with your slog.Handler. But your own slog.Handler should know how to log LevelTrace and LevelVerbose log levels. So you should use sqlslog.ReplaceLevelAttr with your slog.Handler.
Level ¶
sqlslog has 6 log levels: LevelVerbose, LevelTrace, LevelDebug, LevelInfo, LevelWarn, and LevelError. LevelDebug, LevelInfo, LevelWarn, and LevelError are the same as slog's log levels. LevelVerbose and LevelTrace are extra log levels for sqlslog. LevelVerbose is the lowest log level, and LevelTrace is the second lowest log level.
Step and Event ¶
A Step is a logical operation in the database driver, such as a query, a ping, a prepare, etc. An Event is an event that occurs during a Step, such as EventStart, EventError, and EventComplete. A StepOptions is a set of options for logging a Step and has EventOptions for each event. sqlslog provides a way to customize the log message and log Level for each step event. You can customize them by using functions that take StepOptions and return Option, like ConnPrepareContext or StmtQueryContext.
DefaultStepEventMsgBuilder ¶
The default step event message builder is StepEventMsgWithEventName. You can change the default step event message builder by calling SetStepEventMsgBuilder.
Duration ¶
sqlslog measures the duration of each step and logs it. You can change the duration unit by calling Duration function and can change the key name of the duration by calling DurationKey function.
Tracking ID ¶
sqlslog provides a way to track connections, transactions and statements by using a tracking ID. Each tracking ID is a unique identifier for a connection, transaction or statement. Tracking ID key in logs is conn_id, tx_id or stmt_id. Tracking IDs are generated by the ID generator function. The default ID generator function is IDGeneratorDefault. You can change the ID generator function by calling IDGenerator with functions created by RandIntIDGenerator or RandReadIDGenerator with IDGenErrorSuppressor.
Index ¶
- Constants
- Variables
- func ConnExecContextErrorHandler(driverName string) func(err error) (bool, []slog.Attr)
- func ConnQueryContextErrorHandler(driverName string) func(err error) (bool, []slog.Attr)
- func ConnectorConnectErrorHandler(driverName string) func(err error) (bool, []slog.Attr)
- func DriverOpenErrorHandler(driverName string) func(err error) (bool, []slog.Attr)
- func HandleRowsNextError(err error) (bool, []slog.Attr)
- func NewJSONHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- func NewTextHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- func Open(ctx context.Context, driverName, dsn string, opts ...Option) (*sql.DB, *slog.Logger, error)
- func RandReadIDGenerator(randRead func(b []byte) (n int, err error), letters []byte, length int) func() (string, error)
- func ReplaceLevelAttr(_ []string, a slog.Attr) slog.Attr
- func SetStepEventMsgBuilder(f StepEventMsgBuilder)
- func StepEventMsgWithEventName(step Step, event Event) string
- func StepEventMsgWithoutEventName(step Step, _ Event) string
- func WrapHandlerOptions(opts *slog.HandlerOptions) *slog.HandlerOptions
- type DurationType
- type Event
- type EventOptions
- type Factory
- type IDGen
- type Level
- type Option
- func AddSource(v bool) Option
- func ConnBegin(f func(*StepOptions)) Option
- func ConnBeginTx(f func(*StepOptions)) Option
- func ConnClose(f func(*StepOptions)) Option
- func ConnExecContext(f func(*StepOptions)) Option
- func ConnIDKey(key string) Option
- func ConnPing(f func(*StepOptions)) Option
- func ConnPrepare(f func(*StepOptions)) Option
- func ConnPrepareContext(f func(*StepOptions)) Option
- func ConnQueryContext(f func(*StepOptions)) Option
- func ConnResetSession(f func(*StepOptions)) Option
- func ConnectorConnect(f func(*StepOptions)) Option
- func DriverOpen(f func(*StepOptions)) Option
- func DriverOpenConnector(f func(*StepOptions)) Option
- func Duration(v DurationType) Option
- func DurationKey(key string) Option
- func Handler(handler slog.Handler) Option
- func HandlerFunc(handlerFunc func(io.Writer, *slog.HandlerOptions) slog.Handler) Option
- func HandlerOptions(opts *slog.HandlerOptions) Option
- func IDGenerator(idGen IDGen) Option
- func LogLevel(v slog.Leveler) Option
- func LogReplaceAttr(f func([]string, slog.Attr) slog.Attr) Option
- func LogWriter(w io.Writer) Option
- func RowsClose(f func(*StepOptions)) Option
- func RowsNext(f func(*StepOptions)) Option
- func RowsNextResultSet(f func(*StepOptions)) Option
- func SqlslogOpen(f func(*StepOptions)) Option
- func StmtClose(f func(*StepOptions)) Option
- func StmtExec(f func(*StepOptions)) Option
- func StmtExecContext(f func(*StepOptions)) Option
- func StmtIDKey(key string) Option
- func StmtQuery(f func(*StepOptions)) Option
- func StmtQueryContext(f func(*StepOptions)) Option
- func TxCommit(f func(*StepOptions)) Option
- func TxIDKey(key string) Option
- func TxRollback(f func(*StepOptions)) Option
- type ReplaceAttrFunc
- type Step
- type StepEventMsgBuilder
- type StepOptions
Examples ¶
Constants ¶
const ( ConnIDKeyDefault = "conn_id" TxIDKeyDefault = "tx_id" StmtIDKeyDefault = "stmt_id" )
const DurationKeyDefault = "duration"
DurationKeyDefault is the default key for duration value in log.
Variables ¶
var ErrUnknownLevel = errors.New("unknown level")
var IDGeneratorDefault = RandIntIDGenerator(rand.Int, defaultIDLetters, defaultIDLength)
IDGeneratorDefault is the default ID generator.
Functions ¶
func ConnExecContextErrorHandler ¶ added in v0.1.2
func ConnQueryContextErrorHandler ¶ added in v0.1.2
func ConnectorConnectErrorHandler ¶ added in v0.1.2
ConnectorConnectErrorHandler returns a function that handles errors from driver.Connector.Connect. The function returns a boolean indicating completion and a slice of slog.Attr.
# For Postgres: If err is nil, it returns true and a slice of slog.Attr{slog.Bool("success", true)}. If err is io.EOF, it returns true and a slice of slog.Attr{slog.Bool("success", false)}. Otherwise, it returns false and nil.
func DriverOpenErrorHandler ¶ added in v0.1.2
DriverOpenErrorHandler returns a function that handles errors from driver.Driver.Open. The function returns a boolean indicating completion and a slice of slog.Attr.
# For Postgres: If err is nil, it returns true and a slice of slog.Attr{slog.Bool("success", true)}. If err is io.EOF, it returns true and a slice of slog.Attr{slog.Bool("success", false)}. Otherwise, it returns false and nil.
func HandleRowsNextError ¶ added in v0.1.2
HandleRowsNextError returns a boolean indicating completion and a slice of slog.Attr. If err is nil, it returns true and a slice of slog.Attr{slog.Bool("eof", false)}. If err is io.EOF, it returns true and a slice of slog.Attr{slog.Bool("eof", true)}. Otherwise, it returns false and nil.
func NewJSONHandler ¶ added in v0.1.1
NewJSONHandler returns a new JSON handler using slog.NewJSONHandler with custom options for sqlslog. See WrapHandlerOptions for details on the options.
Example ¶
package main import ( "context" "log/slog" sqlslog "github.com/akm/sql-slog" ) func removeTimeAndDuration(groups []string, a slog.Attr) slog.Attr { if len(groups) == 0 { switch a.Key { case slog.TimeKey: return slog.Attr{} case "duration": return slog.Attr{} } } return a } func main() { dsn := "dummy-dsn" ctx := context.TODO() db, logger, _ := sqlslog.Open(ctx, "mock", dsn, sqlslog.HandlerFunc(sqlslog.NewJSONHandler), sqlslog.LogReplaceAttr(removeTimeAndDuration), ) defer db.Close() logger.InfoContext(ctx, "Hello, World!") }
Output: {"level":"INFO","msg":"Open","driver":"mock","dsn":"dummy-dsn"} {"level":"INFO","msg":"Hello, World!"}
func NewTextHandler ¶ added in v0.1.1
NewTextHandler returns a new Text handler using slog.NewTextHandler with custom options for sqlslog. See WrapHandlerOptions for details on the options.
Example ¶
package main import ( "context" "log/slog" sqlslog "github.com/akm/sql-slog" ) func removeTimeAndDuration(groups []string, a slog.Attr) slog.Attr { if len(groups) == 0 { switch a.Key { case slog.TimeKey: return slog.Attr{} case "duration": return slog.Attr{} } } return a } func main() { dsn := "dummy-dsn" ctx := context.TODO() db, logger, _ := sqlslog.Open(ctx, "mock", dsn, sqlslog.HandlerFunc(sqlslog.NewTextHandler), sqlslog.LogReplaceAttr(removeTimeAndDuration), ) defer db.Close() logger.InfoContext(ctx, "Hello, World!") }
Output: level=INFO msg=Open driver=mock dsn=dummy-dsn level=INFO msg="Hello, World!"
func Open ¶
func Open(ctx context.Context, driverName, dsn string, opts ...Option) (*sql.DB, *slog.Logger, error)
Open opens a database specified by its driver name and a driver-specific data source name, and returns a new database handle with logging capabilities.
ctx is the context for the open operation. driverName is the name of the database driver, same as the driverName in sql.Open. dsn is the data source name, same as the dataSourceName in sql.Open. opts are the options for logging behavior. See Option for details.
The returned DB can be used the same way as *sql.DB from sql.Open.
See the following example for usage:
[Logger]: sets the slog.Logger to be used. If not set, the default is slog.Default().
StepOptions: sets the options for logging behavior.
SetStepEventMsgBuilder: sets the function to format the step name.
Example ¶
package main import ( "context" sqlslog "github.com/akm/sql-slog" ) func main() { dsn := "file::memory:?cache=shared" ctx := context.TODO() db, logger, err := sqlslog.Open(ctx, "sqlite3", dsn) if err != nil { // Handle error } defer db.Close() // Use db as a regular *sql.DB logger.InfoContext(ctx, "Hello, World!") }
Output:
Example (WithLevel) ¶
package main import ( "context" sqlslog "github.com/akm/sql-slog" ) func main() { dsn := "file::memory:?cache=shared" ctx := context.TODO() db, _, _ := sqlslog.Open(ctx, "sqlite3", dsn, sqlslog.LogLevel(sqlslog.LevelTrace), ) defer db.Close() }
Output:
Example (WithStmtQueryContext) ¶
package main import ( "context" sqlslog "github.com/akm/sql-slog" ) func main() { dsn := "file::memory:?cache=shared" ctx := context.TODO() db, _, _ := sqlslog.Open(ctx, "sqlite3", dsn, sqlslog.LogLevel(sqlslog.LevelTrace), sqlslog.StmtQueryContext(func(o *sqlslog.StepOptions) { o.SetLevel(sqlslog.LevelDebug) }), ) defer db.Close() }
Output:
func RandReadIDGenerator ¶ added in v0.2.0
func RandReadIDGenerator( randRead func(b []byte) (n int, err error), letters []byte, length int, ) func() (string, error)
Returns a random ID generator that generates a string of length characters using randRead to generate random bytes such as Read function from crypto/rand package.
func ReplaceLevelAttr ¶ added in v0.1.1
ReplaceLevelAttr replaces the log level as sqlslog.Level with its string representation.
func SetStepEventMsgBuilder ¶ added in v0.2.0
func SetStepEventMsgBuilder(f StepEventMsgBuilder)
SetStepEventMsgBuilder sets the builder for the step event message used in logs. If not set, the default is StepLogMsgWithEventName.
Example ¶
sqlslog.SetStepEventMsgBuilder(func(step sqlslog.Step, event sqlslog.Event) string { return "PRFIX:" + step.String() + "/" + event.String() + ":SUFFIX" }) defer sqlslog.SetStepEventMsgBuilder(sqlslog.StepEventMsgWithoutEventName) dsn := "dummy-dsn" ctx := context.TODO() db, logger, _ := sqlslog.Open(ctx, "mock", dsn, sqlslog.LogReplaceAttr(removeTimeAndDuration), // for testing ) defer db.Close() logger.InfoContext(ctx, "Hello, World!")
Output: level=INFO msg=PRFIX:Open/Complete:SUFFIX driver=mock dsn=dummy-dsn level=INFO msg="Hello, World!"
func StepEventMsgWithEventName ¶ added in v0.2.0
StepEventMsgWithEventName returns the formatted step log message with the event name.
func StepEventMsgWithoutEventName ¶ added in v0.2.0
StepEventMsgWithoutEventName returns the formatted step log message without the event name.
func WrapHandlerOptions ¶ added in v0.1.1
func WrapHandlerOptions(opts *slog.HandlerOptions) *slog.HandlerOptions
WrapHandlerOptions wraps the options with custom options for sqlslog. It merges ReplaceAttr functions with ReplaceLevelAttr.
Types ¶
type DurationType ¶ added in v0.1.3
type DurationType int
const ( DurationNanoSeconds DurationType = iota // Duration in nanoseconds. Durations in log are expressed by slog.Int64 DurationMicroSeconds // Duration in microseconds. Durations in log are expressed by slog.Int64 DurationMilliSeconds // Duration in milliseconds. Durations in log are expressed by slog.Int64 DurationGoDuration // Values in log are expressed with slog.Duration DurationString // Values in log are expressed with slog.String and time.Duration.String )
type EventOptions ¶ added in v0.1.1
type IDGen ¶ added in v0.1.4
type IDGen = func() string
IDGen is a function that generates an ID string.
func IDGenErrorSuppressor ¶ added in v0.2.0
IDGenErrorSuppressor returns an ID generator that suppresses errors. If an error occurs, the recover function is called with the error and the result is returned.
Example ¶
package main import ( "context" cryptorand "crypto/rand" sqlslog "github.com/akm/sql-slog" ) func main() { idGen := sqlslog.IDGenErrorSuppressor( sqlslog.RandReadIDGenerator( cryptorand.Read, []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 8, ), func(error) string { return "recovered" }, ) db, _, _ := sqlslog.Open(context.TODO(), "sqlite3", "file::memory:?cache=shared", sqlslog.HandlerFunc(sqlslog.NewJSONHandler), sqlslog.IDGenerator(idGen), ) defer db.Close() }
Output:
func RandIntIDGenerator ¶ added in v0.2.0
Returns a random ID generator that generates a string of length characters using randInt to generate random integers such as Int function from math/rand/v2 package.
Example ¶
package main import ( "context" mathrandv2 "math/rand/v2" sqlslog "github.com/akm/sql-slog" ) func main() { idGen := sqlslog.RandIntIDGenerator( mathrandv2.Int, []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 8, ) db, _, _ := sqlslog.Open(context.TODO(), "sqlite3", "file::memory:?cache=shared", sqlslog.HandlerFunc(sqlslog.NewJSONHandler), sqlslog.IDGenerator(idGen), ) defer db.Close() }
Output:
type Level ¶ added in v0.1.1
Level is the log level for sqlslog.
const ( LevelVerbose Level = Level(-12) // Lower than slog.LevelTrace. LevelTrace Level = Level(-8) // Lower than slog.LevelDebug. LevelDebug Level = Level(slog.LevelDebug) // Same as slog.LevelDebug. LevelInfo Level = Level(slog.LevelInfo) // Same as slog.LevelInfo. LevelWarn Level = Level(slog.LevelWarn) // Same as slog.LevelWarn. LevelError Level = Level(slog.LevelError) // Same as slog.LevelError. )
func ParseLevel ¶ added in v0.2.0
func ParseLevelWithDefault ¶ added in v0.2.0
type Option ¶ added in v0.1.1
type Option func(*options)
Option is a function that sets an option on the options struct.
func ConnBegin ¶ added in v0.1.1
func ConnBegin(f func(*StepOptions)) Option
Set the options for Conn.Begin.
func ConnBeginTx ¶ added in v0.1.1
func ConnBeginTx(f func(*StepOptions)) Option
Set the options for Conn.BeginTx.
func ConnClose ¶ added in v0.1.1
func ConnClose(f func(*StepOptions)) Option
Set the options for Conn.Close.
func ConnExecContext ¶ added in v0.1.1
func ConnExecContext(f func(*StepOptions)) Option
Set the options for Conn.ExecContext.
func ConnIDKey ¶ added in v0.1.4
ConnIDKey sets the key for the connection ID. The default is ConnIDKeyDefault.
func ConnPing ¶ added in v0.1.1
func ConnPing(f func(*StepOptions)) Option
Set the options for Conn.Ping.
func ConnPrepare ¶ added in v0.1.1
func ConnPrepare(f func(*StepOptions)) Option
Set the options for Conn.Prepare.
func ConnPrepareContext ¶ added in v0.1.1
func ConnPrepareContext(f func(*StepOptions)) Option
Set the options for Conn.PrepareContext.
func ConnQueryContext ¶ added in v0.1.1
func ConnQueryContext(f func(*StepOptions)) Option
Set the options for Conn.QueryContext.
func ConnResetSession ¶ added in v0.1.1
func ConnResetSession(f func(*StepOptions)) Option
Set the options for Conn.ResetSession.
func ConnectorConnect ¶ added in v0.1.1
func ConnectorConnect(f func(*StepOptions)) Option
Set the options for Connector.Connect.
func DriverOpen ¶ added in v0.1.1
func DriverOpen(f func(*StepOptions)) Option
Set the options for Driver.Open.
func DriverOpenConnector ¶ added in v0.1.1
func DriverOpenConnector(f func(*StepOptions)) Option
Set the options for Driver.OpenConnector.
func Duration ¶ added in v0.1.3
func Duration(v DurationType) Option
Duration is an option to specify duration value in log. The default is DurationNanoSeconds.
func DurationKey ¶ added in v0.1.3
DurationKey is an option to specify the key for duration value in log. The default is specified by DurationKeyDefault.
func Handler ¶ added in v0.2.0
Handler sets the slog.Handler to be used. If not set, the default is created by HandlerFunc, Writer, SlogOptions. If you set this option, HandlerFunc, Writer, SlogOptions will be ignored. WARNING: If given handler is created without ReplaceAttr options, LevelTrace and LevelVerbose will be logged as DEBUG-4 and DEBUG-8.
Example ¶
package main import ( "context" "log/slog" "os" sqlslog "github.com/akm/sql-slog" ) func removeTimeAndDuration(groups []string, a slog.Attr) slog.Attr { if len(groups) == 0 { switch a.Key { case slog.TimeKey: return slog.Attr{} case "duration": return slog.Attr{} } } return a } func main() { // Normal slog.Handler with sqlslog.ReplaceLevelAttr knows how to show LevelTrace and LevelVerbose. handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ ReplaceAttr: sqlslog.MergeReplaceAttrs( sqlslog.ReplaceLevelAttr, removeTimeAndDuration, // for testing ), Level: sqlslog.LevelVerbose, }) dsn := "dummy-dsn" ctx := context.TODO() logger := sqlslog.New("mock", dsn, sqlslog.Handler(handler)).Logger() logger.Log(ctx, slog.Level(sqlslog.LevelTrace), "Foo") logger.Log(ctx, slog.Level(sqlslog.LevelVerbose), "Bar") }
Output: level=TRACE msg=Foo level=VERBOSE msg=Bar
Example (WithoutReplaceLevelAttr) ¶
package main import ( "context" "log/slog" "os" sqlslog "github.com/akm/sql-slog" ) func removeTimeAndDuration(groups []string, a slog.Attr) slog.Attr { if len(groups) == 0 { switch a.Key { case slog.TimeKey: return slog.Attr{} case "duration": return slog.Attr{} } } return a } func main() { // Normal slog.Handler without sqlslog.ReplaceLevelAttr does not know how to show LevelTrace and LevelVerbose. handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ ReplaceAttr: removeTimeAndDuration, Level: sqlslog.LevelVerbose, }) dsn := "dummy-dsn" ctx := context.TODO() logger := sqlslog.New("mock", dsn, sqlslog.Handler(handler)).Logger() logger.Log(ctx, slog.Level(sqlslog.LevelTrace), "Foo") logger.Log(ctx, slog.Level(sqlslog.LevelVerbose), "Bar") }
Output: level=DEBUG-4 msg=Foo level=DEBUG-8 msg=Bar
func HandlerFunc ¶ added in v0.2.0
HandlerFunc sets the function to create the slog.Handler. If not set, the default is NewTextHandler. WARNING: Unless given handlerFunc considers ReplaceAttr options like NewJSONHandler or NewTextHandler of sqlslog package, LevelTrace and LevelVerbose will be logged as DEBUG-4 and DEBUG-8.
Example ¶
package main import ( "context" sqlslog "github.com/akm/sql-slog" ) func main() { dsn := "file::memory:?cache=shared" ctx := context.TODO() db, logger, _ := sqlslog.Open(ctx, "sqlite3", dsn, sqlslog.HandlerFunc(sqlslog.NewJSONHandler), ) defer db.Close() logger.InfoContext(ctx, "Hello, World!") }
Output:
func HandlerOptions ¶ added in v0.2.0
func HandlerOptions(opts *slog.HandlerOptions) Option
HandlerOptions sets the options to be used for the slog.Handler. If not set, the default is an empty slog.HandlerOptions.
func IDGenerator ¶ added in v0.1.4
IDGenerator returns an Option that sets the ID generator. The default is IDGeneratorDefault.
Example ¶
package main import ( "context" mathrandv2 "math/rand/v2" sqlslog "github.com/akm/sql-slog" ) func main() { idGen := sqlslog.RandIntIDGenerator( mathrandv2.Int, []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 8, ) db, _, _ := sqlslog.Open(context.TODO(), "sqlite3", "file::memory:?cache=shared", sqlslog.HandlerFunc(sqlslog.NewJSONHandler), sqlslog.IDGenerator(idGen), ) defer db.Close() }
Output:
func LogReplaceAttr ¶ added in v0.2.0
ReplaceAttr sets the function to replace the attributes.
func LogWriter ¶ added in v0.2.0
LogWriter sets the writer to be used for the slog.Handler. If not set, the default is os.Stdout.
func RowsClose ¶ added in v0.1.1
func RowsClose(f func(*StepOptions)) Option
Set the options for Rows.Close.
func RowsNext ¶ added in v0.1.1
func RowsNext(f func(*StepOptions)) Option
Set the options for Rows.Next.
func RowsNextResultSet ¶ added in v0.1.1
func RowsNextResultSet(f func(*StepOptions)) Option
Set the options for Rows.NextResultSet.
func SqlslogOpen ¶ added in v0.1.1
func SqlslogOpen(f func(*StepOptions)) Option
Set the options for sqlslog.Open.
func StmtClose ¶ added in v0.1.1
func StmtClose(f func(*StepOptions)) Option
Set the options for Stmt.Close.
func StmtExec ¶ added in v0.1.1
func StmtExec(f func(*StepOptions)) Option
Set the options for Stmt.Exec.
func StmtExecContext ¶ added in v0.1.1
func StmtExecContext(f func(*StepOptions)) Option
Set the options for Stmt.ExecContext.
func StmtIDKey ¶ added in v0.1.4
StmtIDKey sets the key for the statement ID. The default is StmtIDKeyDefault.
func StmtQuery ¶ added in v0.1.1
func StmtQuery(f func(*StepOptions)) Option
Set the options for Stmt.Query.
func StmtQueryContext ¶ added in v0.1.1
func StmtQueryContext(f func(*StepOptions)) Option
Set the options for Stmt.QueryContext.
func TxCommit ¶ added in v0.1.1
func TxCommit(f func(*StepOptions)) Option
Set the options for Tx.Commit.
func TxIDKey ¶ added in v0.1.4
TxIDKey sets the key for the transaction ID. The default is TxIDKeyDefault.
func TxRollback ¶ added in v0.1.1
func TxRollback(f func(*StepOptions)) Option
Set the options for Tx.Rollback.
type ReplaceAttrFunc ¶ added in v0.1.1
ReplaceLevelAttr is a type of ReplaceAttr for slog.HandlerOptions.
func MergeReplaceAttrs ¶ added in v0.1.1
func MergeReplaceAttrs(funcs ...ReplaceAttrFunc) ReplaceAttrFunc
MergeReplaceAttrs merges multiple ReplaceAttrFunc functions. If functions are nil or empty, it returns nil. If there is only one function, it returns that function. If there are multiple functions, it returns a merged function.
type Step ¶ added in v0.2.0
type Step string
const ( StepConnBegin Step = "Conn.Begin" StepConnBeginTx Step = "Conn.BeginTx" StepConnClose Step = "Conn.Close" StepConnPrepare Step = "Conn.Prepare" StepConnPrepareContext Step = "Conn.PrepareContext" StepConnResetSession Step = "Conn.ResetSession" StepConnPing Step = "Conn.Ping" StepConnExecContext Step = "Conn.ExecContext" StepConnQueryContext Step = "Conn.QueryContext" StepConnectorConnect Step = "Connector.Connect" StepDriverOpen Step = "Driver.Open" StepDriverOpenConnector Step = "Driver.OpenConnector" StepSqlslogOpen Step = "Open" StepRowsClose Step = "Rows.Close" StepRowsNext Step = "Rows.Next" StepRowsNextResultSet Step = "Rows.NextResultSet" StepStmtClose Step = "Stmt.Close" StepStmtExec Step = "Stmt.Exec" StepStmtQuery Step = "Stmt.Query" StepStmtExecContext Step = "Stmt.ExecContext" StepStmtQueryContext Step = "Stmt.QueryContext" StepTxCommit Step = "Tx.Commit" StepTxRollback Step = "Tx.Rollback" )
type StepEventMsgBuilder ¶ added in v0.2.0
StepEventMsgBuilder is the function type to format the step log message.
type StepOptions ¶ added in v0.1.1
type StepOptions struct { Start EventOptions Error EventOptions Complete EventOptions // ErrorHandler is the function to handle the error. // When the error should not be logged as an error but as complete, it should return true. // It can also add attributes to the log. ErrorHandler func(error) (bool, []slog.Attr) }
StepOptions is an struct that expresses the options for the step.
func (*StepOptions) SetLevel ¶ added in v0.1.1
func (o *StepOptions) SetLevel(lv Level)