log

package
v0.0.0-...-85c9292 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: MIT Imports: 7 Imported by: 29

Documentation

Overview

Package log exposes a minimal interface for structured logging. It supports log key/value pairs passed through context

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(ctx context.Context, msg string)

func Debugf

func Debugf(ctx context.Context, msg string, params ...interface{})

func Error

func Error(ctx context.Context, msg string)

func Errorf

func Errorf(ctx context.Context, msg string, params ...interface{})

func Fatal

func Fatal(ctx context.Context, msg string)

Fatal logs, then causes the current program to exit status 1 The program terminates immediately; deferred functions are not run.

func Fatalf

func Fatalf(ctx context.Context, msg string, params ...interface{})

Fatalf logs, then causes the current program to exit status 1 The program terminates immediately; deferred functions are not run.

func Info

func Info(ctx context.Context, msg string)

func Infof

func Infof(ctx context.Context, msg string, params ...interface{})

func SimplifyFields

func SimplifyFields(fields map[string]interface{}) map[string]interface{}

Types

type CallbackLogger

type CallbackLogger struct {
	Callback   LogFunc
	Collectors []ContextCollector
}

func NewCallbackLogger

func NewCallbackLogger(callback LogFunc) *CallbackLogger

func (*CallbackLogger) AddCollector

func (sl *CallbackLogger) AddCollector(collector ContextCollector)

func (CallbackLogger) Debug

func (sl CallbackLogger) Debug(ctx context.Context, msg string)

func (CallbackLogger) DebugContext

func (sl CallbackLogger) DebugContext(ctx context.Context, msg string, args ...any)

func (CallbackLogger) Error

func (sl CallbackLogger) Error(ctx context.Context, msg string)

func (CallbackLogger) ErrorContext

func (sl CallbackLogger) ErrorContext(ctx context.Context, msg string, args ...any)

func (CallbackLogger) Info

func (sl CallbackLogger) Info(ctx context.Context, msg string)

func (CallbackLogger) InfoContext

func (sl CallbackLogger) InfoContext(ctx context.Context, msg string, args ...any)

type ContextCollector

type ContextCollector interface {
	LogFieldsFromContext(context.Context) map[string]interface{}
}

type FieldContextProvider

type FieldContextProvider interface {
	WithFields(context.Context, map[string]interface{}) context.Context
	ContextCollector
}
var DefaultContext FieldContextProvider = &MapContext{}

type LogFunc

type LogFunc func(level string, message string, fields map[string]interface{})

func JSONLog

func JSONLog(out io.Writer) LogFunc

type Logger

type Logger interface {
	Debug(context.Context, string)
	Info(context.Context, string)
	Error(context.Context, string)
	AddCollector(ContextCollector)

	ErrorContext(ctx context.Context, msg string, args ...any)
}
var DefaultLogger Logger = NewCallbackLogger(JSONLog(os.Stderr))

type MapContext

type MapContext struct{}

func (MapContext) LogFieldsFromContext

func (sc MapContext) LogFieldsFromContext(ctx context.Context) map[string]interface{}

func (MapContext) WithFields

func (sc MapContext) WithFields(parent context.Context, fields map[string]interface{}) context.Context

type TraceContext

type TraceContext struct{}

func (TraceContext) FromContext

func (sc TraceContext) FromContext(ctx context.Context) string

func (TraceContext) LogFieldsFromContext

func (sc TraceContext) LogFieldsFromContext(ctx context.Context) map[string]interface{}

func (TraceContext) WithTrace

func (sc TraceContext) WithTrace(ctx context.Context, value string) context.Context

type TraceContextProvider

type TraceContextProvider interface {
	WithTrace(context.Context, string) context.Context
	FromContext(context.Context) string
	ContextCollector
}
var DefaultTrace TraceContextProvider = TraceContext{}

type WrappedContext

type WrappedContext struct {
	context.Context
}

WrappedContext is both a context and a logger, allowing either syntax log.WithField(ctx, "key", "val").Debug() or ctx = log.WithField(ctx, "key", "val")

func WithError

func WithError(ctx context.Context, err error) *WrappedContext

func WithField

func WithField(ctx context.Context, key string, value interface{}) *WrappedContext

func WithFields

func WithFields(ctx context.Context, fields map[string]interface{}) *WrappedContext

func (WrappedContext) Debug

func (ctx WrappedContext) Debug(msg string)

func (WrappedContext) Error

func (ctx WrappedContext) Error(msg string)

func (WrappedContext) Info

func (ctx WrappedContext) Info(msg string)

Jump to

Keyboard shortcuts

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