Skip to content

Commit

Permalink
util/log: minor config ergonomic improvements
Browse files Browse the repository at this point in the history
Release note: None
  • Loading branch information
knz committed Jun 13, 2023
1 parent fae003c commit 6d3d2d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
5 changes: 4 additions & 1 deletion pkg/util/log/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 13 additions & 1 deletion pkg/util/log/formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand All @@ -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 {
Expand Down
8 changes: 0 additions & 8 deletions pkg/util/log/formats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"testing"

Expand All @@ -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"))
Expand Down

0 comments on commit 6d3d2d5

Please sign in to comment.