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
- Variables
- func AddSource(options *slog.HandlerOptions)
- func Debug(msg string, args ...any)
- func Debugf(format string, args ...any)
- func DisableColor(b bool)
- func DisableJsonLogger()
- func DisableTextLogger()
- func EnableJsonLogger()
- func EnableTextLogger()
- func Error(msg string, args ...any)
- func Errorf(format string, args ...any)
- func GetChannel() chan slog.Record
- func GetHandler() slog.Handler
- func Info(msg string, args ...any)
- func Infof(format string, args ...any)
- func NewHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- func Panic(msg string, args ...any)
- func Panicf(format string, args ...any)
- func Printf(msg string, args ...any)
- func Println(msg string, args ...any)
- func SetJsonLogger(writer io.Writer, addSource bool)
- func SetLevelDebug()
- func SetLevelError()
- func SetLevelFatal()
- func SetLevelInfo()
- func SetLevelTrace()
- func SetLevelWarn()
- func SetTextLogger(writer io.Writer, addSource bool)
- func Trace(msg string, args ...any)
- func Tracef(format string, args ...any)
- func Warn(msg string, args ...any)
- func Warnf(format string, args ...any)
- func WithValue(key string, val any) context.Context
- type Attr
- func Any(key string, value any) Attr
- func Bool(key string, v bool) Attr
- func Duration(key string, v time.Duration) Attr
- func Float64(key string, v float64) Attr
- func Group(key string, args ...any) Attr
- func Int(key string, v int) Attr
- func Int64(key string, v int64) Attr
- func String(key string, v string) Attr
- func Time(key string, v time.Time) Attr
- func Uint64(key string, v uint64) Attr
- type Logger
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) Debugf(format string, args ...any)
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) Errorf(format string, args ...any)
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) Infof(format string, args ...any)
- func (l *Logger) Log(parent context.Context, level slog.Level, msg string, args ...any)
- func (l *Logger) LogAttrs(ctx context.Context, level slog.Level, msg string, attrs ...Attr)
- func (l *Logger) Panic(msg string, args ...any)
- func (l *Logger) Panicf(format string, args ...any)
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) Warnf(format string, args ...any)
- func (l *Logger) With(args ...any) *Logger
- func (l *Logger) WithGroup(name string) *Logger
Constants ¶
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 Debug ¶
Debug 记录调试消息。
log.Debug("hello world")
log.Debug("hello world", "age", 18, "name", "foo")
func Error ¶
Error 记录错误消息。
log.Error("hello world")
log.Error("hello world", "age", 18, "name", "foo")
func NewHandler ¶
NewHandler returns a log/slog.Handler using the receiver's options. Default options are used if opts is nil.
func Panic ¶
Panic 记录错误消息并以 `1` 错误代码退出当前程序。
log.Panic("hello world")
log.Panic("hello world", "age", 18, "name", "foo")
func Panicf ¶
Panicf 记录并格式化错误消息并以 `1` 错误代码退出当前程序。不能使用属性。
log.Panicf("hello world")
log.Panicf("hello %s", "world")
func Printf ¶
Printf 记录调试消息。
log.Printf("hello world")
log.Printf("hello world", "age", 18, "name", "foo")
func Println ¶
Println 记录调试消息。
log.Println("hello world")
log.Println("hello world", "age", 18, "name", "foo")
func SetJsonLogger ¶
SetJsonLogger 设置并启用 JSON 记录器。
func Trace ¶
Trace 记录跟踪消息。
log.Trace("hello world")
log.Trace("hello world", "age", 18, "name", "foo")
Types ¶
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger 结构体定义了两个不同格式的日志记录器:文本记录器和 JSON 记录器。
Source Files
¶
Click to show internal directories.
Click to hide internal directories.