Categorygithub.com/mpuzanov/wslog
repositorypackage
1.0.0
Repository: https://github.com/mpuzanov/wslog.git
Documentation: pkg.go.dev

# README

Wrapper for slog

Install

go get github.com/mpuzanov/wslog

Added methods

// NewEnv Creating a logger based on Env (local, dev, prod)  
func NewEnv(env string) *Logger

// New create logger  
func New(opts ...LoggerOption) *Logger

Examples

logger := wslog.New(
		wslog.WithLevel(cfg.Log.Level),
		wslog.WithIsJSON(cfg.Log.IsJSON),
		wslog.WithFileLog(cfg.Log.File),
		wslog.WithAddSource(cfg.Log.AddSource),
	)

or

logger := wslog.NewEnv(cfg.Env)
logger.Debug("debug", wslog.Any("cfg", cfg))

logger.Info(cfg.ServiceName,
		wslog.String("version", config.Version),
		wslog.String("time", config.Time),
		wslog.String("log_level", cfg.Log.Level),
	)

adds an slog attribute to the provided context


ctx := r.Context()
reqID := uuid.New().String()
ctx = wslog.AppendCtx(ctx, slog.String("reqID", reqID))

slog.InfoContext(ctx, "request",
	slog.String("user_ip", r.RemoteAddr),
	slog.String("path", r.URL.Path),
	slog.String("method", r.Method),
)