xlog

package
v0.0.0-...-791c70a Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package xlog extends log/slog with some features.

Index

Constants

View Source
const (
	LevelDebug = slog.LevelDebug
	LevelInfo  = slog.LevelInfo
	LevelWarn  = slog.LevelWarn
	LevelError = slog.LevelError
)

alias

Variables

This section is empty.

Functions

func Debug

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

Debug calls Logger.Debug on the default logger.

func DebugContext

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

DebugContext calls Logger.DebugContext on the default logger.

func Debugf

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

Debugf calls Logger.Debugf on the default logger.

func DebugfContext

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

DebugfContext calls Logger.DebugfContext on the default logger.

func Error

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

Error calls Logger.Error on the default logger.

func ErrorContext

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

ErrorContext calls Logger.ErrorContext on the default logger.

func Errorf

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

Errorf calls Logger.Errorf on the default logger.

func ErrorfContext

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

ErrorfContext calls Logger.ErrorfContext on the default logger.

func Info

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

Info calls Logger.Info on the default logger.

func InfoContext

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

InfoContext calls Logger.InfoContext on the default logger.

func Infof

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

Infof calls Logger.Infof on the default logger.

func InfofContext

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

InfofContext calls Logger.InfofContext on the default logger.

func Log

func Log(ctx context.Context, level slog.Level, msg string, args ...any)

Log calls Logger.Log on the default logger.

func LogAttrs

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

LogAttrs calls Logger.LogAttrs on the default logger.

func MultiHandler

func MultiHandler(handlers ...slog.Handler) slog.Handler

MultiHandler distributes records to multiple slog.Handler.

func NewLevelVar

func NewLevelVar(lvl slog.Level) *slog.LevelVar

NewLevelVar returns a *slog.LevelVar with input level.

func SetDefault

func SetDefault(l *Logger)

SetDefault makes l the default Logger. After this call, output from the log package's default Logger (as with log.Print, etc.) will be logged at LevelInfo using l's Handler.

func SetHandlerLevel

func SetHandlerLevel(h slog.Handler, lvl slog.Level)

SetHandlerLevel asserts input slog.Handler as LeveledHandler and call SetLevel() method.

func SetLevel

func SetLevel(lvl slog.Level)

SetLevel calls Logger.SetLevel on the default logger.

func Warn

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

Warn calls Logger.Warn on the default logger.

func WarnContext

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

WarnContext calls Logger.WarnContext on the default logger.

func Warnf

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

Warnf calls Logger.Warnf on the default logger.

func WarnfContext

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

WarnfContext calls Logger.WarnfContext on the default logger.

Types

type Attr

type Attr = slog.Attr

Attr alias slog.Attr

type AttrReplacer

type AttrReplacer func(groups []string, attr slog.Attr) Attr

AttrReplacer is called to rewrite each non-group attribute before it is logged.

func ChainReplacer

func ChainReplacer(replacers ...AttrReplacer) AttrReplacer

ChainReplacer calls replacers in order.

func NormalizeSourceAttrReplacer

func NormalizeSourceAttrReplacer() AttrReplacer

NormalizeSourceAttrReplacer replaces source file path as basename.

func SuppressTimeAttrReplacer

func SuppressTimeAttrReplacer() AttrReplacer

SuppressTimeAttrReplacer removes the top-level time attribute. It is intended to be used as a ReplaceAttr function, to make example output deterministic.

type Config

type Config struct {
	// Level 日志输出级别, 默认为 LevelInfo
	Level slog.Level
	// AddSource 是否输出日志所在文件和位置
	AddSource bool
	// AttrReplacer 重写特定属性, 默认 BasenameSourceAttrReplacer
	AttrReplacer AttrReplacer

	// StdFormat 标准输出的格式, 可选值: ["text", "json"]
	StdFormat string
	// StdWriter 标准输出的 io.Writer, 默认为 os.Stdout
	StdWriter io.Writer

	// Path 日志文件路径, 如果为空表示不输出日志到文件
	Path string
	// MaxSize 单个日志文件最大体积, 单位为 MB, 超过该大小自动切分, 默认为 30 MB
	MaxSize int
	// MaxAge 日志文件最多保留的天数, 默认一直保留
	MaxAge int
	// MaxBackups 日志文件最多保留的个数, 默认一直保留
	MaxBackups int
	// Compress 是否压缩切片的日志文件, 默认不压缩
	Compress bool
}

Config 日志配置

func NewConfig

func NewConfig() Config

NewConfig 返回默认日志配置

func (*Config) BuildHandler

func (c *Config) BuildHandler() slog.Handler

BuildHandler creates a new slog.Handler with config.

type Handler

type Handler = slog.Handler

Handler alias slog.Handler

type HandlerCreator

type HandlerCreator func(w io.Writer, opts *slog.HandlerOptions) slog.Handler

HandlerCreator is a function type to create slog.Handler.

var (
	// JSONHandlerCreator wraps slog.NewJSONHandler as HandlerCreator
	JSONHandlerCreator HandlerCreator = func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
		return slog.NewJSONHandler(w, opts)
	}
	// TextHandlerCreator wraps slog.NewTextHandler as HandlerCreator
	TextHandlerCreator HandlerCreator = func(w io.Writer, opts *slog.HandlerOptions) slog.Handler {
		return slog.NewTextHandler(w, opts)
	}
)

func NewLeveledHandlerCreator

func NewLeveledHandlerCreator(create HandlerCreator) HandlerCreator

NewLeveledHandlerCreator wraps a HandlerCreator to create a LeveledHandler.

type Level

type Level = slog.Level

Level alias slog.Level

type LeveledHandler

type LeveledHandler interface {
	slog.Handler
	// SetLevel changes level dynamicly.
	SetLevel(lvl slog.Level)
}

LeveledHandler wraps slog.Handler with SetLevel() method.

type Logger

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

Logger extends slog.Logger with additional features.

func Default

func Default() *Logger

Default returns the default Logger.

func New

func New(c Config) *Logger

New creates a new Logger with the given non-nil Handler.

func (*Logger) AddCallerSkip

func (l *Logger) AddCallerSkip(skip int) *Logger

AddCallerSkip increases the number of callers skipped by caller annotation.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug logs at LevelDebug.

func (*Logger) DebugContext

func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs at LevelDebug with the given context.

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...any)

Debugf logs at LevelDebug with the given format.

func (*Logger) DebugfContext

func (l *Logger) DebugfContext(ctx context.Context, format string, args ...any)

DebugfContext logs at LevelDebug with the given format and context.

func (*Logger) Enabled

func (l *Logger) Enabled(level slog.Level) bool

Enabled reports whether l emits log records at the given context and level.

func (*Logger) EnabledContext

func (l *Logger) EnabledContext(ctx context.Context, level slog.Level) bool

EnabledContext reports whether l emits log records at the given context and level.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...any)

Error logs at LevelError.

func (*Logger) ErrorContext

func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)

ErrorContext logs at LevelError with the given context.

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...any)

Errorf logs at LevelError with the given format.

func (*Logger) ErrorfContext

func (l *Logger) ErrorfContext(ctx context.Context, format string, args ...any)

ErrorfContext logs at LevelError with the given format and context.

func (*Logger) Handler

func (l *Logger) Handler() slog.Handler

Handler returns l's Handler.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...any)

Info logs at LevelInfo.

func (*Logger) InfoContext

func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)

InfoContext logs at LevelInfo with the given context.

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...any)

Infof logs at LevelInfo with the given format.

func (*Logger) InfofContext

func (l *Logger) InfofContext(ctx context.Context, format string, args ...any)

InfofContext logs at LevelInfo with the given format and context.

func (*Logger) Log

func (l *Logger) Log(ctx context.Context, level slog.Level, msg string, args ...any)

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".

func (*Logger) LogAttrs

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

LogAttrs is a more efficient version of Logger.Log that accepts only Attrs.

func (*Logger) Logf

func (l *Logger) Logf(ctx context.Context, level slog.Level, format string, args ...any)

Logf is a helper method to format message with args instead of Attrs.

func (*Logger) SetLevel

func (l *Logger) SetLevel(lvl slog.Level)

SetLevel supports to change level dynamicly.

func (*Logger) Warn

func (l *Logger) Warn(msg string, args ...any)

Warn logs at LevelWarn.

func (*Logger) WarnContext

func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)

WarnContext logs at LevelWarn with the given context.

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...any)

Warnf logs at LevelWarn with the given format.

func (*Logger) WarnfContext

func (l *Logger) WarnfContext(ctx context.Context, format string, args ...any)

WarnfContext logs at LevelWarn with the given format and context.

func (*Logger) With

func (l *Logger) With(args ...any) *Logger

With returns a Logger that includes the given attributes in each output operation. Arguments are converted to attributes as if by Logger.Log.

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *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.

Jump to

Keyboard shortcuts

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