xslog

package module
v0.0.0-...-da7669f Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2023 License: MIT Imports: 4 Imported by: 0

README

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))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextLogger

func ContextLogger(ctx context.Context) *slog.Logger

ContextLogger returns slog logger from ctx.

func Debug

func Debug(ctx context.Context, msg string, attrs ...slog.Attr)

func Error

func Error(ctx context.Context, msg string, attrs ...slog.Attr)

func Info

func Info(ctx context.Context, msg string, attrs ...slog.Attr)

func Log

func Log(ctx context.Context, leveler slog.Leveler, msg string, attrs []slog.Attr)

func TransferLogger

func TransferLogger(dst context.Context, src context.Context) context.Context

TransferLogger returns a new context that is bound with slog logger from src and based on dst.

func Warn

func Warn(ctx context.Context, msg string, attrs ...slog.Attr)

func WithLogger

func WithLogger(ctx context.Context, logger *slog.Logger) context.Context

WithLogger returns a new context that is bound with logger.

Types

This section is empty.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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