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 DisableJsonLogger()
- func DisableTextLogger()
- func EnableJsonLogger()
- func EnableTextLogger()
- func Error(msg string, args ...any)
- func Errorf(format string, args ...any)
- func Fatal(msg string, args ...any)
- func Fatalf(format string, args ...any)
- func GetChannel() chan slog.Record
- func Info(msg string, args ...any)
- func Infof(format string, args ...any)
- func New(handler slog.Handler) *slog.Logger
- func NewConsoleHandler(w io.Writer, opts *slog.HandlerOptions) slog.Handler
- func NewJSONHandler(w io.Writer, opts *HandlerOptions) *slog.JSONHandler
- func NewTextHandler(w io.Writer, opts *HandlerOptions) *slog.TextHandler
- 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, noColor, 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 WithContext(parent context.Context) context.Context
- func WithValue(parent context.Context, 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 HandlerOptions
- type Level
- 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) Fatal(msg string, args ...any)
- func (l *Logger) Fatalf(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(parent context.Context, level slog.Level, msg string, attrs ...Attr)
- func (l *Logger) Printf(msg string, args ...any)
- func (l *Logger) Println(msg string, args ...any)
- func (l *Logger) Trace(msg string, args ...any)
- func (l *Logger) Tracef(msg 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
- func (l *Logger) WithValue(parent context.Context, key string, val any) context.Context
- type Value
Constants ¶
const ( TimeKey = slog.TimeKey LevelKey = slog.LevelKey MessageKey = slog.MessageKey SourceKey = slog.SourceKey )
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) )
const Name = "slog"
const Version = "v1.0.0"
Variables ¶
var (
TimeFormat = "2006/01/02 03:04.05.000"
)
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 Fatal ¶ added in v0.0.2
Fatal 记录错误消息并以 `1` 错误代码退出当前程序。
log.Fatal("hello world") log.Fatal("hello world", "age", 18, "name", "foo")
func Fatalf ¶ added in v0.0.2
Fatalf 记录并格式化错误消息并以 `1` 错误代码退出当前程序。不能使用属性。
log.Fatalf("hello world") log.Fatalf("hello %s", "world")
func NewConsoleHandler ¶ added in v0.0.2
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 ¶
Printf 记录打印消息,为了兼容fmt.Printf风格输出
log.Printf("hello world") log.Printf("hello world", "age", 18, "name", "foo")
func Println ¶
Println 记录调试消息,为了兼容fmt.Println风格输出
log.Println("hello world") log.Println("hello world", "age", 18, "name", "foo")
func SetJsonLogger ¶
SetJsonLogger 设置并启用 JSON 记录器。
func SetTextLogger ¶
SetTextLogger 设置并启用文本记录器。
func Trace ¶
Trace 记录跟踪消息。
log.Trace("hello world") log.Trace("hello world", "age", 18, "name", "foo")
func WithContext ¶ added in v0.0.3
WithContext 使用给定的上下文
Types ¶
type HandlerOptions ¶ added in v0.0.2
type HandlerOptions = slog.HandlerOptions
func NewOptions ¶ added in v0.0.2
func NewOptions(options *HandlerOptions) *HandlerOptions
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger 结构体定义了两个不同格式的日志记录器:文本记录器和 JSON 记录器。