Skip to content

Commit a26557e

Browse files
committed
ref(promslog): make log style a type, not settable
We only ever really intend to support these 2 formats, and after discussion it's been decided that this config does not need to be settable. Adopting a custom type for the logging style simplifies a fair bit. Signed-off-by: TJ Hoplock <[email protected]>
1 parent a7e4e32 commit a26557e

File tree

2 files changed

+13
-45
lines changed

2 files changed

+13
-45
lines changed

promslog/slog.go

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ import (
2525
"log/slog"
2626
"os"
2727
"path/filepath"
28-
"slices"
2928
"strconv"
3029
"strings"
3130
)
3231

32+
type LogStyle string
33+
34+
const (
35+
SlogStyle LogStyle = "slog"
36+
GoKitStyle LogStyle = "go-kit"
37+
)
38+
3339
var (
3440
LevelFlagOptions = []string{"debug", "info", "warn", "error"}
3541
FormatFlagOptions = []string{"logfmt", "json"}
36-
StyleOptions = []string{"slog", "go-kit"}
3742

3843
callerAddFunc = false
3944
defaultWriter = os.Stderr
@@ -141,35 +146,11 @@ func (f *AllowedFormat) Set(s string) error {
141146
return nil
142147
}
143148

144-
// AllowedStyle is a settable identifier for the log formatting style that a
145-
// logger can have. Valid options are `slog` (default) or `go-kit`.
146-
type AllowedStyle struct {
147-
s string
148-
}
149-
150-
func (a *AllowedStyle) String() string {
151-
return a.s
152-
}
153-
154-
// Set updates the value of the allowed logging style. Valid options are `slog`
155-
// (default), and `go-kit`. Note that while it is possible to call this method
156-
// and update the allowed logging style after creating a logger, it has no
157-
// effect; the last call to Set() prior to initializing a new logger is the
158-
// value that will be used for the lifetime of the logger.
159-
func (a *AllowedStyle) Set(style string) error {
160-
if !slices.Contains(StyleOptions, style) {
161-
return fmt.Errorf("unrecognized log style %s", style)
162-
}
163-
164-
a.s = style
165-
return nil
166-
}
167-
168149
// Config is a struct containing configurable settings for the logger
169150
type Config struct {
170151
Level *AllowedLevel
171152
Format *AllowedFormat
172-
Style *AllowedStyle
153+
Style LogStyle
173154
ioWriter io.Writer
174155
}
175156

@@ -190,7 +171,7 @@ func New(config *Config) *slog.Logger {
190171
AddSource: true,
191172
}
192173

193-
if config.Style != nil && config.Style.s == "go-kit" {
174+
if config.Style == GoKitStyle {
194175
logHandlerOpts.ReplaceAttr = goKitStyleReplaceAttrFunc
195176
}
196177

promslog/slog_test.go

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"log/slog"
2323
"regexp"
24-
"slices"
2524
"strings"
2625
"testing"
2726

@@ -102,30 +101,18 @@ func TestDynamicLevels(t *testing.T) {
102101
wantedLevelCounts := map[string]int{"info": 1, "debug": 1}
103102

104103
tests := map[string]struct {
105-
logStyle string
104+
logStyle LogStyle
106105
logStyleRegexp *regexp.Regexp
107106
wantedLevelCount map[string]int
108107
}{
109-
"empty_log_style": {logStyle: "", logStyleRegexp: slogStyleLogRegexp, wantedLevelCount: wantedLevelCounts},
110-
"bad_log_style": {logStyle: "foo", logStyleRegexp: slogStyleLogRegexp, wantedLevelCount: wantedLevelCounts},
111-
"slog_log_style": {logStyle: "slog", logStyleRegexp: slogStyleLogRegexp, wantedLevelCount: wantedLevelCounts},
112-
"go-kit_log_style": {logStyle: "go-kit", logStyleRegexp: goKitStyleLogRegexp, wantedLevelCount: wantedLevelCounts},
108+
"slog_log_style": {logStyle: SlogStyle, logStyleRegexp: slogStyleLogRegexp, wantedLevelCount: wantedLevelCounts},
109+
"go-kit_log_style": {logStyle: GoKitStyle, logStyleRegexp: goKitStyleLogRegexp, wantedLevelCount: wantedLevelCounts},
113110
}
114111

115112
for name, tc := range tests {
116113
t.Run(name, func(t *testing.T) {
117114
buf.Reset() // Ensure buf is reset prior to tests
118-
config := &Config{ioWriter: &buf, Style: &AllowedStyle{}}
119-
err := config.Style.Set(tc.logStyle)
120-
// AllowedStyle.Set() returns an error on invalid style
121-
// values and the tests use a mixture of valid and
122-
// invalid values. We must expect no error for valid
123-
// values, and expect an error for the invalid values.
124-
if slices.Contains(StyleOptions, tc.logStyle) {
125-
require.NoError(t, err, "log style returned an error setting a valid value", "value", tc.logStyle)
126-
} else {
127-
require.ErrorContains(t, err, fmt.Sprintf("unrecognized log style %s", tc.logStyle), "expected error setting log style to invalid value", "value", tc.logStyle)
128-
}
115+
config := &Config{ioWriter: &buf, Style: tc.logStyle}
129116
logger := New(config)
130117

131118
// Test that log level can be adjusted on-the-fly to debug and that a

0 commit comments

Comments
 (0)