From e6f2d6de3ebbdd1f878b4ae8970e65386b462fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=A0=CF=85=CE=B1=CE=B7=20=D7=A0=CF=85=CE=B1=CE=B7=D1=95?= =?UTF-8?q?=CF=83=CE=B7?= Date: Sat, 20 Jan 2024 14:34:38 -0800 Subject: [PATCH] chore: additional initLogger cleanup No need to create another log instance, simply change the log level once config is loaded. --- cmd/root.go | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index f5495a4..a7f9c3f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -78,21 +78,12 @@ func init() { _ = viper.BindPFlag("giltFile", rootCmd.PersistentFlags().Lookup("gilt-file")) _ = viper.BindPFlag("giltDir", rootCmd.PersistentFlags().Lookup("gilt-dir")) _ = viper.BindPFlag("repositories", rootCmd.PersistentFlags().Lookup("repositories")) -} -func getLogger(logLevel slog.Leveler) *slog.Logger { - return slog.New( - tint.NewHandler(os.Stderr, &tint.Options{ - Level: logLevel, - TimeFormat: time.Kitchen, - NoColor: !term.IsTerminal(int(os.Stdout.Fd())), - }), - ) + cobra.OnInitialize(initLogger) } func logFatal(message string, logGroup any) { - log := getLogger(slog.LevelError) - log.Error( + logger.Error( message, logGroup, ) @@ -106,7 +97,13 @@ func initLogger() { logLevel = slog.LevelDebug } - logger = getLogger(logLevel) + logger = slog.New( + tint.NewHandler(os.Stderr, &tint.Options{ + Level: logLevel, + TimeFormat: time.Kitchen, + NoColor: !term.IsTerminal(int(os.Stdout.Fd())), + }), + ) } func initConfig() {