From 468e3661684dfe11dbc4dcc5856a166b87e21009 Mon Sep 17 00:00:00 2001 From: Anatolii Kurotych Date: Sat, 30 Nov 2019 20:46:29 +0200 Subject: [PATCH] Use loglevel instead comma-separated list of logging --- cmd/yggdrasil/main.go | 45 ++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/cmd/yggdrasil/main.go b/cmd/yggdrasil/main.go index 981c6ef..813e950 100644 --- a/cmd/yggdrasil/main.go +++ b/cmd/yggdrasil/main.go @@ -132,6 +132,32 @@ func doGenconf(isjson bool) string { return string(bs) } +func setLogLevel(loglevel string, logger *log.Logger) { + levels := [...]string{"error", "warn", "info", "debug", "trace"} + loglevel = strings.ToLower(loglevel) + + contains := func() bool { + for _, l := range levels { + if l == loglevel { + return true + } + } + return false + } + + if !contains() { // set default log level + logger.Infoln("Loglevel parse failed. Set default level(info)") + loglevel = "info" + } + + for _, l := range levels { + logger.EnableLevel(l) + if l == loglevel { + break + } + } +} + // The main function is responsible for configuring and starting Yggdrasil. func main() { // Configure the command line parameters. @@ -142,10 +168,10 @@ func main() { confjson := flag.Bool("json", false, "print configuration from -genconf or -normaliseconf as JSON instead of HJSON") autoconf := flag.Bool("autoconf", false, "automatic mode (dynamic IP, peer with IPv6 neighbors)") ver := flag.Bool("version", false, "prints the version of this build") - logging := flag.String("logging", "info,warn,error", "comma-separated list of logging levels to enable") logto := flag.String("logto", "stdout", "file path to log to, \"syslog\" or \"stdout\"") getaddr := flag.Bool("address", false, "returns the IPv6 address as derived from the supplied configuration") getsnet := flag.Bool("subnet", false, "returns the IPv6 subnet as derived from the supplied configuration") + loglevel := flag.String("loglevel", "info", "loglevel to enable") flag.Parse() var cfg *config.NodeConfig @@ -239,20 +265,9 @@ func main() { logger = log.New(os.Stdout, "", log.Flags()) logger.Warnln("Logging defaulting to stdout") } - //logger.EnableLevel("error") - //logger.EnableLevel("warn") - //logger.EnableLevel("info") - if levels := strings.Split(*logging, ","); len(levels) > 0 { - for _, level := range levels { - l := strings.TrimSpace(level) - switch l { - case "error", "warn", "info", "trace", "debug": - logger.EnableLevel(l) - default: - continue - } - } - } + + setLogLevel(*loglevel, logger) + // Setup the Yggdrasil node itself. The node{} type includes a Core, so we // don't need to create this manually. n := node{}