From 8c2eebc43800a2ae8407a2b0817ba6da1f7e391c Mon Sep 17 00:00:00 2001 From: Darius klein Date: Sat, 19 Jul 2025 22:05:15 +0200 Subject: [PATCH] cleanup logging + added log levels --- commands/config/commands.go | 6 +--- commands/config/subcommands/getConfig.go | 2 +- commands/templateCommand/commands.go | 8 ++---- .../templateCommand/subcommands/template.go | 16 ++++++++--- common/config.go | 7 ++++- common/default.config.toml | 4 ++- common/readConfig.go | 3 +- config.toml | 5 ---- main.go | 28 ++++++++++++++++--- 9 files changed, 50 insertions(+), 29 deletions(-) delete mode 100644 config.toml diff --git a/commands/config/commands.go b/commands/config/commands.go index 38c7a90..5b00921 100644 --- a/commands/config/commands.go +++ b/commands/config/commands.go @@ -27,9 +27,5 @@ func commands() []*cli.Command { // Action show help command if no sub commands are given for Config func Action(context context.Context, c *cli.Command) error { - err := cli.ShowSubcommandHelp(c) - if err != nil { - return err - } - return nil + return cli.ShowSubcommandHelp(c) } diff --git a/commands/config/subcommands/getConfig.go b/commands/config/subcommands/getConfig.go index 26307d9..a4d1833 100644 --- a/commands/config/subcommands/getConfig.go +++ b/commands/config/subcommands/getConfig.go @@ -34,7 +34,7 @@ func getConfigFlags() []cli.Flag { func getConfigAction(context context.Context, c *cli.Command) error { _, configPath, err := common.GetConfigPath() if err != nil { - fmt.Println(err) + return err } file, err := os.ReadFile(configPath) diff --git a/commands/templateCommand/commands.go b/commands/templateCommand/commands.go index d26409b..3e1095c 100644 --- a/commands/templateCommand/commands.go +++ b/commands/templateCommand/commands.go @@ -26,13 +26,9 @@ func commands() []*cli.Command { } } -// action show help command if no sub commands are given for Category +// Action show help command if no sub commands are given for Category func Action(context context.Context, c *cli.Command) error { - err := cli.ShowSubcommandHelp(c) - if err != nil { - return err - } - return nil + return cli.ShowSubcommandHelp(c) } // PLACEHOLDER sub-category of CATEGORY NAME Category diff --git a/commands/templateCommand/subcommands/template.go b/commands/templateCommand/subcommands/template.go index 607d34a..57afa0c 100644 --- a/commands/templateCommand/subcommands/template.go +++ b/commands/templateCommand/subcommands/template.go @@ -2,10 +2,12 @@ package subcommands import ( "context" + "errors" "github.com/urfave/cli/v3" + "log/slog" ) -var templateVar int +var templateVar bool // Template Command func Template() *cli.Command { @@ -21,12 +23,12 @@ func Template() *cli.Command { // templateFlags Register cli flags func templateFlags() []cli.Flag { return []cli.Flag{ - &cli.IntFlag{ + &cli.BoolFlag{ Name: "template", Aliases: []string{"t"}, Usage: "usage", - Value: 1000, - DefaultText: "default text", + Value: false, + DefaultText: "errors template", Required: false, Destination: &templateVar, }, @@ -35,5 +37,11 @@ func templateFlags() []cli.Flag { // templateAction logic for Template func templateAction(context context.Context, c *cli.Command) error { + slog.InfoContext(context, "template called", "logLevel", "INFO") + slog.DebugContext(context, "template called", "logLevel", "DEBUG") + if templateVar { + slog.ErrorContext(context, "template called", "logLevel", "ERROR") + return errors.New("template called with error") + } return nil } diff --git a/common/config.go b/common/config.go index 0c985eb..0974870 100644 --- a/common/config.go +++ b/common/config.go @@ -1,5 +1,10 @@ package common type Config struct { - environment string `toml:"environment"` + Settings Settings `toml:"settings"` +} + +type Settings struct { + Environment string `toml:"environment"` + LogLevel string `toml:"log_level"` } diff --git a/common/default.config.toml b/common/default.config.toml index a028bd6..1257f20 100644 --- a/common/default.config.toml +++ b/common/default.config.toml @@ -2,4 +2,6 @@ # Settings for kleinCommand [settings] -environment="" \ No newline at end of file +environment="" +#INFO,WARN,DEBUG,ERROR +log_level="DEBUG" \ No newline at end of file diff --git a/common/readConfig.go b/common/readConfig.go index 3fa4775..895c65d 100644 --- a/common/readConfig.go +++ b/common/readConfig.go @@ -1,7 +1,6 @@ package common import ( - "fmt" "github.com/BurntSushi/toml" "os" ) @@ -11,7 +10,7 @@ var config Config func ReadConfig() (Config, error) { _, configPath, err := GetConfigPath() if err != nil { - fmt.Println(err) + return config, err } file, err := os.ReadFile(configPath) diff --git a/config.toml b/config.toml deleted file mode 100644 index a028bd6..0000000 --- a/config.toml +++ /dev/null @@ -1,5 +0,0 @@ -# Example config for kleinCommand - -# Settings for kleinCommand -[settings] -environment="" \ No newline at end of file diff --git a/main.go b/main.go index aecf44c..4c38f50 100644 --- a/main.go +++ b/main.go @@ -2,19 +2,24 @@ package main import ( "context" - config "github.com/DariusKlein/kleinCommand/commands/config" + "github.com/DariusKlein/kleinCommand/commands/config" "github.com/DariusKlein/kleinCommand/commands/templateCommand" "github.com/DariusKlein/kleinCommand/common" "github.com/urfave/cli/v3" "log" + "log/slog" "net/mail" "os" + "strings" ) -var Config common.Config - func main() { - Config, _ = common.ReadConfig() + conf, err := common.ReadConfig() + if err != nil { + log.Fatal(err) + } + + setLogLevel(conf) app := &cli.Command{ Name: "KleinCommand", @@ -39,3 +44,18 @@ func main() { log.Fatal(err) } } + +func setLogLevel(conf common.Config) { + opts := &slog.HandlerOptions{} + switch strings.ToUpper(conf.Settings.LogLevel) { + case "INFO": + opts.Level = slog.LevelInfo + case "WARN": + opts.Level = slog.LevelWarn + case "DEBUG": + opts.Level = slog.LevelDebug + case "ERROR": + opts.Level = slog.LevelError + } + slog.SetLogLoggerLevel(opts.Level.Level()) +}