slogx

package module
v0.0.0-...-b930f50 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README ΒΆ

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

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

View Source
const (
	SizeMB = 1024 * 1024

	RotateTimeLayoutEveryYear    = "2006"
	RotateTimeLayoutEveryMonth   = "200601"
	RotateTimeLayoutEveryDay     = "20060102"
	RotateTimeLayoutEveryHour    = "2006010215"
	RotateTimeLayoutEveryMinutes = "200601021504"
	RotateTimeLayoutEverySecond  = "20060102150405"
)

Variables ΒΆ

This section is empty.

Functions ΒΆ

func FromContext ΒΆ

func FromContext(ctx context.Context) *slog.Logger

FromContext returns the Logger stored in ctx by NewContext, or the default Logger if there is none.

func NewContext ΒΆ

func NewContext(ctx context.Context, l *slog.Logger) context.Context

NewContext returns a context that contains the given Logger. Use FromContext to retrieve the Logger.

Types ΒΆ

type FileWriter ΒΆ

type FileWriter struct {
	// contains filtered or unexported fields
}

func NewFileWriter ΒΆ

func NewFileWriter(opts ...FileWriterOption) (*FileWriter, error)

func (*FileWriter) Write ΒΆ

func (w *FileWriter) Write(p []byte) (n int, err error)

type FileWriterOption ΒΆ

type FileWriterOption interface {
	// contains filtered or unexported methods
}

func WithCompress ΒΆ

func WithCompress(compress bool) FileWriterOption

func WithFileName ΒΆ

func WithFileName(fileName string) FileWriterOption

func WithMaxCount ΒΆ

func WithMaxCount(maxCount int) FileWriterOption

func WithMaxTime ΒΆ

func WithMaxTime(maxTime time.Duration) FileWriterOption

func WithPath ΒΆ

func WithPath(path string) FileWriterOption

func WithRotateSize ΒΆ

func WithRotateSize(rotateSize int) FileWriterOption

func WithRotateTimeLayout ΒΆ

func WithRotateTimeLayout(rotateTimeLayout string) FileWriterOption

type FileWriterOptionFn ΒΆ

type FileWriterOptionFn struct {
	// contains filtered or unexported fields
}

func NewFileWriterOptionFn ΒΆ

func NewFileWriterOptionFn(f func(*FileWriterOptions)) *FileWriterOptionFn

type FileWriterOptions ΒΆ

type FileWriterOptions struct {
	Path             string
	FileName         string
	MaxTime          time.Duration
	MaxCount         int
	RotateTimeLayout string
	// RotateSize is the maximum size in MB of the log file before it gets rotated.
	// It defaults to 100 MB.
	RotateSize int
	Compress   bool
}

func (FileWriterOptions) NewFileWriter ΒΆ

func (opts FileWriterOptions) NewFileWriter() (*FileWriter, error)

type Handler ΒΆ

type Handler struct {
	// contains filtered or unexported fields
}

func NewHandler ΒΆ

func NewHandler(w io.Writer, opts *HandlerOptions) *Handler

func (*Handler) Enabled ΒΆ

func (h *Handler) Enabled(ctx context.Context, level slog.Level) bool

func (*Handler) Handle ΒΆ

func (h *Handler) Handle(ctx context.Context, record slog.Record) error

func (*Handler) WithAttrs ΒΆ

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup ΒΆ

func (h *Handler) WithGroup(name string) slog.Handler

type HandlerOptions ΒΆ

type HandlerOptions struct {
	Level         slog.Leveler
	Prefix        string
	AddSource     bool
	AddCaller     bool
	AddStackTrace bool
	ReplaceAttr   func(groups []string, a slog.Attr) slog.Attr
}

func (HandlerOptions) NewHandler ΒΆ

func (opts HandlerOptions) NewHandler(w io.Writer) *Handler

type MultiHandler ΒΆ

type MultiHandler struct {
	// contains filtered or unexported fields
}

func NewMultiHandler ΒΆ

func NewMultiHandler(handlers ...slog.Handler) *MultiHandler

func (*MultiHandler) Enabled ΒΆ

func (h *MultiHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*MultiHandler) Handle ΒΆ

func (h *MultiHandler) Handle(ctx context.Context, record slog.Record) error

func (*MultiHandler) WithAttrs ΒΆ

func (h *MultiHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*MultiHandler) WithGroup ΒΆ

func (h *MultiHandler) WithGroup(name string) slog.Handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL