cleanup logging + added log levels
This commit is contained in:
parent
4a458f66b0
commit
8c2eebc438
@ -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)
|
||||
}
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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"`
|
||||
}
|
||||
|
||||
@ -3,3 +3,5 @@
|
||||
# Settings for kleinCommand
|
||||
[settings]
|
||||
environment=""
|
||||
#INFO,WARN,DEBUG,ERROR
|
||||
log_level="DEBUG"
|
||||
@ -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)
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
# Example config for kleinCommand
|
||||
|
||||
# Settings for kleinCommand
|
||||
[settings]
|
||||
environment=""
|
||||
28
main.go
28
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())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user