slog

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 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"
View Source
const Version = "v1.0.0"

Variables

View Source
var (
	TimeFormat     = "2006/01/02 03:04.05.000"
	LevelTextNames = map[slog.Leveler]string{
		LevelInfo:  "I",
		LevelDebug: "D",
		LevelWarn:  "W",
		LevelError: "E",
		LevelTrace: "T",
		LevelFatal: "F",
	}
)
View Source
var (
	LevelJsonNames = map[slog.Leveler]string{
		LevelInfo:  "Info",
		LevelDebug: "Debug",
		LevelWarn:  "Warn",
		LevelError: "Error",
		LevelTrace: "Trace",
		LevelFatal: "Fatal",
	}
)

Functions

func AddSource

func AddSource(options *slog.HandlerOptions)

AddSource 将源添加到 slog 处理程序选项。

func Context added in v0.0.2

func Context(parent context.Context) context.Context

Context 使用给定的上下文

func Debug

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

Debug 记录调试消息。

log.Debug("hello world")
log.Debug("hello world", "age", 18, "name", "foo")

func Debugf

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

Debugf 记录并格式化调试消息。不能使用属性。

log.Debugf("hello world")
log.Debugf("hello %s", "world")

func DisableColor

func DisableColor(b bool)

DisableColor 禁用颜色

func DisableJsonLogger

func DisableJsonLogger()

DisableJsonLogger 禁用 JSON 记录器。

func DisableTextLogger

func DisableTextLogger()

DisableTextLogger 禁用文本记录器。

func EnableJsonLogger

func EnableJsonLogger()

EnableJsonLogger 启用 JSON 记录器。

func EnableTextLogger

func EnableTextLogger()

EnableTextLogger 启用文本记录器。

func Error

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

Error 记录错误消息。

log.Error("hello world")
log.Error("hello world", "age", 18, "name", "foo")

func Errorf

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

Errorf 记录并格式化错误消息。不能使用属性。

log.Errorf("hello world")
log.Errorf("hello %s", "world")

func Fatal added in v0.0.2

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

Fatal 记录错误消息并以 `1` 错误代码退出当前程序。

log.Fatal("hello world")
log.Fatal("hello world", "age", 18, "name", "foo")

func Fatalf added in v0.0.2

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

Fatalf 记录并格式化错误消息并以 `1` 错误代码退出当前程序。不能使用属性。

log.Fatalf("hello world")
log.Fatalf("hello %s", "world")

func GetChannel

func GetChannel() chan slog.Record

GetChannel 获取通道数据,若通道不存在则自动初始化。

func Info

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

Info 记录信息消息。

log.Info("hello world")
log.Info("hello world", "age", 18, "name", "foo")

func Infof

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

Infof 记录并格式化信息消息。不能使用属性。

log.Infof("hello world")
log.Infof("hello %s", "world")

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 NewTextHandler added in v0.0.2

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

func Printf

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

Printf 记录打印消息,为了兼容fmt.Printf风格输出

log.Printf("hello world")
log.Printf("hello world", "age", 18, "name", "foo")

func Println

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

Println 记录调试消息,为了兼容fmt.Println风格输出

log.Println("hello world")
log.Println("hello world", "age", 18, "name", "foo")

func SetJsonLogger

func SetJsonLogger(writer io.Writer, addSource bool)

SetJsonLogger 设置并启用 JSON 记录器。

func SetLevelDebug

func SetLevelDebug()

SetLevelDebug 将默认记录器的级别设置为调试。

func SetLevelError

func SetLevelError()

SetLevelError 将默认记录器的级别设置为错误。

func SetLevelFatal

func SetLevelFatal()

SetLevelFatal 将默认记录器的级别设置为错误。

func SetLevelInfo

func SetLevelInfo()

SetLevelInfo 将默认记录器的级别设置为信息。

func SetLevelTrace

func SetLevelTrace()

LevelTrace 将默认记录器的级别设置为跟踪。

func SetLevelWarn

func SetLevelWarn()

SetLevelWarn 将默认记录器的级别设置为警告。

func SetTextLogger

func SetTextLogger(writer io.Writer, addSource bool)

SetTextLogger 设置并启用文本记录器。

func Trace

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

Trace 记录跟踪消息。

log.Trace("hello world")
log.Trace("hello world", "age", 18, "name", "foo")

func Tracef

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

Tracef 记录并格式化跟踪消息。不能使用属性。

log.Tracef("hello world")
log.Tracef("hello %s", "world")

func Warn

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

Warn 记录警告消息。

log.Warn("hello world")
log.Warn("hello world", "age", 18, "name", "foo")

func Warnf

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

Warnf 记录并格式化警告消息。不能使用属性。

log.Warnf("hello world")
log.Warnf("hello %s", "world")

func WithValue

func WithValue(parent context.Context, key string, val any) context.Context

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

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

func NewOptions added in v0.0.2

func NewOptions(options *HandlerOptions) *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() *Logger

Default 返回默认记录器。

func Prefix added in v0.0.2

func Prefix(key string) *Logger

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) 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 slog.Level, msg string, args ...any)

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

func (*Logger) LogAttrs

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

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

func (*Logger) Printf added in v0.0.2

func (l *Logger) Printf(msg 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) 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) WithGroup

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

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

func (*Logger) WithValue added in v0.0.2

func (l *Logger) WithValue(parent context.Context, key string, val any) context.Context

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

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