Documentation
¶
Overview ¶
Package sqldblogger act as thin and transparent logger without having to change existing *sql.DB usage.
Index ¶
- func OpenDriver(dsn string, drv driver.Driver, lg Logger, opt ...Option) *sql.DB
- type DurationUnit
- type Level
- type Logger
- type NullUID
- type Option
- func WithConnectionIDFieldname(name string) Option
- func WithDurationFieldname(name string) Option
- func WithDurationUnit(unit DurationUnit) Option
- func WithErrorFieldname(name string) Option
- func WithExecerLevel(lvl Level) Option
- func WithIncludeStartTime(flag bool) Option
- func WithLogArguments(flag bool) Option
- func WithLogDriverErrorSkip(flag bool) Option
- func WithMinimumLevel(lvl Level) Option
- func WithPreparerLevel(lvl Level) Option
- func WithQueryerLevel(lvl Level) Option
- func WithSQLArgsFieldname(name string) Option
- func WithSQLQueryAsMessage(flag bool) Option
- func WithSQLQueryFieldname(name string) Option
- func WithStartTimeFieldname(name string) Option
- func WithStatementIDFieldname(name string) Option
- func WithTimeFieldname(name string) Option
- func WithTimeFormat(format TimeFormat) Option
- func WithTransactionIDFieldname(name string) Option
- func WithUIDGenerator(gen UIDGenerator) Option
- func WithWrapResult(flag bool) Option
- type TimeFormat
- type UIDGenerator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DurationUnit ¶
type DurationUnit uint8
DurationUnit is total time spent on an actual driver function call calculated by time.Since(start).
const ( // DurationNanosecond will format time.Since() result to nanosecond unit (1/1_000_000_000 second). DurationNanosecond DurationUnit = iota // DurationMicrosecond will format time.Since() result to microsecond unit (1/1_000_000 second). DurationMicrosecond // DurationMillisecond will format time.Since() result to millisecond unit (1/1_000 second). DurationMillisecond )
type Level ¶
type Level uint8
Level is a log level which filterable by minimum level option.
const ( // LevelTrace is the lowest level and the most detailed. // Use this if you want to know interaction flow from prepare, statement, execution to result/rows. LevelTrace Level = iota // LevelDebug is used by non Queryer(Context) and Execer(Context) call like Ping() and Connect(). LevelDebug // LevelInfo is used by Queryer, Execer, Preparer, and Stmt. LevelInfo // LevelError is used on actual driver error or when driver not implement some optional sql/driver interface. LevelError )
type Logger ¶
type Logger interface {
Log(ctx context.Context, level Level, msg string, data map[string]interface{})
}
Logger interface copied from: https://github.com/jackc/pgx/blob/f3a3ee1a0e5c8fc8991928bcd06fdbcd1ee9d05c/logger.go#L46-L49
type NullUID ¶
type NullUID struct{}
NullUID is used to disable unique id when set to WithUIDGenerator().
type Option ¶
type Option func(*options)
Option is optional variadic type in OpenDriver().
func WithConnectionIDFieldname ¶
WithConnectionIDFieldname to customize connection ID fieldname on log output.
Default: "conn_id"
func WithDurationFieldname ¶
WithDurationFieldname to customize duration fieldname on log output.
Default: "duration"
func WithDurationUnit ¶
func WithDurationUnit(unit DurationUnit) Option
WithDurationUnit to customize log duration unit.
Options: DurationMillisecond | DurationMicrosecond | DurationNanosecond
Default: DurationMillisecond
func WithErrorFieldname ¶
WithErrorFieldname to customize error fieldname on log output.
Default: "error"
func WithExecerLevel ¶
WithExecerLevel set default level of Exec(Context) method calls.
Default: LevelInfo
func WithIncludeStartTime ¶
WithIncludeStartTime flag to include actual start time before actual driver execution.
Can be useful if we want to combine Log implementation with tracing from context and set start time span manually.
Default: false
func WithLogArguments ¶
WithLogArguments set flag to log SQL query argument or not.
When set to false, any SQL and result/rows argument on Queryer(Context) and Execer(Context) will not logged.
When set to true, argument type string and []byte will subject to trim on parseArgs() log output.
Default: true
func WithLogDriverErrorSkip ¶
WithLogDriverErrorSkip set flag for driver.ErrSkip.
If driver not implement optional interfaces, driver will return driver.ErrSkip and sql.DB will handle that. driver.ErrSkip could be false alarm in log analyzer because it was not actual error from app.
When set to false, logger will log any driver.ErrSkip.
Default: true
func WithMinimumLevel ¶
WithMinimumLevel set minimum level to be logged. Logger will always log level >= minimum level.
Options: LevelTrace < LevelDebug < LevelInfo < LevelError
Default: LevelDebug
func WithPreparerLevel ¶
WithPreparerLevel set default level of Prepare(Context) method calls.
Default: LevelInfo
func WithQueryerLevel ¶
WithQueryerLevel set default level of Query(Context) method calls.
Default: LevelInfo
func WithSQLArgsFieldname ¶
WithSQLArgsFieldname to customize SQL query arguments fieldname on log output.
Default: "args"
func WithSQLQueryAsMessage ¶
WithSQLQueryAsMessage set SQL query as message in log output (only for function call with SQL query).
Default: false
func WithSQLQueryFieldname ¶
WithSQLQueryFieldname to customize SQL query fieldname on log output.
Default: "query"
func WithStartTimeFieldname ¶
WithStartTimeFieldname to customize start time fieldname on log output.
If WithIncludeStartTime true, start time fieldname will use this value.
Default: "start"
func WithStatementIDFieldname ¶
WithStatementIDFieldname to customize prepared statement ID fieldname on log output.
Default: "stmt_id"
func WithTimeFieldname ¶
WithTimeFieldname to customize log timestamp fieldname on log output.
Default: "time"
func WithTimeFormat ¶
func WithTimeFormat(format TimeFormat) Option
WithTimeFormat to customize log time format.
Options: TimeFormatUnix | TimeFormatUnixNano | TimeFormatRFC3339 | TimeFormatRFC3339Nano
Default: TimeFormatUnix
func WithTransactionIDFieldname ¶
WithTransactionIDFieldname to customize database transaction ID fieldname on log output.
Default: "tx_id"
func WithUIDGenerator ¶
func WithUIDGenerator(gen UIDGenerator) Option
WithUIDGenerator set custom unique id generator for context call (connection, statement, transaction).
To disable unique id in log output, use &NullUID{}.
Default: newDefaultUIDDGenerator() called from setDefaultOptions().
func WithWrapResult ¶
WithWrapResult set flag to wrap Queryer(Context) and Execer(Context) driver.Rows/driver.Result response.
When set to false, result returned from db (driver.Rows/driver.Result object), will returned as is without wrapped inside &rows{} and &result{}.
Default: true
type TimeFormat ¶
type TimeFormat uint8
TimeFormat is time.Now() format when Log() deliver the log message.
const ( // TimeFormatUnix will format log time to unix timestamp. TimeFormatUnix TimeFormat = iota // TimeFormatUnixNano will format log time to unix timestamp with nano seconds. TimeFormatUnixNano // TimeFormatRFC3339 will format log time to time.RFC3339 format. TimeFormatRFC3339 // TimeFormatRFC3339Nano will format log time to time.RFC3339Nano format. TimeFormatRFC3339Nano )
type UIDGenerator ¶
type UIDGenerator interface {
UniqueID() string
}
UIDGenerator is an interface to generate unique ID for context call (connection, statement, transaction). The point of having unique id per context call is to easily track and analyze logs.
Note: no possible way to track id when statement Execer(Context),Queryer(Context) called from under db.Tx.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
logadapter
|
|
logrusadapter
Module
|
|
onelogadapter
Module
|
|
zapadapter
Module
|
|
zerologadapter
Module
|