diff --git a/pkg/util/log/flags.go b/pkg/util/log/flags.go index 1b0d217776ac..d8cd094bcba0 100644 --- a/pkg/util/log/flags.go +++ b/pkg/util/log/flags.go @@ -15,6 +15,7 @@ import ( "fmt" "io/fs" "math" + "strings" "github.com/cockroachdb/cockroach/pkg/util/envutil" "github.com/cockroachdb/cockroach/pkg/util/log/channel" @@ -24,6 +25,7 @@ import ( "github.com/cockroachdb/cockroach/pkg/util/log/severity" "github.com/cockroachdb/cockroach/pkg/util/syncutil" "github.com/cockroachdb/errors" + "github.com/cockroachdb/redact" ) type config struct { @@ -445,7 +447,8 @@ func (l *sinkInfo) applyConfig(c logconfig.CommonSinkConfig) error { l.criticality = *c.Criticality f, ok := formatters[*c.Format] if !ok { - return errors.Newf("unknown format: %q", *c.Format) + return errors.WithHintf(errors.Newf("unknown format: %q", *c.Format), + "Supported formats: %s.", redact.Safe(strings.Join(formatNames, ", "))) } l.formatter = f() for k, v := range c.FormatOptions { diff --git a/pkg/util/log/formats.go b/pkg/util/log/formats.go index 350985762b12..6d05e892fad9 100644 --- a/pkg/util/log/formats.go +++ b/pkg/util/log/formats.go @@ -48,7 +48,11 @@ var formatParsers = map[string]string{ var formatters = func() map[string]func() logFormatter { m := make(map[string]func() logFormatter) r := func(f func() logFormatter) { - m[f().formatterName()] = f + name := f().formatterName() + if _, ok := m[name]; ok { + panic("duplicate formatter name: " + name) + } + m[name] = f } r(func() logFormatter { return &formatCrdbV1{showCounter: false, colorProfile: ttycolor.StderrProfile, colorProfileName: "auto"} @@ -75,6 +79,14 @@ var formatters = func() map[string]func() logFormatter { return m }() +var formatNames = func() (res []string) { + for name := range formatters { + res = append(res, name) + } + sort.Strings(res) + return res +}() + // GetFormatterDocs returns the embedded documentation for all the // supported formats. func GetFormatterDocs() map[string]string { diff --git a/pkg/util/log/formats_test.go b/pkg/util/log/formats_test.go index dc4895602049..9a19413bed17 100644 --- a/pkg/util/log/formats_test.go +++ b/pkg/util/log/formats_test.go @@ -16,7 +16,6 @@ import ( "fmt" "os" "path/filepath" - "sort" "strconv" "testing" @@ -34,13 +33,6 @@ func TestFormatRedaction(t *testing.T) { sc := ScopeWithoutShowLogs(t) defer sc.Close(t) - // Make the test below deterministic. - formatNames := make([]string, 0, len(formatters)) - for n := range formatters { - formatNames = append(formatNames, n) - } - sort.Strings(formatNames) - ctx := context.Background() ctx = logtags.AddTag(ctx, "a", "secret1") ctx = logtags.AddTag(ctx, "b", redact.Sprintf("safe1 %s", "secret2"))