From d6887727112eeaf6e5a8c4e1a07c973eae798198 Mon Sep 17 00:00:00 2001 From: Kelsey Hightower Date: Sun, 3 Nov 2013 08:31:08 -0800 Subject: [PATCH] Logging can be configured from the confd config file --- README.md | 3 +++ confd.go | 18 ++++++------------ config/config.go | 46 ++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 47 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 72c54b6e9..8e2fe15bb 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ and loaded from `/etc/confd/confd.toml` by default. Optional: +* `debug` (bool) - Enable debug logging. * `client_cert` (string) The cert file of the client. * `client_key` (string) The key file of the client. * `confdir` (string) - The path to confd configs. The default is /etc/confd. @@ -101,7 +102,9 @@ Optional: * `noop` (bool) - Enable noop mode. Process all template resource, but don't update target config. * `prefix` (string) - The prefix string to prefix to keys when making calls to etcd. The default is "/". +* `quiet` (bool) - Enable quiet logging. Only error messages are printed. * `srv_domain` (string) - The domain to query for etcd SRV records. +* `verbose` (bool) - Enable verbose logging. Example: diff --git a/confd.go b/confd.go index 5c9dac736..4821be7b7 100644 --- a/confd.go +++ b/confd.go @@ -17,19 +17,13 @@ import ( var ( configFile = "" - debug bool defaultConfigFile = "/etc/confd/confd.toml" onetime bool - quiet bool - verbose bool ) func init() { flag.StringVar(&configFile, "config-file", "", "the confd config file") - flag.BoolVar(&debug, "debug", false, "enable debug logging") flag.BoolVar(&onetime, "onetime", false, "run once and exit") - flag.BoolVar(&quiet, "quiet", false, "silence non-error messages") - flag.BoolVar(&verbose, "verbose", false, "enable verbose logging") } func main() { @@ -37,12 +31,6 @@ func main() { // override configuration settings from the command line. Parse the flags now // to make them active. flag.Parse() - // Configure logging. While you can enable debug and verbose logging, however - // if quiet is set to true then debug and verbose messages will not be printed. - log.SetQuiet(quiet) - log.SetVerbose(verbose) - log.SetDebug(debug) - log.Info("Starting confd") if configFile == "" { if IsFileExist(defaultConfigFile) { configFile = defaultConfigFile @@ -52,6 +40,12 @@ func main() { if err := config.LoadConfig(configFile); err != nil { log.Fatal(err.Error()) } + // Configure logging. While you can enable debug and verbose logging, however + // if quiet is set to true then debug and verbose messages will not be printed. + log.SetQuiet(config.Quiet()) + log.SetVerbose(config.Verbose()) + log.SetDebug(config.Debug()) + log.Info("Starting confd") // Create the etcd client upfront and use it for the life of the process. // The etcdClient is an http.Client and designed to be reused. log.Debug("Connecting to " + strings.Join(config.EtcdNodes(), ", ")) diff --git a/config/config.go b/config/config.go index 08a1dbc89..467c89eed 100644 --- a/config/config.go +++ b/config/config.go @@ -20,12 +20,15 @@ var ( clientKey string config Config // holds the global confd config. confdir string + debug bool etcdNodes Nodes etcdScheme string interval int noop bool prefix string + quiet bool srvDomain string + verbose bool ) // Config represents the confd configuration settings. @@ -35,18 +38,22 @@ type Config struct { // confd represents the parsed configuration settings. type confd struct { - ClientCert string `toml:"client_cert"` - ClientKey string `toml:"client_key"` - ConfDir string + Debug bool `toml:"debug"` + ClientCert string `toml:"client_cert"` + ClientKey string `toml:"client_key"` + ConfDir string `toml:"confdir"` EtcdNodes []string `toml:"etcd_nodes"` EtcdScheme string `toml:"etcd_scheme"` - Interval int - Noop bool `toml:"noop"` - Prefix string - SRVDomain string `toml:"srv_domain"` + Interval int `toml:"interval"` + Noop bool `toml:"noop"` + Prefix string `toml:"prefix"` + Quiet bool `toml:"quiet"` + SRVDomain string `toml:"srv_domain"` + Verbose bool `toml:"verbose"` } func init() { + flag.BoolVar(&debug, "debug", false, "enable debug logging") flag.StringVar(&clientCert, "client-cert", "", "the client cert") flag.StringVar(&clientKey, "client-key", "", "the client key") flag.StringVar(&confdir, "confdir", "/etc/confd", "confd conf directory") @@ -55,7 +62,9 @@ func init() { flag.IntVar(&interval, "interval", 600, "etcd polling interval") flag.BoolVar(&noop, "noop", false, "only show pending changes, don't sync configs.") flag.StringVar(&prefix, "prefix", "/", "etcd key path prefix") + flag.BoolVar(&quiet, "quiet", false, "enable quiet logging. Only error messages are printed.") flag.StringVar(&srvDomain, "srv-domain", "", "the domain to query for the etcd SRV record, i.e. example.com") + flag.BoolVar(&verbose, "verbose", false, "enable verbose logging") } // LoadConfig initializes the confd configuration by first setting defaults, @@ -84,6 +93,11 @@ func LoadConfig(path string) error { return nil } +// Debug reports whether debug mode is enabled. +func Debug() bool { + return config.Confd.Debug +} + // ClientCert returns the client cert path. func ClientCert() string { return config.Confd.ClientCert @@ -110,7 +124,7 @@ func Interval() int { return config.Confd.Interval } -// Noop returns the state of noop mode. +// Noop reports whether noop mode is enabled. func Noop() bool { return config.Confd.Noop } @@ -120,6 +134,16 @@ func Prefix() string { return config.Confd.Prefix } +// Quiet reports whether quiet mode is enabled. +func Quiet() bool { + return config.Confd.Quiet +} + +// Verbose reports whether verbose mode is enabled. +func Verbose() bool { + return config.Confd.Verbose +} + // SetConfDir sets the confd conf dir. func SetConfDir(path string) { config.Confd.ConfDir = path @@ -214,6 +238,8 @@ func processFlags() { func setConfigFromFlag(f *flag.Flag) { switch f.Name { + case "debug": + config.Confd.Debug = debug case "client-cert": config.Confd.ClientCert = clientCert case "client-key": @@ -230,7 +256,11 @@ func setConfigFromFlag(f *flag.Flag) { config.Confd.Noop = noop case "prefix": config.Confd.Prefix = prefix + case "quiet": + config.Confd.Quiet = quiet case "srv-domain": config.Confd.SRVDomain = srvDomain + case "verbose": + config.Confd.Verbose = verbose } }