slog icon indicating copy to clipboard operation
slog copied to clipboard

slog: failed to handle log, error: write ./logs/info.log: file already closed

Open qmdx opened this issue 2 months ago • 0 comments

windows 自动创建日志目录异常 slog: failed to handle log, error: write ./logs/info.log: file already closed

import (
	"github.com/gookit/slog"
	"github.com/gookit/slog/handler"
	"github.com/gookit/slog/rotatefile"
)

func Slog() {
	defer slog.MustClose()

	// DangerLevels 包含: slog.PanicLevel, slog.ErrorLevel, slog.WarnLevel
	h1 := handler.MustRotateFile("./logs/error.log", rotatefile.EveryDay,
		handler.WithLogLevels(slog.DangerLevels),
		handler.WithCompress(true),
	)

	// NormalLevels 包含: slog.InfoLevel, slog.NoticeLevel, slog.DebugLevel, slog.TraceLevel
	h2 := handler.MustFileHandler("./logs/info.log", handler.WithLogLevels(slog.NormalLevels))

	// 注册 handler 到 logger(调度器)
	slog.PushHandlers(h1, h2)

	// add logs
	slog.Info("info message text")
	slog.Error("error message text")
}

qmdx avatar Apr 19 '24 07:04 qmdx