log

package
v0.0.0-...-2040d2c Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Caller

func Caller(skip int) (pc uintptr)

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, def slog.Level) 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 MessageReceive

func MessageReceive(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 NewWriter

func NewWriter(ctx context.Context) io.Writer

func Prefix

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

func Std

func Std(w io.Writer, prefix string) *log.Logger

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
}

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.

type Writer

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

func (*Writer) Write

func (w *Writer) Write(buf []byte) (int, error)

Jump to

Keyboard shortcuts

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