galecore / xslog

Extension handlers for the slog library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

xslog

Extension handlers and example usage for the golang slog library

Overview

The slog library is a new logging interface for golang. It is a simple interface that allows you to use any logging library you want, as long as it implements the Handler interface.

The philosophy behind slog is that it should be easy to use with any logging library, as an interface set of log functions. The implementation of how to log is left to the Handler interface.

Example usage

package main

import (
	"context"
	"os"

	"github.com/galecore/xslog/xdata"
	"github.com/galecore/xslog/xotel"
	"github.com/galecore/xslog/xtee"
	"github.com/galecore/xslog/xzerolog"
	"github.com/rs/zerolog"
	"golang.org/x/exp/slog"
)

// NewProductionHandlers creates a set of slog handlers that can be used in production
// environments. This is a good starting point for your own production logging setup.
func NewProductionHandlers() *slog.Logger {
	otelHandler := xotel.NewHandler([]slog.Level{slog.LevelWarn, slog.LevelError}, xotel.DefaultKeyBuilder)

	zerologger := zerolog.New(os.Stdout)
	zerologHandler := xzerolog.NewHandler(&zerologger)
	teeHandler := xtee.NewHandler(otelHandler, zerologHandler)

	handler := xdata.NewHandler(teeHandler)
	return slog.New(handler)
}

func main() {
	logger := NewProductionHandlers()
	slog.SetDefault(logger)

	ctx := context.Background()
	ctx = xdata.WithAttrs(ctx, slog.String("author", "galecore"))

	slog.InfoContext(ctx, "hello world", slog.String("foo", "bar"), slog.Int("baz", 42))
}

About

Extension handlers for the slog library

License:MIT License


Languages

Language:Go 100.0%