slog

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: MIT Imports: 15 Imported by: 6

README

说明

log 包封装了 slog 包,提供了更简单的接口。并且提供了一个全局的 logger(同时包含了使用[slog.TextHandler]和[slog.JSONHandler]的Logger),可以直接使用。

Example

slog.SetLevelInfo()
slog.Debugf("hello %s", "world")
slog.Infof("hello %s", "world")
slog.Warnf("hello %s", "world")
slog.Errorf("hello world")
slog.Debug("hello world", "age", 18)
slog.Info("hello world", "age", 18)
slog.Warn("hello world", "age", 18)
slog.Error("hello world", "age", 18)

l := log.Default()
l.LogAttrs(context.Background(), log.LevelInfo, "hello world", log.Int("age", 22))
l.Log(context.Background(), log.LevelInfo, "hello world", "age", 18)
l.Debugf("hello %s", "world")
l.Infof("hello %s", "world")
l.Warnf("hello %s", "world")
l.Errorf("hello world")

Documentation

Overview

log 包封装了 slog 包,提供了更简单的接口。并且提供了一个全局的 logger(同时包含了使用[slog.TextHandler]和[slog.JSONHandler]的Logger),可以直接使用。 package log encapsulates the slog package and provides a simpler interface. And provides a global logger (which contains both the Logger using slog.TextHandler and slog.JSONHandler), which can be used directly.

Example

log.SetLevelInfo()
log.Debugf("hello %s", "world")
log.Infof("hello %s", "world")
log.Warnf("hello %s", "world")
log.Errorf("hello world")
log.Debug("hello world", "age", 18)
log.Info("hello world", "age", 18)
log.Warn("hello world", "age", 18)
log.Error("hello world", "age", 18)

l := log.Default()
l.LogAttrs(context.Background(), log.LevelInfo, "hello world", log.Int("age", 22))
l.Log(context.Background(), log.LevelInfo, "hello world", "age", 18)
l.Debugf("hello %s", "world")
l.Infof("hello %s", "world")
l.Warnf("hello %s", "world")
l.Errorf("hello world")

Index

Constants

View Source
const (
	TimeKey    = slog.TimeKey
	LevelKey   = slog.LevelKey
	MessageKey = slog.MessageKey
	SourceKey  = slog.SourceKey
)
View Source
const (
	LevelTrace = slog.Level(-8)
	LevelDebug = slog.Level(-4)
	LevelInfo  = slog.Level(0)
	LevelWarn  = slog.Level(4)
	LevelError = slog.Level(8)
	LevelFatal = slog.Level(12)
)
View Source
const (
	Name    = "slog"
	Version = "v1.0.0"
)

Variables

View Source
var (
	TimeFormat = "2006/01/02 03:04.05.000"
)

Functions

func AddSource

func AddSource(options *slog.HandlerOptions)

func Debug

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

对外公开的接口

func Debugf

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

func DisableJsonLogger

func DisableJsonLogger()

func DisableTextLogger

func DisableTextLogger()

func EnableJsonLogger

func EnableJsonLogger()

func EnableTextLogger

func EnableTextLogger()

func Error

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

func Errorf

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

func Fatal added in v0.0.2

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

func Fatalf added in v0.0.2

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

func GetChannel

func GetChannel() chan slog.Record

func Info

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

func Infof

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

func New added in v0.0.2

func New(handler slog.Handler) *slog.Logger

func NewConsoleHandler added in v0.0.2

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

NewConsoleHandler returns a log/slog.Handler using the receiver's options. Default options are used if opts is nil.

func NewJSONHandler added in v0.0.2

func NewJSONHandler(w io.Writer, opts *HandlerOptions) *slog.JSONHandler

func NewOptions added in v0.0.2

func NewOptions(options *slog.HandlerOptions) *slog.HandlerOptions

NewOptions 创建新的处理程序选项。

func NewTextHandler added in v0.0.2

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

func Printf

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

func Println

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

func SetJsonLogger

func SetJsonLogger(writer io.Writer, addSource bool)

func SetLevelDebug

func SetLevelDebug()

func SetLevelError

func SetLevelError()

func SetLevelFatal

func SetLevelFatal()

func SetLevelInfo

func SetLevelInfo()

func SetLevelTrace

func SetLevelTrace()

func SetLevelWarn

func SetLevelWarn()

func SetTextLogger

func SetTextLogger(writer io.Writer, noColor, addSource bool)

func Trace

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

func Tracef

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

func Warn

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

func Warnf

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

Types

type Attr

type Attr = slog.Attr

func Any

func Any(key string, value any) Attr

func Bool

func Bool(key string, v bool) Attr

func Duration

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

func Float64

func Float64(key string, v float64) Attr

func Group

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

func Int

func Int(key string, v int) Attr

func Int64

func Int64(key string, v int64) Attr

func String

func String(key string, v string) Attr

func Time

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

func Uint64

func Uint64(key string, v uint64) Attr

type HandlerOptions added in v0.0.2

type HandlerOptions = slog.HandlerOptions

type Level added in v0.0.2

type Level = slog.Level

type Logger

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

Logger 结构体定义了两个不同格式的日志记录器:文本记录器和 JSON 记录器。

func Default

func Default(module ...string) *Logger

func WithContext added in v0.0.3

func WithContext(ctx context.Context) *Logger

WithContext 使用给定的上下文

func WithValue

func WithValue(key string, val any) *Logger

WithValue 在上下文中存储一个键值对,并返回新的上下文

func (*Logger) Debug

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

Debug 方法用于记录调试级别的日志。

func (*Logger) Debugf

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

Debugf 方法用于格式化并记录调试级别的日志。

func (*Logger) Error

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

Error 方法用于记录错误级别的日志。

func (*Logger) Errorf

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

Errorf 方法用于格式化并记录错误级别的日志。

func (*Logger) Fatal added in v0.0.2

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

Fatal 方法用于记录严重错误级别的日志,并终止程序。

func (*Logger) Fatalf added in v0.0.2

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

Fatalf 方法用于格式化并记录严重错误级别的日志,并终止程序。

func (*Logger) GetLevel added in v0.0.9

func (l *Logger) GetLevel() Level

GetLevel 方法用于获取当前的日志等级

func (*Logger) Info

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

Info 方法用于记录信息级别的日志。

func (*Logger) Infof

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

Infof 方法用于格式化并记录信息级别的日志。

func (*Logger) Log

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

Log 方法用于记录指定级别的日志。

func (*Logger) LogAttrs

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

LogAttrs 方法用于记录具有指定属性的日志。

func (*Logger) Printf added in v0.0.2

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

Printf 为了兼容fmt.Printf风格输出

func (*Logger) Println added in v0.0.2

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

Println 为了兼容fmt.Println风格输出

func (*Logger) Trace added in v0.0.6

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

Trace 方法用于记录跟踪级别的日志。

func (*Logger) Tracef added in v0.0.6

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

Tracef 方法用于记录跟踪级别的日志。

func (*Logger) Warn

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

Warn 方法用于记录警告级别的日志。

func (*Logger) Warnf

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

Warnf 方法用于格式化并记录警告级别的日志。

func (*Logger) With

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

With 方法返回一个新的 Logger,其中包含给定的参数。

func (*Logger) WithContext added in v0.0.7

func (l *Logger) WithContext(ctx context.Context) *Logger

WithContext 使用给定的上下文

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *Logger

WithGroup 方法返回一个新的 Logger,其中包含给定的组名。

func (*Logger) WithValue added in v0.0.2

func (l *Logger) WithValue(key string, val any) *Logger

WithValue 在上下文中存储一个键值对,并返回新的上下文

type Record added in v0.0.8

type Record = slog.Record

type Value added in v0.0.2

type Value = slog.Value

func StringValue added in v0.0.2

func StringValue(value string) Value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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