From cd2ca137c0a58d3ee911e8ff3a67abe5d56214f6 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Thu, 5 Aug 2021 19:19:25 +0100 Subject: [PATCH] Make log_level user configurable --- README.md | 5 +++++ cmd/headscale/cli/utils.go | 10 ++++++---- cmd/headscale/headscale.go | 30 ++++++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index aea8fd6..cb42b66 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,11 @@ Headscale's configuration file is named `config.json` or `config.yaml`. Headscal `server_url` is the external URL via which Headscale is reachable. `listen_addr` is the IP address and port the Headscale program should listen on. `ip_prefix` is the IP prefix (range) in which IP addresses for nodes will be allocated (default 100.64.0.0/10, e.g., 192.168.4.0/24, 10.0.0.0/8) +``` + "log_level": "debug" +``` +`log_level` can be used to set the Log level for Headscale, it defaults to `debug`, and the available levels are: `trace`, `debug`, `info`, `warn` and `error`. + ``` "private_key_path": "private.key", ``` diff --git a/cmd/headscale/cli/utils.go b/cmd/headscale/cli/utils.go index ddaeca7..d104f04 100644 --- a/cmd/headscale/cli/utils.go +++ b/cmd/headscale/cli/utils.go @@ -39,6 +39,8 @@ func LoadConfig(path string) error { viper.SetDefault("ip_prefix", "100.64.0.0/10") + viper.SetDefault("log_level", "debug") + err := viper.ReadInConfig() if err != nil { return fmt.Errorf("Fatal error reading config file: %s \n", err) @@ -170,24 +172,24 @@ func JsonOutput(result interface{}, errResult error, outputFormat string) { if errResult != nil { j, err = json.MarshalIndent(ErrorOutput{errResult.Error()}, "", "\t") if err != nil { - log.Fatalln(err) + log.Fatal().Err(err) } } else { j, err = json.MarshalIndent(result, "", "\t") if err != nil { - log.Fatalln(err) + log.Fatal().Err(err) } } case "json-line": if errResult != nil { j, err = json.Marshal(ErrorOutput{errResult.Error()}) if err != nil { - log.Fatalln(err) + log.Fatal().Err(err) } } else { j, err = json.Marshal(result) if err != nil { - log.Fatalln(err) + log.Fatal().Err(err) } } } diff --git a/cmd/headscale/headscale.go b/cmd/headscale/headscale.go index c7b834c..3769f53 100644 --- a/cmd/headscale/headscale.go +++ b/cmd/headscale/headscale.go @@ -1,15 +1,41 @@ package main import ( - "log" + "os" + "time" "github.com/juanfont/headscale/cmd/headscale/cli" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "github.com/spf13/viper" ) func main() { + zerolog.TimeFieldFormat = zerolog.TimeFormatUnix + log.Logger = log.Output(zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: time.RFC3339, + }) + err := cli.LoadConfig("") if err != nil { - log.Fatalf(err.Error()) + log.Fatal().Err(err) + } + + logLevel := viper.GetString("log_level") + switch logLevel { + case "trace": + zerolog.SetGlobalLevel(zerolog.TraceLevel) + case "debug": + zerolog.SetGlobalLevel(zerolog.DebugLevel) + case "info": + zerolog.SetGlobalLevel(zerolog.InfoLevel) + case "warn": + zerolog.SetGlobalLevel(zerolog.WarnLevel) + case "error": + zerolog.SetGlobalLevel(zerolog.ErrorLevel) + default: + zerolog.SetGlobalLevel(zerolog.DebugLevel) } cli.Execute()