slogger

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: May 19, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const TraceIDKey = "trace_id"

Variables

View Source
var Strings = map[slog.Level]string{
	slog.LevelDebug: "•",
	slog.LevelInfo:  "•",
	slog.LevelWarn:  "•",
	slog.LevelError: "⨯",
}

Functions

func ContextWithLogger

func ContextWithLogger(ctx context.Context, l *slog.Logger) context.Context

ContextWithLogger adds logger to context.

func ExtractLoggerFromContext

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

ExtractLoggerFromContext returns logger from context.

func NewHandler

func NewHandler(options *options) slog.Handler

NewHandler create new slog handler. Default stdout with json handler and info level

func NewHandlerOptions

func NewHandlerOptions(level slog.Leveler, opt *Options) slog.HandlerOptions

NewHandlerOptions create new options with level

func NewLogger

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

NewLogger create a new *slog.Logger with tracing handler wrapper

Types

type CLIHandler

type CLIHandler struct {
	slog.Handler
	// contains filtered or unexported fields
}

func NewCLIHandler

func NewCLIHandler(
	out io.Writer,
	opts *CLIHandlerOptions,
) *CLIHandler

func (*CLIHandler) Enabled

func (h *CLIHandler) Enabled(_ context.Context, l slog.Level) bool

func (*CLIHandler) Handle

func (h *CLIHandler) Handle(_ context.Context, r slog.Record) error

func (*CLIHandler) WithAttrs

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

func (*CLIHandler) WithGroup

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

type CLIHandlerOptions

type CLIHandlerOptions struct {
	DisableColor bool
	slog.HandlerOptions
}

type Option

type Option func(o *options)

Option is an application option.

func WithAppName

func WithAppName(name string) Option

WithAppName application name

func WithDisableSource

func WithDisableSource() Option

WithDisableSource disable source log

func WithDisableTime

func WithDisableTime() Option

WithDisableTime disable time in log

func WithFormat

func WithFormat(format string) Option

WithFormat set output format. Default "json"

func WithFullSource

func WithFullSource() Option

WithFullSource disable crop source

func WithLevel

func WithLevel(level string) Option

WithLevel set log level. Default "info"

func WithOrganization

func WithOrganization(name string) Option

WithOrganization set name of organization

func WithOutput

func WithOutput(output string) Option

WithOutput set output. Default "stdout"

func WithTracing

func WithTracing() Option

WithTracing enable tracing

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter set writer

type Options

type Options struct {
	DisableSource bool
	FullSource    bool
	DisableTime   bool
	DisableColor  bool // for cli

	// for format
	Name         string
	Organization string
	Context      string
}

Options

type TracingHandler

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

TracingHandler slog with traces

func NewTracingHandler

func NewTracingHandler(h slog.Handler) *TracingHandler

NewTracingHandler create tracing handler

func (*TracingHandler) Enabled

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

Enabled implements Handler.Enabled by reporting whether level is at least as large as h's level.

func (*TracingHandler) Handle

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

Handle implements Handler.Handle.

func (*TracingHandler) Handler

func (h *TracingHandler) Handler() slog.Handler

Handler returns the Handler wrapped by handler.

func (*TracingHandler) WithAttrs

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

WithAttrs implements Handler.WithAttrs.

func (*TracingHandler) WithGroup

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

WithGroup implements Handler.WithGroup.

Jump to

Keyboard shortcuts

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