bunzerolog

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2024 License: MIT Imports: 4 Imported by: 0

README

bun-zerolog

A query hook for uptrace/bun that logs with rs/zerolog.

go get github.com/oh0123/bun-zerolog

With the hook added, All errors will be logged at error level. And if you want log everything, please set zerolog.SetGlobalLevel(zerolog.DebugLevel). If SlowDuration is defined, only operations that taking longer than the defined duration will be logged.

Usage

import (
    "os"

    bunzerolog "github.com/oh0123/bun-zerolog"
    "github.com/rs/zerolog"
)

// zerolog.SetGlobalLevel(zerolog.DebugLevel)

db := bun.NewDB()
log := zerolog.New(os.Stdout).With().Logger()
db.AddQueryHook(bunzerolog.NewQueryHook(bunzerolog.QueryHookOptions{
    Logger:         log,
    SlowDuration:   100 * time.Millisecond,
}))

Documentation

Index

Constants

View Source
const (
	SqlFieldName            = "query"
	SqlArgsFieldName        = "args"
	SqlExecuteTimeFieldName = "elapse"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type QueryHook

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

func NewQueryHook

func NewQueryHook(options QueryHookOptions) QueryHook

func (QueryHook) AfterQuery

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

func (QueryHook) BeforeQuery

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

type QueryHookOptions

type QueryHookOptions struct {
	Logger       zerolog.Logger
	SlowDuration time.Duration
}

Jump to

Keyboard shortcuts

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