From cc4edc5cb6f90425df868dd3136d8a7ac63fb6d4 Mon Sep 17 00:00:00 2001 From: moonD4rk Date: Fri, 10 Mar 2023 15:58:10 +0800 Subject: [PATCH] refactor: initial default logger --- cmd/hack-browser-data/main.go | 5 +---- log/log.go | 17 +++++++++-------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/cmd/hack-browser-data/main.go b/cmd/hack-browser-data/main.go index ff26680..c2b28a0 100644 --- a/cmd/hack-browser-data/main.go +++ b/cmd/hack-browser-data/main.go @@ -40,11 +40,8 @@ func Execute() { HideHelpCommand: true, Action: func(c *cli.Context) error { if verbose { - log.Init("debug") - } else { - log.Init("notice") + log.SetVerbose() } - browsers, err := browser.PickBrowsers(browserName, profilePath) if err != nil { log.Error(err) diff --git a/log/log.go b/log/log.go index cbcf023..4bd48fe 100644 --- a/log/log.go +++ b/log/log.go @@ -8,19 +8,20 @@ import ( var std = &slog.SugaredLogger{} -func Init(l string) { - if l == "debug" { - std = newStdLogger(slog.DebugLevel) - } else { - std = newStdLogger(slog.NoticeLevel) - } +func init() { + std = newStdLogger(slog.NoticeLevel) +} + +// SetVerbose set log level to debug +func SetVerbose() { + std = newStdLogger(slog.DebugLevel) } const template = "[{{level}}] [{{caller}}] {{message}} {{data}} {{extra}}\n" -// NewStdLogger instance +// newStdLogger is a new std logger func newStdLogger(level slog.Level) *slog.SugaredLogger { - return slog.NewSugaredLogger(os.Stdout, level).Configure(func(sl *slog.SugaredLogger) { + return slog.NewSugaredLogger(os.Stdout, level).Config(func(sl *slog.SugaredLogger) { sl.SetName("stdLogger") sl.ReportCaller = true sl.CallerSkip = 7