ctxslog

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2024 License: MIT Imports: 3 Imported by: 0

README

ctxslog

An slog handler to pull shared information from context.Context for use with structured logging.

Installation

go get github.com/felttrip/ctxslog

Usage

  • Add ctxslog into the handler chain when calling SetDefault with `slog.
  • Use ctxslog.WithValue or ctxslog.WithValues to add Key Value pairs to the context to be logged
    • This operates the same way as context.Context.WithValue but with the addition of keeping track of which fields on the context should be logged.
  • Use the slog.InfoContext, slog.WarnContext, or slog.ErrorContext functions providing the context that has the fields you want to log attached.
slog.SetDefault(slog.New(ctxslog.NewHandler(slog.NewJSONHandler(os.Stdout, nil))))

ctx := ctxslog.WithValue(context.Background(), "AccountID", 123456789)
ctx = ctxslog.WithValue(ctx, "email", "noone@felttrip.com")
ctx = ctxslog.WithValue(ctx, "sender", "greg@BailysInAShoe.lake")

slog.InfoContext(ctx, "Info With Context")
fmt.Println()
ctx = ctxslog.WithValues(context.Background(), map[string]interface{}{
  "AccountID": 987654321,
  "email":     "bob@TheBuilder.fake",
  "complexData": ComplexData{
    IntField:   123,
    StrField:   "DEADBEEF",
    BoolField:  true,
    SliceField: []string{"one", "two", "three"},
  },
})

slog.ErrorContext(ctx, "Error With Context")

Example Output

{"time":"2024-07-12T10:38:41.610863-06:00","level":"INFO","msg":"Info With Context","AccountID":123456789,"email":"noone@felttrip.com","sender":"greg@BailysInAShoe.lake"}

{"time":"2024-07-12T10:38:41.611199-06:00","level":"ERROR","msg":"Error With Context","complexData":{"IntField":123,"StrField":"DEADBEEF","BoolField":true,"SliceField":["one","two","three"]},"AccountID":987654321,"email":"bob@TheBuilder.fake"}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(handler slog.Handler) slog.Handler

func WithValue

func WithValue(ctx context.Context, k string, v any) context.Context

func WithValues

func WithValues(ctx context.Context, fields map[string]any) context.Context

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}
Example
package main

import (
	"context"
	"log/slog"
	"os"

	"github.com/felttrip/ctxslog"
)

type ComplexData struct {
	IntField   int
	StrField   string
	BoolField  bool
	SliceField []string
}

func main() {
	slog.SetDefault(slog.New(ctxslog.NewHandler(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
		ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
			// Remove time from the output for predictable test output.
			if a.Key == slog.TimeKey {
				return slog.Attr{}
			}
			return a
		},
	}))))

	ctx := ctxslog.WithValue(context.Background(), "AccountID", 123456789)
	ctx = ctxslog.WithValue(ctx, "email", "noone@felttrip.com")
	ctx = ctxslog.WithValue(ctx, "sender", "greg@BailysInAShoe.lake")

	slog.InfoContext(ctx, "Info With Context")
	ctx = ctxslog.WithValues(context.Background(), map[string]interface{}{
		"AccountID": 987654321,
		"email":     "bob@TheBuilder.fake",
		"complexData": ComplexData{
			IntField:   123,
			StrField:   "DEADBEEF",
			BoolField:  true,
			SliceField: []string{"one", "two", "three"},
		},
	})

	slog.ErrorContext(ctx, "Error With Context")
}
Output:

{"level":"INFO","msg":"Info With Context","AccountID":123456789,"email":"noone@felttrip.com","sender":"greg@BailysInAShoe.lake"}
{"level":"ERROR","msg":"Error With Context","AccountID":987654321,"email":"bob@TheBuilder.fake","complexData":{"IntField":123,"StrField":"DEADBEEF","BoolField":true,"SliceField":["one","two","three"]}}

func (Handler) Enabled

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

Enabled implements slog.Handler.

func (Handler) Handle

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

Handle implements slog.Handler.

func (Handler) WithAttrs

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

WithAttrs implements slog.Handler.

func (Handler) WithGroup

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

WithGroup implements slog.Handler.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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