log

package module
v0.0.0-...-24d5830 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: MIT Imports: 14 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(msg any, args ...any)

func Enabled

func Enabled(ctx context.Context, level Level) bool

func Error

func Error(msg any, args ...any)

func Fatal

func Fatal(msg any, args ...any)

func Info

func Info(msg any, args ...any)

func Log

func Log(level Level, msg any, args ...any)

func Output

func Output() io.Writer

func Panic

func Panic(msg any, args ...any)

func SetDefault

func SetDefault(l Logger)

func SetLevel

func SetLevel(level Level)

func SetOutput

func SetOutput(w io.Writer)

func Trace

func Trace(msg any, args ...any)

func Warn

func Warn(msg any, args ...any)

Types

type Attr

type Attr = slog.Attr

Attr 引用 log.Attr,方便日后定制

func Any

func Any(key string, value any) Attr

Any returns an Attr for the supplied value. See [AnyValue] for how values are treated.

func Bool

func Bool(key string, v bool) Attr

Bool returns an Attr for a bool.

func Duration

func Duration(key string, v time.Duration) Attr

Duration returns an Attr for a time.Duration.

func Float64

func Float64(key string, v float64) Attr

Float64 returns an Attr for a floating-point number.

func Group

func Group(key string, args ...any) Attr

Group returns an Attr for a Group Instance. The first argument is the key; the remaining arguments are converted to Attrs as in [Logger.Log].

Use Group to collect several key-value pairs under a single key on a log line, or as the result of LogValue in order to log a single value as multiple Attrs.

func Int

func Int(key string, value int) Attr

Int converts an int to an int64 and returns an Attr with that value.

func Int64

func Int64(key string, value int64) Attr

Int64 returns an Attr for an int64.

func String

func String(key, value string) Attr

String returns an Attr for a string value.

func Time

func Time(key string, v time.Time) Attr

Time returns an Attr for a time.Time. It discards the monotonic portion.

func Uint64

func Uint64(key string, v uint64) Attr

Uint64 returns an Attr for an uint64.

type IndentHandler

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

func NewIndentHandler

func NewIndentHandler(out io.Writer, opts *slog.HandlerOptions) *IndentHandler

func (*IndentHandler) Enabled

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

func (*IndentHandler) Handle

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

func (*IndentHandler) WithAttrs

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

func (*IndentHandler) WithGroup

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

type Level

type Level int
const (
	LevelTrace Level = iota
	LevelDebug       // 用于程序调试
	LevelInfo        // 用于程序运行
	LevelWarn        // 潜在错误或非预期结果
	LevelError       // 发生错误,但不影响系统的继续运行
	LevelPanic
	LevelFatal
)

func GetLevel

func GetLevel() Level

func (Level) Level

func (l Level) Level() slog.Level

Level 实现 slog.Leveler 接口

func (Level) MarshalJSON

func (l Level) MarshalJSON() ([]byte, error)

MarshalJSON 实现 encoding/json.Marshaler 接口, 使用双引号包裹 Level.String 的结果作为返回值。

func (Level) MarshalText

func (l Level) MarshalText() ([]byte, error)

MarshalText 实现 encoding.TextMarshaler 接口

func (Level) String

func (l Level) String() string

String 返回字符串形式

func (*Level) UnmarshalJSON

func (l *Level) UnmarshalJSON(data []byte) error

UnmarshalJSON 实现 encoding/json.Unmarshaler 接口 It accepts any string produced by Level.MarshalJSON, ignoring a case. It also accepts numeric offsets that would result in a different string on output. For example, "Error-8" would marshal as "INFO".

func (*Level) UnmarshalText

func (l *Level) UnmarshalText(data []byte) error

UnmarshalText 实现 encoding.TextUnmarshaler 接口。

type Logger

type Logger interface {
	Output() io.Writer
	SetOutput(w io.Writer)
	Level() Level
	SetLevel(level Level)
	Enabled(ctx context.Context, level Level) bool
	// With returns a Logger that includes the given attributes
	// in each output operation. Arguments are converted to
	// attributes as if by [Logger.Log].
	With(args ...any) Logger
	// WithGroup returns a Logger that starts a group, if name is non-empty.
	// The keys of all attributes added to the Logger will be qualified by the given
	// name. (How that qualification happens depends on the [Handler.WithGroup]
	// method of the Logger's Handler.)
	//
	// If name is empty, WithGroup returns the receiver.
	WithGroup(name string) Logger
	// Log emits a log record with the current time and the given level and message.
	// The Record's Attrs consist of the Logger's attributes followed by
	// the Attrs specified by args.
	//
	// The attribute arguments are processed as follows:
	//   - If an argument is an Attr, it is used as is.
	//   - If an argument is a string and this is not the last argument,
	//     the following argument is treated as the value and the two are combined
	//     into an Attr.
	//   - Otherwise, the argument is treated as a value with key "!BADKEY".
	Log(level Level, msg any, args ...any)
	// Trace logs at [LevelTrace].
	Trace(msg any, args ...any)
	// Debug logs at [LevelDebug].
	Debug(msg any, args ...any)
	// Info logs at [LevelInfo].
	Info(msg any, args ...any)
	// Warn logs at [LevelWarn].
	Warn(msg any, args ...any)
	// Error logs at [LevelError].
	Error(msg any, args ...any)
	// Panic logs at [LevelPanic].
	Panic(msg any, args ...any)
	// Fatal logs at [LevelFatal].
	Fatal(msg any, args ...any)
}

Logger defines the logging interface.

func Default

func Default() Logger

func New

func New(opts *Options) Logger

func With

func With(args ...any) Logger

func WithGroup

func WithGroup(name string) Logger

type Options

type Options struct {
	// AddSource causes the handler to compute the source code position
	// of the log statement and add a SourceKey attribute to the output.
	AddSource bool

	// Level reports the minimum record level that will be logged.
	// The handler discards records with lower levels.
	// If Level is nil, the handler assumes LevelInfo.
	// The handler calls Level.Level for each record processed;
	// to adjust the minimum level dynamically, use a LevelVar.
	Level Level

	// ReplaceAttr is called to rewrite each non-group attribute before it is logged.
	// The attribute's value has been resolved (see [Value.Resolve]).
	// If ReplaceAttr returns a zero Attr, the attribute is discarded.
	//
	// The built-in attributes with keys "time", "level", "source", and "msg"
	// are passed to this function, except that time is omitted
	// if zero, and source is omitted if AddSource is false.
	//
	// The first argument is a list of currently open groups that contain the
	// Attr. It must not be retained or modified. ReplaceAttr is never called
	// for Group attributes, only their contents. For example, the attribute
	// list
	//
	//     Int("a", 1), Group("g", Int("b", 2)), Int("c", 3)
	//
	// results in consecutive calls to ReplaceAttr with the following arguments:
	//
	//     nil, Int("a", 1)
	//     []string{"g"}, Int("b", 2)
	//     nil, Int("c", 3)
	//
	// ReplaceAttr can be used to change the default keys of the built-in
	// attributes, convert types (for example, to replace a `time.Time` with the
	// integer seconds since the Unix epoch), sanitize personal information, or
	// remove attributes from the output.
	ReplaceAttr func(groups []string, a Attr) Attr

	// 前端日志写入接口
	Writer io.Writer

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

type TextHandler

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

func NewTextHandler

func NewTextHandler(out io.Writer, opts *slog.HandlerOptions) *TextHandler

func (*TextHandler) Enabled

func (h *TextHandler) Enabled(_ context.Context, level slog.Level) bool

func (*TextHandler) Handle

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

func (*TextHandler) WithAttrs

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

func (*TextHandler) WithGroup

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

Jump to

Keyboard shortcuts

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