Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

需要一个动态设置日志级别的接口 #130

Open
konglinglong opened this issue Dec 13, 2023 · 0 comments
Open

需要一个动态设置日志级别的接口 #130

konglinglong opened this issue Dec 13, 2023 · 0 comments
Assignees
Labels
question Further information is requested

Comments

@konglinglong
Copy link

在程序运行过程,有时候为了定位问题,有动态设置日志级别的需求,但没有找到方便友好的设置接口。
slog.SetLogLevel(lvl)只在std logger上生效。
请问有没有方便友好的动态设置日志级别接口?

package logger

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

var (
	Log1   *slog.Logger
	Log2   *slog.Logger
	AppLog *slog.Record
	CfgLog *slog.Record
)

func init() {
	logTemplate := "[{{datetime}}] [{{level}}] {{message}}\n"

	formatter := slog.NewTextFormatter()
	formatter.TimeFormat = "2006-01-02 15:04:05.000"
	formatter.SetTemplate(logTemplate)
	slog.SetFormatter(formatter)

	h1, _ := handler.NewEmptyConfig(
		handler.WithLogfile("/var/log/rnxt/nas/nas.log"),
		handler.WithRotateTime(rotatefile.EveryDay),
		handler.WithBackupNum(10),
		handler.WithMaxSize(1024*1024*20),
		handler.WithLogLevels(slog.NormalLevels),
	).CreateHandler()
	h1.SetFormatter(formatter)

	h2 := handler.NewConsoleHandler(slog.AllLevels)
	h2.SetFormatter(formatter)

	Log1 = slog.NewWithHandlers(h1, h2)

	AppLog = Log1.WithFields(slog.M{
		"component": "APP",
	})
	CfgLog = Log1.WithFields(slog.M{
		"component": "CFG",
	})
}

func SetLogLevel(level string) {
	lvl, err := slog.Name2Level(level)
	if err != nil {
		AppLog.Fatalln("Failed to parse log level:", err)
	}
	slog.SetLogLevel(lvl)
}
@inhere inhere added the question Further information is requested label Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants