ctxslog

package module
v0.0.0-...-8cce29b Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 4 Imported by: 2

README

ctxslog

PkgGoDev

This package provides a context value slog.Logger. You can use it to log messages with a request scoped logger that can be extended by additional attributes.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddArgs

func AddArgs(ctx context.Context, args ...any)

AddArgs adds attributes to the context logger.

func Debug

func Debug(ctx context.Context, msg string, args ...any)

Debug is equivalent to calling Debug on the logger in the context.

func Error

func Error(ctx context.Context, msg string, args ...any)

Error is equivalent to calling Error on the logger in the context.

func Extract

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

Extract returns the context-scoped Logger.

It always returns a Logger.

func Info

func Info(ctx context.Context, msg string, args ...any)

Info is equivalent to calling Info on the logger in the context.

func ToContext

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

ToContext adds the slog.Logger to the context for extraction later. Returning the new context that has been created.

Example
package main

import (
	"context"
	"fmt"
	"log/slog"
	"os"
	"path/filepath"

	"github.com/MottoStreaming/ctxslog.go"
)

func main() {
	th := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
		AddSource:   true,
		ReplaceAttr: RemoveTimeAndBaseSource,
	})
	logger := slog.New(th)

	ctx := ctxslog.ToContext(context.Background(), logger)

	ctxslog.AddArgs(ctx, slog.String("name", "mycujoo"))

	// This line doesn't appear in the output because default logger is not visible in tests.
	ctxslog.Warn(context.Background(), "this is a warning")

	// This one is not logged because the level is not enabled.
	ctxslog.Debug(ctx, "debug msg")

	err := fmt.Errorf("failed to read data: %w", os.ErrPermission)
	ctxslog.Error(ctx, "failed to read data", "error", err)

	ctxslog.Info(ctx, "additional event")

	l := ctxslog.Extract(ctx)
	l.WithGroup("group").Info("this is a log", "test", "a")
}

// RemoveTimeAndBaseSource removes the top-level time attribute and simplifies the source file path.
// It is intended to be used as a ReplaceAttr function,
// to make example output deterministic.
func RemoveTimeAndBaseSource(groups []string, a slog.Attr) slog.Attr {
	if a.Key == slog.TimeKey && len(groups) == 0 {
		return slog.Attr{}
	}
	if a.Key == slog.SourceKey {
		s := a.Value.Any().(*slog.Source)
		s.File = filepath.Base(s.File)
		return slog.Any(a.Key, s)
	}
	return a
}
Output:

level=ERROR source=example_test.go:31 msg="failed to read data" name=mycujoo error="failed to read data: permission denied"
level=INFO source=example_test.go:33 msg="additional event" name=mycujoo
level=INFO source=example_test.go:36 msg="this is a log" name=mycujoo group.test=a

func Warn

func Warn(ctx context.Context, msg string, args ...any)

Warn is equivalent to calling Warn on the logger in the context.

Types

This section is empty.

Jump to

Keyboard shortcuts

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