slogexp

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT Imports: 7 Imported by: 0

README

slog-exp

slog experimental features such individual log files for different levels, and other extensions.

GitHub GitHub Action Go Report Card GoDoc

See also:

Installation

go get github.com/smllnest/slog-clickhouse

Compatibility: go >= 1.21

Usage

LevelHandler

Set different handlers for different log levels. For example, you can set different handlers to save files for different log levels, such as info, warn, and error.

    infoFile, err := os.OpenFile("testdata/info.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
    if err != nil {
        panic(err)
    }
    defer infoFile.Close()

    warnFile, err := os.OpenFile("testdata/warn.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
    if err != nil {
        panic(err)
    }
    defer warnFile.Close()

    errorFile, err := os.OpenFile("testdata/error.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
    if err != nil {
        panic(err)
    }
    defer errorFile.Close()

    infoHandler := slog.NewTextHandler(infoFile, &slog.HandlerOptions{Level: slog.LevelInfo})
    warnHandler := slog.NewTextHandler(warnFile, &slog.HandlerOptions{Level: slog.LevelWarn})
    errorHandler := slog.NewTextHandler(errorFile, &slog.HandlerOptions{Level: slog.LevelError})

    handler := NewLevelHandler(map[slog.Level]slog.Handler{
        slog.LevelInfo:  infoHandler,
        slog.LevelWarn:  warnHandler,
        slog.LevelError: errorHandler,
    })

    logger := slog.New(handler)

    logger.Info("info text")
    logger.Warn("warn text")
    logger.Error("error text")
customized datetime and source

use ReplaceTimeAttr and ReplaceSourceAttr.

	handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
		ReplaceAttr: WrapReplaceAttrFunc(ReplaceTimeAttr(time.TimeOnly), ReplaceSourceAttr()),
		AddSource:   true,
	})
	logger := slog.New(handler)

	logger.Info("info text")
	logger.Warn("warn text")
	logger.Error("error text")
goutine id
handler := NewLevelHandler(map[slog.Level]slog.Handler{
		slog.LevelInfo:  infoHandler,
		slog.LevelWarn:  warnHandler,
		slog.LevelError: errorHandler,
	}).WithAttrs([]slog.Attr{
		slog.Int64("gid", gid.ID()),
	})

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBufferFull = errors.New("buffer full")

Functions

func Buffer

func Buffer(h slog.Handler, bufferSize int, block bool) slog.Handler

Buffer returns a new handler that buffers records and sends them to the given handler. If block is true, the buffer will block when it is full. If block is false, the buffer will discard the record if it is full.

func NewNopHandler

func NewNopHandler() slog.Handler

NewNopHandler creates a slog handler that discards all log messages.

func NewNopLogger

func NewNopLogger() *slog.Logger

NewNopLogger creates a slog logger that discards all log messages.

Types

type LevelHandler

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

LevelHandler is a handler that logs records with levels greater than or equal to a given level using the given handler.

Example
infoFile, err := os.OpenFile("testdata/info.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
	panic(err)
}
defer infoFile.Close()

warnFile, err := os.OpenFile("testdata/warn.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
	panic(err)
}
defer warnFile.Close()

errorFile, err := os.OpenFile("testdata/error.log", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
if err != nil {
	panic(err)
}
defer errorFile.Close()

infoHandler := slog.NewTextHandler(infoFile, &slog.HandlerOptions{Level: slog.LevelInfo})
warnHandler := slog.NewTextHandler(warnFile, &slog.HandlerOptions{Level: slog.LevelWarn})
errorHandler := slog.NewTextHandler(errorFile, &slog.HandlerOptions{Level: slog.LevelError})

handler := NewLevelHandler(map[slog.Level]slog.Handler{
	slog.LevelInfo:  infoHandler,
	slog.LevelWarn:  warnHandler,
	slog.LevelError: errorHandler,
})

logger := slog.New(handler)

logger.Info("info text")
logger.Warn("warn text")
logger.Error("error text")

func NewLevelHandler

func NewLevelHandler(levelHandlers map[slog.Level]slog.Handler) *LevelHandler

NewLevelHandler returns a new handler that logs records with levels greater than or equal to the given level using the given handler.

func (*LevelHandler) Enabled

func (h *LevelHandler) Enabled(_ context.Context, level slog.Level) bool

func (*LevelHandler) Handle

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

Handle logs the record if the level is greater than or equal to the handler's level.

func (*LevelHandler) WithAttrs

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

WithAttrs returns a new handler with the given attributes added to the existing attributes.

func (*LevelHandler) WithGroup

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

WithGroup returns a new handler with the given group added to the existing group.

type ReplaceAttrFunc

type ReplaceAttrFunc func(groups []string, a slog.Attr) slog.Attr

ReplaceAttrFunc is a function that replaces an attribute.

func ReplaceSourceAttr

func ReplaceSourceAttr() ReplaceAttrFunc

func ReplaceTimeAttr

func ReplaceTimeAttr(timeFormat string) ReplaceAttrFunc

ReplaceAttrFuncs is a list of ReplaceAttrFuncs.

func WrapReplaceAttrFunc

func WrapReplaceAttrFunc(fns ...ReplaceAttrFunc) ReplaceAttrFunc

WrapReplaceAttrFunc wraps a ReplaceAttrFunc.

Directories

Path Synopsis
package rotate is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.
package rotate is a port of File-RotateLogs from Perl (https://metacpan.org/release/File-RotateLogs), and it allows you to automatically rotate output files when you write to them according to the filename pattern that you can specify.

Jump to

Keyboard shortcuts

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