zerologbun

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: MIT Imports: 8 Imported by: 1

README

zerologbun

A simple hook for bun that enables logging with zerolog

go get github.com/sheb-gregor/zerologbun

Usage

db := bun.NewDB(...)
log := zerolog.New(zerolog.NewConsoleWriter())
db.AddQueryHook(NewQueryHook(QueryHookOptions{Logger: log}))

QueryHookOptions
  • LogSlow time.Duration value of queries considered 'slow'
  • Logger logger following zerolog.FieldLogger interface
  • QueryLevel zerolog.Level for logging queries, eg: QueryLevel: zerolog.DebugLevel
  • SlowLevel zerolog.Level for logging slow queries
  • ErrorLevel zerolog.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(QueryHookOptions{
    LogSlow:    time.Second,
    Logger:     log,
    QueryLevel: zerolog.DebugLevel,
    ErrorLevel: zerolog.ErrorLevel,
    SlowLevel:  zerolog.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 to template

type QueryHook

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

QueryHook wraps query hook

func NewQueryHook

func NewQueryHook(opts QueryHookOptions) *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 zerolog 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          zerolog.Logger
	QueryLevel      zerolog.Level
	SlowLevel       zerolog.Level
	ErrorLevel      zerolog.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