Skip to content

Latest commit

 

History

History
107 lines (77 loc) · 2.54 KB

README.md

File metadata and controls

107 lines (77 loc) · 2.54 KB

slogx

tag Go Version GoDoc Go report Coverage Contributors License

slogx provides enhanced extensions for slog, including handler, writetr, format, middleware, etc.

🚀 Install

go get -u golang.org/x/exp/slog
import "github.com/three-body/slogx"

Compatibility: go >= 1.20.3

This library is v0 and follows SemVer strictly. On slog final release (go 1.21), this library will go v1.

No breaking changes will be made to exported APIs before v1.0.0.

💡 Usage

Handler

Handler

writer, err := slogx.NewFileWriter()
if err != nil {
    panic(err)
}

h := slogx.HandlerOptions{
    Level: slog.LevelError,
}.NewHandler(writer)

logger := slog.New(h)
slog.SetDefault(logger)

slog.Info("hello world")

MutilHandler

errHandler := slogx.HandlerOptions{
    Level: slog.LevelError,
}.NewHandler(os.Stderr)

infoHandler := slogx.HandlerOptions{
    Level: slog.LevelInfo,
}.NewHandler(os.Stdout)

h := slogx.NewMultiHandler(errHandler, infoHandler)
logger := slog.New(h)
slog.SetDefault(logger)

slog.Info("hello world")

Writer

Writer is a io.Writer that can write log to a file or a stream.

FileWriter

writer, err := slogx.FileWriterOptions{
    Path:             "./logs",
    FileName:         "app.log",
    MaxTime:          0,
    MaxCount:         0,
    RotateTimeLayout: slogx.RotateTimeLayoutEveryHour,
    RotateSize:       100,
    Compress:         false,
}.NewFileWriter()
if err != nil {
    panic(err)
}

logger := slog.New(slog.NewJSONHandler(writer))
slog.SetDefault(logger)

slog.Info("hello world")

KafkaWriter

RedisWriter

MysqlWriter

NsqWriter

Formatter

Formatter is a function that can format log entry to a string.

Middleware

📝 License

Copyright 2023 three-body.

This project is Apache-2.0 licensed.git