logrusbun

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 21, 2026 License: MIT Imports: 10 Imported by: 13

README

logrusbun

A simple hook for bun that enables logging with logrus

go get github.com/oiime/logrusbun

Usage

db := bun.NewDB(...)
log := logrus.New()
db.AddQueryHook(logrusbun.NewQueryHook(logrusbun.WithQueryHookOptions(QueryHookOptions{Logger: log})))

Similar to bundebug, additional logging setup is available:

db := bun.NewDB(...)
log := logrus.New()
db.AddQueryHook(logrusbun.NewQueryHook(
    // disable the hook
    logrusbun.WithEnabled(false),

    // BUNDEBUG=1 logs failed queries
    // BUNDEBUG=2 logs all queries
    logrusbun.FromEnv("BUNDEBUG"),
    	
    // finally set logrus settings
    logrusbun.WithQueryHookOptions(QueryHookOptions{Logger: log}),
))
QueryHookOptions
  • LogSlow time.Duration value of queries considered 'slow'
  • Logger logger following logrus.FieldLogger interface
  • QueryLevel logrus.Level for logging queries, eg: QueryLevel: logrus.DebugLevel
  • SlowLevel logrus.Level for logging slow queries
  • ErrorLevel logrus.Level for logging errors
  • MessageTemplate alternative message string template, avialable variables listed below
  • ErrorTemplate alternative error string template, available variables listed below
Message template variables
  • {{.Timestamp}} Event timestmap
  • {{.Duration}} Duration of query
  • {{.Query}} Query string
  • {{.Operation}} Operation name (eg: SELECT, UPDATE...)
  • {{.Error}} Error message if available
Kitchen sink example
db.AddQueryHook(NewQueryHook(WithQueryHookOptions(QueryHookOptions{
    LogSlow:    time.Second,
    Logger:     log,
    QueryLevel: logrus.DebugLevel,
    ErrorLevel: logrus.ErrorLevel,
    SlowLevel:  logrus.WarnLevel,
    MessageTemplate: "{{.Operation}}[{{.Duration}}]: {{.Query}}",
    ErrorTemplate: "{{.Operation}}[{{.Duration}}]: {{.Query}}: {{.Error}}",
})))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogEntryVars

type LogEntryVars struct {
	Timestamp time.Time
	Query     string
	Operation string
	Duration  time.Duration
	Error     error
}

LogEntryVars variables made available t otemplate

type Option added in v0.1.2

type Option func(hook *QueryHook)

func FromEnv added in v0.1.2

func FromEnv(keys ...string) Option

FromEnv configures the hook using the environment variable value. For example, WithEnv("BUNDEBUG"):

  • BUNDEBUG=0 - disables the hook.
  • BUNDEBUG=1 - enables the hook.
  • BUNDEBUG=2 - enables the hook and verbose mode.

func WithEnabled added in v0.1.2

func WithEnabled(on bool) Option

WithEnabled enables/disables this hook

func WithQueryHookOptions added in v0.1.2

func WithQueryHookOptions(opts QueryHookOptions) Option

WithQueryHookOptions allows setting the initial logging options for logrus

func WithVerbose added in v0.1.2

func WithVerbose(on bool) Option

WithVerbose configures the hook to log all queries (by default, only failed queries are logged)

type QueryHook

type QueryHook struct {
	// contains filtered or unexported fields
}

QueryHook wraps query hook

func NewQueryHook

func NewQueryHook(options ...Option) *QueryHook

NewQueryHook returns new instance

func (*QueryHook) AfterQuery

func (h *QueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent)

AfterQuery convert a bun QueryEvent into a logrus message

func (*QueryHook) BeforeQuery

func (h *QueryHook) BeforeQuery(ctx context.Context, event *bun.QueryEvent) context.Context

BeforeQuery does nothing tbh

type QueryHookOptions

type QueryHookOptions struct {
	LogSlow         time.Duration
	Logger          logrus.FieldLogger
	QueryLevel      logrus.Level
	SlowLevel       logrus.Level
	ErrorLevel      logrus.Level
	MessageTemplate string
	ErrorTemplate   string
}

QueryHookOptions logging options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL