log

package
v0.0.0-...-3570b50 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func InterceptorLogger

func InterceptorLogger(l *slog.Logger) logging.Logger

InterceptorLogger is a gRPC interceptor that logs calls to gRPC endpoints, from: https://github.com/grpc-ecosystem/go-grpc-middleware/blob/main/interceptors/logging/examples/slog/example_test.go licensed as Apache 2.0.

func New

func New(opts ...Option) *slog.Logger

func NewFromHandler

func NewFromHandler(handler slog.Handler) *slog.Logger

NewFromHandler initializes a logger with the provided slog.Handler interface.

If the provided handler is nil, the function returns a no-op slog.Logger.

func NewSpanContextHandler

func NewSpanContextHandler(handler slog.Handler, withSpanID bool) slog.Handler

NewSpanContextHandler creates a SpanContextHandler from the input slog.Handler and optional configuration.

func NoOp

func NoOp() *slog.Logger

Types

type Config

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

type Option

type Option interface {
	// contains filtered or unexported methods
}

type OptionFunc

type OptionFunc func(*Config)

func WithLevel

func WithLevel(level string) OptionFunc

func WithSource

func WithSource() OptionFunc

func WithWriter

func WithWriter(writer io.Writer) OptionFunc

type SpanContextHandler

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

SpanContextHandler is a slog.Handler wrapper that adds trace data as log attributes on each Handle call, given that the input context to the method contains a valid trace.SpanContext.

func (*SpanContextHandler) Enabled

func (h *SpanContextHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower. It is called early, before any arguments are processed, to save effort if the log event should be discarded. If called from a Logger method, the first argument is the context passed to that method, or context.Background() if nil was passed or the method does not take a context. The context is passed so Enabled can use its values to make a decision.

func (*SpanContextHandler) Handle

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

Handle handles the Record. It will only be called when Enabled returns true. The Context argument is as for Enabled. It is present solely to provide Handlers access to the context's values. Canceling the context should not affect record processing. (Among other things, log messages may be necessary to debug a cancellation-related problem.)

Handle methods that produce output should observe the following rules:

  • If r.Time is the zero time, ignore the time.
  • If r.PC is zero, ignore it.
  • Attr's values should be resolved.
  • If an Attr's key and value are both the zero value, ignore the Attr. This can be tested with attr.Equal(Attr{}).
  • If a group's key is empty, inline the group's Attrs.
  • If a group has no Attrs (even if it has a non-empty key), ignore it.

func (*SpanContextHandler) WithAttrs

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

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments. The Handler owns the slice: it may retain, modify or discard it.

func (*SpanContextHandler) WithGroup

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

WithGroup returns a new Handler with the given group appended to the receiver's existing groups. The keys of all subsequent attributes, whether added by With or in a Record, should be qualified by the sequence of group names.

How this qualification happens is up to the Handler, so long as this Handler's attribute keys differ from those of another Handler with a different sequence of group names.

A Handler should treat WithGroup as starting a Group of Attrs that ends at the end of the log event. That is,

logger.WithGroup("s").LogAttrs(level, msg, slog.Int("a", 1), slog.Int("b", 2))

should behave like

logger.LogAttrs(level, msg, slog.Group("s", slog.Int("a", 1), slog.Int("b", 2)))

If the name is empty, WithGroup returns the receiver.

Jump to

Keyboard shortcuts

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