log

package module
v0.0.30 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddSource added in v0.0.20

func AddSource(logger *slog.Logger, debugOnly bool)

func Err

func Err(err error) slog.Attr

Err returns a tinted (colorized) slog.Attr that will be written in red color by the [tint.Handler]. When used with any other slog.Handler, it behaves as

slog.Any("err", err)

func ForDefault

func ForDefault(level string, addSource ...bool)

ForDefault 函数用于设置默认日志记录器的级别和是否添加源信息。

参数:

  • level string - 日志级别的字符串表示,例如 "info", "debug" 等。
  • addSource ...bool - 可选参数,用于指定是否添加源信息。如果提供,第一个布尔值将被用于设置是否添加源信息。否则根据 level == debug 判断添加源信息。

func GetPrefix

func GetPrefix(ctx context.Context) (prefix string)

func Level

func Level(logger *slog.Logger, level string)

Level sets the log level

func LevelFromString

func LevelFromString(level string) slog.Level

LevelFromString parse the level from string, ignore case

  • debug => slog.LevelDebug
  • info, information => slog.LevelInfo
  • warn, warning => slog.LevelWarn
  • error, err => slog.LevelError
  • otherwise slog.LevelInfo

func MessageRecive added in v0.0.10

func MessageRecive(ctx context.Context, handleMessage func(ctx context.Context, msg string)) io.WriteCloser

func New

func New(opts *Options) *slog.Logger

func NewHandler

func NewHandler(w io.Writer, opts *Options) slog.Handler

NewHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options. If opts is nil, the default options are used.

func Prefix

func Prefix(ctx context.Context, prefix string) context.Context

func SetDefault added in v0.0.20

func SetDefault(opts *Options)

Types

type Options

type Options struct {
	// Enable source code location (Default: false)
	AddSource bool

	// Minimum level to log (Default: slog.LevelInfo)
	Level slog.Leveler

	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// See https://pkg.go.dev/log/slog#HandlerOptions for details.
	ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr

	// Time format (Default: "01/02 15:04:05")
	TimeFormat string

	// Disable color (Default: false)
	NoColor bool

	DebugSourceOnly bool
}

Options for a slog.Handler that writes tinted logs. A zero Options consists entirely of default values.

Options can be used as a drop-in replacement for slog.HandlerOptions.

Jump to

Keyboard shortcuts

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