Skip to content

Commit

Permalink
Logging can be configured from the confd config file
Browse files Browse the repository at this point in the history
  • Loading branch information
kelseyhightower committed Nov 3, 2013
1 parent 0c86396 commit d688772
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 20 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:

Expand Down
18 changes: 6 additions & 12 deletions confd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,20 @@ 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() {
// Most flags are defined in the confd/config package which allows us to
// 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
Expand All @@ -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(), ", "))
Expand Down
46 changes: 38 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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":
Expand All @@ -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
}
}

0 comments on commit d688772

Please sign in to comment.