Documentation ¶
Overview ¶
Package xlog extends log/slog with some features.
Index ¶
- Constants
- func Debug(msg string, args ...any)
- func DebugContext(ctx context.Context, msg string, args ...any)
- func Debugf(format string, args ...any)
- func DebugfContext(ctx context.Context, format string, args ...any)
- func Error(msg string, args ...any)
- func ErrorContext(ctx context.Context, msg string, args ...any)
- func Errorf(format string, args ...any)
- func ErrorfContext(ctx context.Context, format string, args ...any)
- func Info(msg string, args ...any)
- func InfoContext(ctx context.Context, msg string, args ...any)
- func Infof(format string, args ...any)
- func InfofContext(ctx context.Context, format string, args ...any)
- func Log(ctx context.Context, level slog.Level, msg string, args ...any)
- func LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
- func MultiHandler(handlers ...slog.Handler) slog.Handler
- func NewLevelVar(lvl slog.Level) *slog.LevelVar
- func SetDefault(l *Logger)
- func SetHandlerLevel(h slog.Handler, lvl slog.Level)
- func SetLevel(lvl slog.Level)
- func Warn(msg string, args ...any)
- func WarnContext(ctx context.Context, msg string, args ...any)
- func Warnf(format string, args ...any)
- func WarnfContext(ctx context.Context, format string, args ...any)
- type Attr
- type AttrReplacer
- type Config
- type Handler
- type HandlerCreator
- type Level
- type LeveledHandler
- type Logger
- func (l *Logger) AddCallerSkip(skip int) *Logger
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Debugf(format string, args ...any)
- func (l *Logger) DebugfContext(ctx context.Context, format string, args ...any)
- func (l *Logger) Enabled(level slog.Level) bool
- func (l *Logger) EnabledContext(ctx context.Context, level slog.Level) bool
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Errorf(format string, args ...any)
- func (l *Logger) ErrorfContext(ctx context.Context, format string, args ...any)
- func (l *Logger) Handler() slog.Handler
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Infof(format string, args ...any)
- func (l *Logger) InfofContext(ctx context.Context, format string, args ...any)
- func (l *Logger) Log(ctx context.Context, level slog.Level, msg string, args ...any)
- func (l *Logger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr)
- func (l *Logger) Logf(ctx context.Context, level slog.Level, format string, args ...any)
- func (l *Logger) SetLevel(lvl slog.Level)
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Warnf(format string, args ...any)
- func (l *Logger) WarnfContext(ctx context.Context, format string, args ...any)
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithGroup(name string) *Logger
Constants ¶
const ( LevelDebug = slog.LevelDebug LevelInfo = slog.LevelInfo LevelWarn = slog.LevelWarn LevelError = slog.LevelError )
alias
Variables ¶
This section is empty.
Functions ¶
func DebugContext ¶
DebugContext calls Logger.DebugContext on the default logger.
func DebugfContext ¶
DebugfContext calls Logger.DebugfContext on the default logger.
func ErrorContext ¶
ErrorContext calls Logger.ErrorContext on the default logger.
func ErrorfContext ¶
ErrorfContext calls Logger.ErrorfContext on the default logger.
func InfoContext ¶
InfoContext calls Logger.InfoContext on the default logger.
func InfofContext ¶
InfofContext calls Logger.InfofContext on the default logger.
func MultiHandler ¶
MultiHandler distributes records to multiple slog.Handler.
func NewLevelVar ¶
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 ¶
SetHandlerLevel asserts input slog.Handler as LeveledHandler and call SetLevel() method.
func WarnContext ¶
WarnContext calls Logger.WarnContext on the default logger.
Types ¶
type AttrReplacer ¶
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 (*Config) BuildHandler ¶
BuildHandler creates a new slog.Handler with config.
type HandlerCreator ¶
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 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 (*Logger) AddCallerSkip ¶
AddCallerSkip increases the number of callers skipped by caller annotation.
func (*Logger) DebugContext ¶
DebugContext logs at LevelDebug with the given context.
func (*Logger) DebugfContext ¶
DebugfContext logs at LevelDebug with the given format and context.
func (*Logger) Enabled ¶
Enabled reports whether l emits log records at the given context and level.
func (*Logger) EnabledContext ¶
EnabledContext reports whether l emits log records at the given context and level.
func (*Logger) ErrorContext ¶
ErrorContext logs at LevelError with the given context.
func (*Logger) ErrorfContext ¶
ErrorfContext logs at LevelError with the given format and context.
func (*Logger) InfoContext ¶
InfoContext logs at LevelInfo with the given context.
func (*Logger) InfofContext ¶
InfofContext logs at LevelInfo with the given format and context.
func (*Logger) Log ¶
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 ¶
LogAttrs is a more efficient version of Logger.Log that accepts only Attrs.
func (*Logger) WarnContext ¶
WarnContext logs at LevelWarn with the given context.
func (*Logger) WarnfContext ¶
WarnfContext logs at LevelWarn with the given format and context.
func (*Logger) With ¶
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 ¶
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.