Skip to content

Commit fec1359

Browse files
committed
ref(promslog): dry out AllowedStyle Set(), rename style options var
- Renames the style options var to drop the `Flag` portion of it's name, since it's no longer used for a flag, but merely to house valid options. - drys out Set method for AllowedStyle by comparing provided flag against our valid options list using `slices.Contains()`, updates method var name for the funcs to not double up on `s` usage for potential confusion Signed-off-by: TJ Hoplock <[email protected]>
1 parent dbf5621 commit fec1359

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

promslog/slog.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ import (
2525
"log/slog"
2626
"os"
2727
"path/filepath"
28+
"slices"
2829
"strconv"
2930
"strings"
3031
)
3132

3233
var (
3334
LevelFlagOptions = []string{"debug", "info", "warn", "error"}
3435
FormatFlagOptions = []string{"logfmt", "json"}
35-
StyleFlagOptions = []string{"slog", "go-kit"}
36+
StyleOptions = []string{"slog", "go-kit"}
3637

3738
callerAddFunc = false
3839
defaultWriter = os.Stderr
@@ -146,18 +147,21 @@ type AllowedStyle struct {
146147
s string
147148
}
148149

149-
func (s *AllowedStyle) String() string {
150-
return s.s
150+
func (a *AllowedStyle) String() string {
151+
return a.s
151152
}
152153

153-
func (s *AllowedStyle) Set(style string) error {
154-
switch style {
155-
case "slog", "go-kit":
156-
s.s = style
157-
default:
158-
return fmt.Errorf("unrecognized log style %q", s)
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)
159162
}
160163

164+
a.s = style
161165
return nil
162166
}
163167

0 commit comments

Comments
 (0)