bslog

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2025 License: Unlicense Imports: 5 Imported by: 0

README

bslog

🍃 Log archival as MongoDB BSON documents for log/slog

Test Go Reference

Overview

The bslog package archives ordinary logs as queryable MongoDB BSON documents with minimal configuration.

Built on top of the standard library's log/slog package, it offers a straightforward way to:

  • ✅ Archive logs in MongoDB as native BSON documents
  • ✅ Preserve the full structure and types of structured log attributes
  • ✅ Wrap any existing log/slog Handler implementation
  • ✅ Maintain full compatibility with existing log/slog integrations

It's lightweight, easy to configure, and integrates seamlessly with any existing log/slog-based logging setup.

Installing

  1. First, use go get to install the latest version of the package:

    go get -u go.luke.ph/bslog@latest
    
  2. Next, include the package in your application:

    import "go.luke.ph/bslog"
    

Usage

The bslog package is designed for simplicity. Just wrap your existing handler and provide MongoDB collection details:

client, err := mongo.Connect(context.Background())
if err != nil {
    // Handle the error...
}
defer client.Disconnect(context.Background())

handler := bslog.NewHandler(
   slog.NewTextHandler(os.Stdout, nil),
   client.Database("app").Collection("logs"),
)

slog.SetDefault(slog.New(handler))

License

The package is released under the Unlicense license.

References

Documentation

Overview

Package bslog provides log archival as MongoDB BSON documents for log/slog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

A Handler implements the slog.Handler interface.

func NewHandler

func NewHandler(handler slog.Handler, collection *mongo.Collection) *Handler

NewHandler constructs a *Handler that wraps the given handler.

func (*Handler) Enabled

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

Enabled implements the slog.Handler Enabled interface method. It calls the wrapped handler's Enabled method directly.

func (*Handler) Handle

func (h *Handler) Handle(ctx context.Context, r slog.Record) error

Handle implements the slog.Handler Handle interface method. It calls the wrapped handler's Handle method directly before persisting the log to MongoDB.

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs implements the slog.Handler WithAttrs interface method. It calls the wrapped handler's WithAttrs method directly.

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

WithGroup implements the slog.Handler WithGroup interface method. It calls the wrapped handler's WithGroup method directly.

Jump to

Keyboard shortcuts

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