zerobun

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

README

zerobun

Zerobun is a query hook for uptrace/bun that logs with rs/zerolog

go get -u github.com/comsma/zerobun

Usage

package main

import (
	"os"

	"github.com/rs/zerolog"
	"github.com/uptrace/bun"
)

func main() {
	logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
	db := bun.NewDb()
	db.AddQueryHook(
		zerobun.NewQueryHook(
			zerobun.QueryHookOptions{
				SlowDuration: 5,
				Logger:       &logger,
			},
		),
	)
}

Example Output

{ "level":"error", 
  "bun_info":{
    "operation":"SELECT",
    "operation_time_ms":0,
    "query":"SELECT * FROM products WHERE ID = 5",
  },
  "error":"database error",
  "time":"2023-11-15T16:12:24-05:00"}

Documentation

Index

Constants

View Source
const (
	BunInfoFieldName       = "bun_info"
	OperationFieldName     = "operation"
	OperationQueryName     = "query"
	OperationTimeFieldName = "operation_time_ms"
)

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

NewQueryHook returns a new bun.QueryHook that outputs database transactions using an instance of zerolog.Logger

func (QueryHook) AfterQuery

func (qh QueryHook) AfterQuery(_ context.Context, event *bun.QueryEvent)

AfterQuery logs an entry after a database transaction

func (QueryHook) BeforeQuery

func (qh QueryHook) BeforeQuery(ctx context.Context, _ *bun.QueryEvent) context.Context

BeforeQuery called before a database transaction but currently does nothing

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