slog

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	LEVEL_DEBUG Level = -4
	LEVEL_INFO  Level = 0
	LEVEL_WARN  Level = 4
	LEVEL_ERROR Level = 8
	LEVEL_FATAL Level = 12
	LEVEL_TRACE Level = 16

	FORMAT_JSON Formatter = "json"
	FORMAT_TEXT Formatter = "text"
)

Variables

View Source
var SourceKey = "source"

Functions

func AnyValueToString

func AnyValueToString(v slog.Value) string

func AttrsToString

func AttrsToString(attrs ...slog.Attr) map[string]string

func ContextExtractor

func ContextExtractor(ctx context.Context, fns []func(ctx context.Context) []slog.Attr) []slog.Attr

func Debug

func Debug(args ...any)

Debug calls the default logger's Debug method.

func DebugContext

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

DebugContext calls the default logger's DebugContext method.

func Debugf

func Debugf(msg string, args ...any)

Debugf calls the default logger's Debugf method.

func DebugfContext

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

DebugfContext calls the default logger's DebugfContext method.

func DefaultConverter

func DefaultConverter(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record, tag string) map[string]string

func Error

func Error(args ...any)

Error calls the default logger's Error method.

func ErrorContext

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

ErrorContext calls the default logger's ErrorContext method.

func Errorf

func Errorf(msg string, args ...any)

Errorf calls the default logger's Errorf method.

func ErrorfContext

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

ErrorfContext calls the default logger's ErrorfContext method.

func ExtractFromContext

func ExtractFromContext(keys ...any) func(ctx context.Context) []slog.Attr

func Info

func Info(args ...any)

Info calls the default logger's Info method.

func InfoContext

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

InfoContext calls the default logger's InfoContext method.

func Infof

func Infof(msg string, args ...any)

Infof calls the default logger's Infof method.

func InfofContext

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

InfofContext calls the default logger's InfofContext method.

func LogWithAttrs

func LogWithAttrs(level Level, ctx context.Context, msg string, attrs ...slog.Attr)

func NewDefaultHandler

func NewDefaultHandler(w io.Writer, opts *slog.HandlerOptions, config *coreConfig) *defaultHandler

func SetContextTraceId

func SetContextTraceId(ctx context.Context, traceId string) context.Context

func SetLogger

func SetLogger(l IFullLogger)

SetLogger sets the default logger. Note that this method is not concurrent-safe and must not be called after the use of DefaultLogger and global functions in this package.

func ValueToString

func ValueToString(v slog.Value) string

func Warn

func Warn(args ...any)

Warn calls the default logger's Warn method.

func WarnContext

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

WarnContext calls the default logger's WarnContext method.

func Warnf

func Warnf(msg string, args ...any)

Warnf calls the default logger's Warnf method.

func WarnfContext

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

WarnfContext calls the default logger's WarnfContext method.

Types

type ContextKey

type ContextKey string

type Converter

type Converter func(addSource bool, replaceAttr func(groups []string, a slog.Attr) slog.Attr, loggerAttr []slog.Attr, groups []string, record *slog.Record, tag string) map[string]string

type Formatter

type Formatter string

type IContextFormatLogger

type IContextFormatLogger interface {
	InfofContext(ctx context.Context, msg string, args ...any)
	WarnfContext(ctx context.Context, msg string, args ...any)
	ErrorfContext(ctx context.Context, msg string, args ...any)
	DebugfContext(ctx context.Context, msg string, args ...any)
}

type IContextLogger

type IContextLogger interface {
	InfoContext(ctx context.Context, args ...any)
	WarnContext(ctx context.Context, args ...any)
	ErrorContext(ctx context.Context, args ...any)
	DebugContext(ctx context.Context, args ...any)
}

type ICustomLogger

type ICustomLogger interface {
	LogWithAttrs(level Level, ctx context.Context, msg string, attrs ...slog.Attr)
}

type IFormatLogger

type IFormatLogger interface {
	Infof(msg string, args ...any)
	Warnf(msg string, args ...any)
	Errorf(msg string, args ...any)
	Debugf(msg string, args ...any)
}

type IFullLogger

func GetLogger

func GetLogger() IFullLogger

DefaultLogger return the default logger.

type ILogger

type ILogger interface {
	Info(args ...any)
	Warn(args ...any)
	Error(args ...any)
	Debug(args ...any)
}

type Level

type Level slog.Level

type Option

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

Option slog option

func WithAttrs

func WithAttrs(attrs ...slog.Attr) Option

func WithFileSource

func WithFileSource() Option

func WithFluentd

func WithFluentd(client *fluent.Fluent, tag string) Option

WithFluentd fluentd config

func WithFormatter

func WithFormatter(formatter Formatter) Option

WithFormatter formatter default json. Enum: json or text

func WithHandlerOptions

func WithHandlerOptions(opt *slog.HandlerOptions) Option

WithHandlerOptions slog handler-options

func WithLevel

func WithLevel(level Level) Option

WithLevel slog level

func WithOutput

func WithOutput(iow io.Writer) Option

WithOutput slog writer

func WithRotateFile

func WithRotateFile(f string) Option

WithRotateFile rotate file option

func WithTraceId

func WithTraceId() Option

type SLogger

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

func NewSLogger

func NewSLogger(opts ...Option) *SLogger

func (*SLogger) Debug

func (l *SLogger) Debug(args ...any)

func (*SLogger) DebugContext

func (l *SLogger) DebugContext(ctx context.Context, args ...any)

func (*SLogger) Debugf

func (l *SLogger) Debugf(msg string, args ...any)

func (*SLogger) DebugfContext

func (l *SLogger) DebugfContext(ctx context.Context, msg string, args ...any)

func (*SLogger) Error

func (l *SLogger) Error(args ...any)

func (*SLogger) ErrorContext

func (l *SLogger) ErrorContext(ctx context.Context, args ...any)

func (*SLogger) Errorf

func (l *SLogger) Errorf(msg string, args ...any)

func (*SLogger) ErrorfContext

func (l *SLogger) ErrorfContext(ctx context.Context, msg string, args ...any)

func (*SLogger) Info

func (l *SLogger) Info(args ...any)

func (*SLogger) InfoContext

func (l *SLogger) InfoContext(ctx context.Context, args ...any)

func (*SLogger) Infof

func (l *SLogger) Infof(msg string, args ...any)

func (*SLogger) InfofContext

func (l *SLogger) InfofContext(ctx context.Context, msg string, args ...any)

func (*SLogger) Log

func (l *SLogger) Log(level Level, msg string)

func (*SLogger) LogCtxf

func (l *SLogger) LogCtxf(level Level, ctx context.Context, format string, kvs ...interface{})

func (*SLogger) LogWithAttrs

func (l *SLogger) LogWithAttrs(level Level, ctx context.Context, msg string, attrs ...slog.Attr)

func (*SLogger) Logf

func (l *SLogger) Logf(level Level, format string, kvs ...interface{})

func (*SLogger) SetLevel

func (l *SLogger) SetLevel(level Level)

func (*SLogger) SetOutput

func (l *SLogger) SetOutput(writer io.Writer)

func (*SLogger) Warn

func (l *SLogger) Warn(args ...any)

func (*SLogger) WarnContext

func (l *SLogger) WarnContext(ctx context.Context, args ...any)

func (*SLogger) Warnf

func (l *SLogger) Warnf(msg string, args ...any)

func (*SLogger) WarnfContext

func (l *SLogger) WarnfContext(ctx context.Context, msg string, args ...any)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳