otelslog

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package otelslog provides a OpenTelemetry aware slog.Handler implementation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(h slog.Handler) *slog.Logger

New provides a simple wrapper for slog.New(NewHandler(h)).

Types

type Handler

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

Handler is an slog.Handler which helps standardize and correlate your logs by automatically adding the Trace ID and Span ID to your logs.

func NewHandler

func NewHandler(h slog.Handler) *Handler

NewHandler returns a new Handler.

func (*Handler) Enabled

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

Enabled implements the slog.Handler interface.

func (*Handler) Handle

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

Handle implements the slog.Handler interface.

func (*Handler) WithAttrs

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

WithAttrs implements the slog.Handler interface.

Example
var buf bytes.Buffer
var h slog.Handler = NewHandler(slog.NewJSONHandler(&buf, &slog.HandlerOptions{}))
h = h.WithAttrs([]slog.Attr{slog.String("a", "b")})

logger := slog.New(h)
logger.Info("hello world")

var record struct {
	Message string `json:"msg"`
	A       string `json:"a"`
}
err := json.Unmarshal(buf.Bytes(), &record)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(record.Message)
fmt.Print(record.A)
Output:

hello world
b

func (*Handler) WithGroup

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

WithGroup implements the slog.Handler interface.

Example
var buf bytes.Buffer
var h slog.Handler = NewHandler(slog.NewJSONHandler(&buf, &slog.HandlerOptions{}))
h = h.WithGroup("n")

logger := slog.New(h)
logger.Info("hello world", slog.Int("one", 1))

var record struct {
	Message string `json:"msg"`
	N       struct {
		One int `json:"one"`
	} `json:"n"`
}
err := json.Unmarshal(buf.Bytes(), &record)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(record.Message)
fmt.Print(record.N.One)
Output:

hello world
1

Jump to

Keyboard shortcuts

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