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 NewOptions(options *slog.HandlerOptions) *slog.HandlerOptions
- func NewTextHandler(w io.Writer, opts *HandlerOptions) *slog.TextHandler
- func Printf(format 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)
- 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) GetLevel() Level
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) Infof(format string, args ...any)
- func (l *Logger) Log(parent context.Context, level Level, msg string, args ...any)
- func (l *Logger) LogAttrs(parent context.Context, level Level, msg string, attrs ...slog.Attr)
- func (l *Logger) Printf(format string, args ...any)
- func (l *Logger) Println(msg string, args ...any)
- func (l *Logger) Trace(msg string, args ...any)
- func (l *Logger) Tracef(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) WithContext(ctx context.Context) *Logger
- func (l *Logger) WithGroup(name string) *Logger
- func (l *Logger) WithValue(key string, val any) *Logger
- type Record
- type Value
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 DisableJsonLogger ¶
func DisableJsonLogger()
func DisableTextLogger ¶
func DisableTextLogger()
func EnableJsonLogger ¶
func EnableJsonLogger()
func EnableTextLogger ¶
func EnableTextLogger()
func GetChannel ¶
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 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 SetJsonLogger ¶
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 ¶
Types ¶
type HandlerOptions ¶ added in v0.0.2
type HandlerOptions = slog.HandlerOptions
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger 结构体定义了两个不同格式的日志记录器:文本记录器和 JSON 记录器。
func WithContext ¶ added in v0.0.3
WithContext 使用给定的上下文
func (*Logger) WithContext ¶ added in v0.0.7
WithContext 使用给定的上下文
Source Files ¶
Click to show internal directories.
Click to hide internal directories.