cmd: Reduce spammy logs from --watch

This commit is contained in:
Matthew Holt 2023-05-12 11:04:02 -06:00
parent cfc85ae8ca
commit 5ebb7d496d
No known key found for this signature in database
GPG Key ID: 2A349DD577D586A5

View File

@ -89,6 +89,10 @@ func handlePingbackConn(conn net.Conn, expect []byte) error {
// and returns the resulting JSON config bytes along with // and returns the resulting JSON config bytes along with
// the name of the loaded config file (if any). // the name of the loaded config file (if any).
func LoadConfig(configFile, adapterName string) ([]byte, string, error) { func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
return loadConfigWithLogger(caddy.Log(), configFile, adapterName)
}
func loadConfigWithLogger(logger *zap.Logger, configFile, adapterName string) ([]byte, string, error) {
// specifying an adapter without a config file is ambiguous // specifying an adapter without a config file is ambiguous
if adapterName != "" && configFile == "" { if adapterName != "" && configFile == "" {
return nil, "", fmt.Errorf("cannot adapt config without config file (use --config)") return nil, "", fmt.Errorf("cannot adapt config without config file (use --config)")
@ -107,9 +111,11 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
if err != nil { if err != nil {
return nil, "", fmt.Errorf("reading config file: %v", err) return nil, "", fmt.Errorf("reading config file: %v", err)
} }
caddy.Log().Info("using provided configuration", if logger != nil {
zap.String("config_file", configFile), logger.Info("using provided configuration",
zap.String("config_adapter", adapterName)) zap.String("config_file", configFile),
zap.String("config_adapter", adapterName))
}
} else if adapterName == "" { } else if adapterName == "" {
// as a special case when no config file or adapter // as a special case when no config file or adapter
// is specified, see if the Caddyfile adapter is // is specified, see if the Caddyfile adapter is
@ -126,7 +132,9 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
} else { } else {
// success reading default Caddyfile // success reading default Caddyfile
configFile = "Caddyfile" configFile = "Caddyfile"
caddy.Log().Info("using adjacent Caddyfile") if logger != nil {
logger.Info("using adjacent Caddyfile")
}
} }
} }
} }
@ -161,7 +169,9 @@ func LoadConfig(configFile, adapterName string) ([]byte, string, error) {
if warn.Directive != "" { if warn.Directive != "" {
msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message) msg = fmt.Sprintf("%s: %s", warn.Directive, warn.Message)
} }
caddy.Log().Warn(msg, zap.String("adapter", adapterName), zap.String("file", warn.File), zap.Int("line", warn.Line)) if logger != nil {
logger.Warn(msg, zap.String("adapter", adapterName), zap.String("file", warn.File), zap.Int("line", warn.Line))
}
} }
config = adaptedConfig config = adaptedConfig
} }
@ -192,7 +202,7 @@ func watchConfigFile(filename, adapterName string) {
} }
// get current config // get current config
lastCfg, _, err := LoadConfig(filename, adapterName) lastCfg, _, err := loadConfigWithLogger(nil, filename, adapterName)
if err != nil { if err != nil {
logger().Error("unable to load latest config", zap.Error(err)) logger().Error("unable to load latest config", zap.Error(err))
return return
@ -203,7 +213,6 @@ func watchConfigFile(filename, adapterName string) {
// begin poller // begin poller
//nolint:staticcheck //nolint:staticcheck
for range time.Tick(1 * time.Second) { for range time.Tick(1 * time.Second) {
// get current config // get current config
newCfg, _, err := LoadConfig(filename, adapterName) newCfg, _, err := LoadConfig(filename, adapterName)
if err != nil { if err != nil {