logging

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2023 License: Apache-2.0 Imports: 3 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

View Source
var TraceIDKey = "traceID"

TraceIDKey is the key used by loggers for the trace ID field in key/value pairs. It is set as a variable rather than a constant so that it can be changed by users at startup.

Functions

func Context

func Context(ctx context.Context, logger Logger) context.Context

Context returns a new context built from the provided context with the provided logger in it. The Logger added with Context() can be retrieved with FromContext()

Types

type Logger

type Logger interface {
	// Debug logs a message at the DEBUG level, with optional arguments as a sequence of key/value pairs
	// (e.g. Debug("message", "key1", "val1", "key2", "val2"))
	Debug(msg string, args ...any)
	// Info logs a message at the INFO level, with optional arguments as a sequence of key/value pairs
	// (e.g. Info("message", "key1", "val1", "key2", "val2"))
	Info(msg string, args ...any)
	// Warn logs a message at the WARN level, with optional arguments as a sequence of key/value pairs
	// (e.g. Warn("message", "key1", "val1", "key2", "val2"))
	Warn(msg string, args ...any)
	// Error logs a message at the ERROR level, with optional arguments as a sequence of key/value pairs
	// (e.g. Error("message", "key1", "val1", "key2", "val2"))
	Error(msg string, args ...any)
	// With returns a Logger with the supplied key/value pair arguments attached to any messages it logs.
	// This is syntactically equivalent to adding args to every call to a log method on the logger.
	With(args ...any) Logger
	// WithContext returns a Logger with the provided context added, such that any subsequent
	// calls to log methods should pass the context to the underlying handler.
	WithContext(context.Context) Logger
}

Logger is a simple logging interface that exposes methods got writing structured log messages at varying levels, and methods to return an altered logger (With and WithContext).

var (
	// DefaultLogger is the default Logger for all SDK logging, if one hasn't been provided in the context.
	DefaultLogger Logger = &NoOpLogger{}
)

func FromContext

func FromContext(ctx context.Context) Logger

FromContext returns the Logger set in the context with Context(), or the DefaultLogger if no Logger is set in the context. If DefaultLogger is nil, it returns a *NoOpLogger so that the return is always valid to call methods on without nil-checking. So long as the Logger to return is not the NoOpLogger, it will carry the context provided in this call (by calling WithContext on the logger in the context).

type NoOpLogger

type NoOpLogger struct{}

NoOpLogger is an implementation of Logger which does nothing when its methods are called

func (*NoOpLogger) Debug

func (*NoOpLogger) Debug(string, ...any)

func (*NoOpLogger) Error

func (*NoOpLogger) Error(string, ...any)

func (*NoOpLogger) Info

func (*NoOpLogger) Info(string, ...any)

func (*NoOpLogger) Warn

func (*NoOpLogger) Warn(string, ...any)

func (*NoOpLogger) With

func (n *NoOpLogger) With(...any) Logger

func (*NoOpLogger) WithContext

func (n *NoOpLogger) WithContext(context.Context) Logger

type SLogLogger

type SLogLogger struct {
	Logger *slog.Logger
	// contains filtered or unexported fields
}

SLogLogger wraps slog.Logger both to override the With() method to return an *SLogLogger, and to have an embedded context.Context, which is passed to the slog.Logger's _Level_Context method when the _Level_ method is called.

func NewSLogLogger

func NewSLogLogger(handler slog.Handler) *SLogLogger

NewSLogLogger creates a new SLogLogger which wraps an *slog.Logger that has a handler to always add a trace ID to the log messages if the context is provided in the log call (e.g. InfoContext())

func (*SLogLogger) Debug

func (s *SLogLogger) Debug(msg string, args ...any)

Debug calls the slog.Logger's DebugContext method with the context provided by WithContext

func (*SLogLogger) Error

func (s *SLogLogger) Error(msg string, args ...any)

Error calls the slog.Logger's ErrorContext method with the context provided by WithContext

func (*SLogLogger) Info

func (s *SLogLogger) Info(msg string, args ...any)

Info calls the slog.Logger's InfoContext method with the context provided by WithContext

func (*SLogLogger) Warn

func (s *SLogLogger) Warn(msg string, args ...any)

Warn calls the slog.Logger's WarnContext method with the context provided by WithContext

func (*SLogLogger) With

func (s *SLogLogger) With(args ...any) Logger

With returns a new *SLogLogger with the provided key/value pairs attached

func (*SLogLogger) WithContext

func (s *SLogLogger) WithContext(ctx context.Context) Logger

WithContext returns an *SLogLogger which still points to the same underlying *slog.Logger, but has the provided context attached for Debug, Info, Warn, and Error calls.

Jump to

Keyboard shortcuts

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