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
|
// Action show help command if no sub commands are given for Config
|
||||||
func Action(context context.Context, c *cli.Command) error {
|
func Action(context context.Context, c *cli.Command) error {
|
||||||
err := cli.ShowSubcommandHelp(c)
|
return cli.ShowSubcommandHelp(c)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -34,7 +34,7 @@ func getConfigFlags() []cli.Flag {
|
|||||||
func getConfigAction(context context.Context, c *cli.Command) error {
|
func getConfigAction(context context.Context, c *cli.Command) error {
|
||||||
_, configPath, err := common.GetConfigPath()
|
_, configPath, err := common.GetConfigPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.ReadFile(configPath)
|
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 {
|
func Action(context context.Context, c *cli.Command) error {
|
||||||
err := cli.ShowSubcommandHelp(c)
|
return cli.ShowSubcommandHelp(c)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PLACEHOLDER sub-category of CATEGORY NAME Category
|
// PLACEHOLDER sub-category of CATEGORY NAME Category
|
||||||
|
|||||||
@ -2,10 +2,12 @@ package subcommands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
|
"log/slog"
|
||||||
)
|
)
|
||||||
|
|
||||||
var templateVar int
|
var templateVar bool
|
||||||
|
|
||||||
// Template Command
|
// Template Command
|
||||||
func Template() *cli.Command {
|
func Template() *cli.Command {
|
||||||
@ -21,12 +23,12 @@ func Template() *cli.Command {
|
|||||||
// templateFlags Register cli flags
|
// templateFlags Register cli flags
|
||||||
func templateFlags() []cli.Flag {
|
func templateFlags() []cli.Flag {
|
||||||
return []cli.Flag{
|
return []cli.Flag{
|
||||||
&cli.IntFlag{
|
&cli.BoolFlag{
|
||||||
Name: "template",
|
Name: "template",
|
||||||
Aliases: []string{"t"},
|
Aliases: []string{"t"},
|
||||||
Usage: "usage",
|
Usage: "usage",
|
||||||
Value: 1000,
|
Value: false,
|
||||||
DefaultText: "default text",
|
DefaultText: "errors template",
|
||||||
Required: false,
|
Required: false,
|
||||||
Destination: &templateVar,
|
Destination: &templateVar,
|
||||||
},
|
},
|
||||||
@ -35,5 +37,11 @@ func templateFlags() []cli.Flag {
|
|||||||
|
|
||||||
// templateAction logic for Template
|
// templateAction logic for Template
|
||||||
func templateAction(context context.Context, c *cli.Command) error {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,10 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
type Config struct {
|
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 for kleinCommand
|
||||||
[settings]
|
[settings]
|
||||||
environment=""
|
environment=""
|
||||||
|
#INFO,WARN,DEBUG,ERROR
|
||||||
|
log_level="DEBUG"
|
||||||
@ -1,7 +1,6 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
@ -11,7 +10,7 @@ var config Config
|
|||||||
func ReadConfig() (Config, error) {
|
func ReadConfig() (Config, error) {
|
||||||
_, configPath, err := GetConfigPath()
|
_, configPath, err := GetConfigPath()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return config, err
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.ReadFile(configPath)
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
config "github.com/DariusKlein/kleinCommand/commands/config"
|
"github.com/DariusKlein/kleinCommand/commands/config"
|
||||||
"github.com/DariusKlein/kleinCommand/commands/templateCommand"
|
"github.com/DariusKlein/kleinCommand/commands/templateCommand"
|
||||||
"github.com/DariusKlein/kleinCommand/common"
|
"github.com/DariusKlein/kleinCommand/common"
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
"log"
|
"log"
|
||||||
|
"log/slog"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Config common.Config
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
Config, _ = common.ReadConfig()
|
conf, err := common.ReadConfig()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
setLogLevel(conf)
|
||||||
|
|
||||||
app := &cli.Command{
|
app := &cli.Command{
|
||||||
Name: "KleinCommand",
|
Name: "KleinCommand",
|
||||||
@ -39,3 +44,18 @@ func main() {
|
|||||||
log.Fatal(err)
|
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