bunslog

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: MIT Imports: 6 Imported by: 0

README

🥯 bunslog

A simple logger hook for bun ORM using Go's slog package. It logs SQL queries, errors, and slow queries with flexible configuration. 🚀

✨ Features

  • 🐞 Log all queries
  • 🎚️ Custom log levels for queries, errors, and slow queries
  • ⚙️ Environment variable configuration
  • 🔗 Integrates with Go's slog logger

📦 Installation

go get github.com/XanderD99/bunslog

📝 Usage Example

package main

import (
    "context"
    "log/slog"
    "os"
    "time"
    "github.com/uptrace/bun"
    "github.com/XanderD99/bunslog"
)

func main() {
    db := bun.NewDB(/* ... */)
    logger := slog.New(slog.NewTextHandler(os.Stdout, nil))
    hook := bunslog.NewQueryHook(
        bunslog.WithLogger(logger),
        bunslog.WithLogSlow(100 * time.Millisecond), // log queries slower than 100ms
    )
    db.AddQueryHook(hook)
    // ... your queries ...
}

🌍 Environment Configuration

You can control logging via environment variables:

  • 📴 BUNDEBUG=0 disables logging
  • BUNDEBUG=1 enables logging

📄 License

MIT 🎉

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(hook *QueryHook)

func FromEnv

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.

func WithEnabled

func WithEnabled(on bool) Option

WithEnabled enables/disables this hook

func WithErrorLevel

func WithErrorLevel(level slog.Level) Option

func WithLogSlow

func WithLogSlow(duration time.Duration) Option

func WithLogger

func WithLogger(logger *slog.Logger) Option

func WithQueryLevel

func WithQueryLevel(level slog.Level) Option

func WithSlowLevel

func WithSlowLevel(level slog.Level) Option

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 slog message

func (*QueryHook) BeforeQuery

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

BeforeQuery does nothing

Jump to

Keyboard shortcuts

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