log

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: MIT Imports: 15 Imported by: 0

README

logger

这只是一个handler的实现

install

go get -u github.com/ndsky1003/log
usage
	handler := sloghandler.NewFastTextHandler(sloghandler.Options().SetAddSource(true).SetReplaceAttr(func(groups []string, a slog.Attr) slog.Attr {
//在这里修改指定的打印逻辑,比如passwd,用******替换
		if a.Key == "a" {
			a.Value = slog.StringValue("hello")
		}
		return a
	}).SetExtractorAttr(func(ctx context.Context, r *slog.Record) {
// 根据项目在这里注入traceid
		r.AddAttrs(slog.String("extracted", "value"))
	}).SetForceDebugFn(func(ctx context.Context) bool {
//强制某些人,不被打印等级控制
		return true
	}))
	handler.SetAddSource(false) //动态修改打印行号
	handler.SetLevel(slog.LevelError) //动态修改打印逻辑

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger

func Logger() *slog.Logger

func SetLogger

func SetLogger(l *slog.Logger)

Types

type FastTextHandler

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

FastTextHandler 高性能文本 Handler

func NewFastTextHandler

func NewFastTextHandler(opts ...*Option) *FastTextHandler

NewFastTextHandler 构造函数

func (*FastTextHandler) Enabled

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

func (*FastTextHandler) Handle

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

Handle 核心热点路径

func (*FastTextHandler) SetAddSource

func (h *FastTextHandler) SetAddSource(b bool)

func (*FastTextHandler) SetLevel

func (h *FastTextHandler) SetLevel(level Level)

func (*FastTextHandler) WithAttrs

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

func (*FastTextHandler) WithGroup

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

type Level

type Level = slog.Level
const (
	LevelDebug Level = -4
	LevelInfo  Level = 0
	LevelWarn  Level = 4
	LevelError Level = 8
)

type Option

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

func Options

func Options() *Option

func (Option) Merge

func (o Option) Merge(opts ...*Option) Option

func (*Option) SetAddSource

func (o *Option) SetAddSource(addSource bool) *Option

func (*Option) SetExtractorAttr

func (o *Option) SetExtractorAttr(fn func(context.Context, *slog.Record)) *Option

func (*Option) SetForceDebugFn

func (o *Option) SetForceDebugFn(fn func(ctx context.Context) bool) *Option

func (*Option) SetLevel

func (o *Option) SetLevel(level Level) *Option

func (*Option) SetLevelString

func (o *Option) SetLevelString(lvl string) *Option

func (*Option) SetReplaceAttr

func (o *Option) SetReplaceAttr(replaceAttr func(groups []string, a slog.Attr) slog.Attr) *Option

func (*Option) SetWriter

func (o *Option) SetWriter(w io.Writer) *Option

type SlogHandler

type SlogHandler interface {
	slog.Handler
	SetLevel(level Level)
	SetAddSource(b bool)
}

func New

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

NOTE: 返回值中的 SlogHandler 可选用于进一步配置 Handler,从后台动态修改调试日志级别

func SetDefault

func SetDefault(opts ...*Option) SlogHandler

Jump to

Keyboard shortcuts

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