Skip to content

Provides enhanced extensions for slog, such as handler, writetr, format, middleware, etc.

License

Notifications You must be signed in to change notification settings

threebody-labs/slogx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Provides enhanced extensions for slog, such as handler, writetr, format, middleware, etc.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages