Skip to content

Commit b9745cf

Browse files
author
Russ Egan
committed
Removed the use of function types.
Function types are a little weird in that you need to cast functions to them *sometimes*. Simpler to just leave them as raw function signatures.
1 parent 4319d17 commit b9745cf

File tree

4 files changed

+55
-26
lines changed

4 files changed

+55
-26
lines changed

v2/config_from_env.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,39 +78,43 @@ func UnmarshalEnv(o *HandlerOptions, envvars ...string) error {
7878

7979
var handlerFns sync.Map
8080

81-
func init() { //nolint:gochecknoinits
82-
resetBuiltInHandlerFns()
83-
}
81+
var initHandlerFnsOnce sync.Once
8482

8583
func resetBuiltInHandlerFns() {
8684
handlerFns = sync.Map{}
8785
textHandlerFn := func(_ string, w io.Writer, opts *slog.HandlerOptions) slog.Handler {
8886
return slog.NewTextHandler(w, opts)
8987
}
90-
RegisterHandlerFn(TextHandler, textHandlerFn)
88+
handlerFns.Store(TextHandler, textHandlerFn)
9189
// for v1 compatibility, "console" is an alias for "text"
92-
RegisterHandlerFn(ConsoleHandler, textHandlerFn)
93-
RegisterHandlerFn(JSONHandler, func(_ string, w io.Writer, opts *slog.HandlerOptions) slog.Handler {
90+
handlerFns.Store(ConsoleHandler, textHandlerFn)
91+
handlerFns.Store(JSONHandler, func(_ string, w io.Writer, opts *slog.HandlerOptions) slog.Handler {
9492
return slog.NewJSONHandler(w, opts)
9593
})
96-
RegisterHandlerFn(TermHandler, termHandlerFn(false))
97-
RegisterHandlerFn(TermColorHandler, termHandlerFn(true))
98-
RegisterHandlerFn(NoopHandler, func(_ string, _ io.Writer, _ *slog.HandlerOptions) slog.Handler {
94+
handlerFns.Store(TermHandler, termHandlerFn(false))
95+
handlerFns.Store(TermColorHandler, termHandlerFn(true))
96+
handlerFns.Store(NoopHandler, func(_ string, _ io.Writer, _ *slog.HandlerOptions) slog.Handler {
9997
return noop
10098
})
10199
}
102100

103-
func LookupHandlerFn(name string) HandlerFn {
101+
func initHandlerFns() {
102+
initHandlerFnsOnce.Do(func() {
103+
resetBuiltInHandlerFns()
104+
})
105+
}
106+
107+
func LookupHandlerFn(name string) func(string, io.Writer, *slog.HandlerOptions) slog.Handler {
108+
initHandlerFns()
104109
v, ok := handlerFns.Load(name)
105110
if !ok {
106111
return nil
107112
}
108-
return v.(HandlerFn) //nolint:forcetypeassert // if it's not a HandlerFn, we should panic
113+
return v.(func(string, io.Writer, *slog.HandlerOptions) slog.Handler) //nolint:forcetypeassert // if it's not a HandlerFn, we should panic
109114
}
110115

111-
type HandlerFn func(string, io.Writer, *slog.HandlerOptions) slog.Handler
112-
113-
func RegisterHandlerFn(name string, fn HandlerFn) {
116+
func RegisterHandlerFn(name string, fn func(string, io.Writer, *slog.HandlerOptions) slog.Handler) {
117+
initHandlerFns()
114118
if fn == nil {
115119
panic(fmt.Sprintf("constructor for sink %q is nil", name))
116120
}
@@ -120,7 +124,7 @@ func RegisterHandlerFn(name string, fn HandlerFn) {
120124
handlerFns.Store(name, fn)
121125
}
122126

123-
func termHandlerFn(color bool) HandlerFn {
127+
func termHandlerFn(color bool) func(string, io.Writer, *slog.HandlerOptions) slog.Handler {
124128
return func(_ string, w io.Writer, opts *slog.HandlerOptions) slog.Handler {
125129
if opts == nil {
126130
opts = &slog.HandlerOptions{}
@@ -131,7 +135,7 @@ func termHandlerFn(color bool) HandlerFn {
131135
Theme: console.NewDefaultTheme(),
132136
ReplaceAttr: opts.ReplaceAttr,
133137
TimeFormat: "15:04:05.000",
134-
HeaderFormat: "%t %[logger]12h %l | %m",
138+
HeaderFormat: "%t %[" + LoggerKey + "]8h %l | %m",
135139
TruncateSourcePath: 2,
136140
})
137141
}

v2/config_from_env_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func TestRegisterHandlerFn(t *testing.T) {
200200
tests := []struct {
201201
name string
202202
handlerName string
203-
handlerFn HandlerFn
203+
handlerFn func(string, io.Writer, *slog.HandlerOptions) slog.Handler
204204
want string
205205
wantPanic bool
206206
}{
@@ -259,8 +259,8 @@ func TestRegisterHandlerFn(t *testing.T) {
259259
}
260260

261261
builtIns := map[string]string{
262-
TermHandler: "blue INF | hi\n",
263-
TermColorHandler: "\x1b[1;90mblue \x1b[0m \x1b[32mINF\x1b[0m \x1b[1;90m|\x1b[0m \x1b[1mhi\x1b[0m\n",
262+
TermHandler: "blue INF | hi\n",
263+
TermColorHandler: "\x1b[1;90mblue \x1b[0m \x1b[32mINF\x1b[0m \x1b[1;90m|\x1b[0m \x1b[1mhi\x1b[0m\n",
264264
TextHandler: "level=INFO msg=hi logger=blue\n",
265265
JSONHandler: `{"level":"INFO","msg":"hi","logger":"blue"}` + "\n",
266266
ConsoleHandler: "level=INFO msg=hi logger=blue\n",

v2/default.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package flume
33
import (
44
"log/slog"
55
"math"
6+
"sync"
67
)
78

89
const (
@@ -29,14 +30,19 @@ func New(name string) *slog.Logger {
2930
return slog.New(Default().Named(name))
3031
}
3132

32-
var defaultHandler = NewHandler(nil, &HandlerOptions{
33-
HandlerFn: LookupHandlerFn(NoopHandler),
34-
})
33+
var defaultHandler *Handler
34+
35+
var initDefaultHandlerOnce sync.Once
3536

3637
// Default returns the default Handler. It will never be
3738
// nil. By default, it is a noop handler that discards all log messages.
3839
//
3940
// The simplest way to enable logging is to call Default().SetHandlerOptions(nil)
4041
func Default() *Handler {
42+
initDefaultHandlerOnce.Do(func() {
43+
defaultHandler = NewHandler(nil, &HandlerOptions{
44+
HandlerFn: LookupHandlerFn(NoopHandler),
45+
})
46+
})
4147
return defaultHandler
4248
}

v2/default_test.go

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ import (
88

99
func TestNew(t *testing.T) {
1010
tests := []handlerTest{
11+
{
12+
// this is a bit of a hack to test that the default handler is a noop handler
13+
// for the very first test, the test loop will not configure the default handler,
14+
// so it should be a noop handler
15+
name: "default noop",
16+
handlerFn: func(_ *bytes.Buffer) slog.Handler {
17+
return Default()
18+
},
19+
want: "",
20+
},
1121
{
1222
name: "New",
1323
handlerFn: func(_ *bytes.Buffer) slog.Handler {
@@ -20,15 +30,24 @@ func TestNew(t *testing.T) {
2030
handlerFn: func(_ *bytes.Buffer) slog.Handler {
2131
h := Default()
2232
// need something to trigger rebuilding the handlers
23-
// so the captured stdout is correct
33+
// the test is will have replaced os.Stdout, but the default handler
34+
// is still writing to the old os.Stdout
2435
h.SetOut(nil)
2536
return h
2637
},
2738
want: "level=INFO msg=hi\n",
2839
},
2940
}
30-
for _, tt := range tests {
31-
tt.stdout = true
32-
tt.Run(t)
41+
for i, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
if i != 0 {
44+
ogOpts := Default().HandlerOptions()
45+
defer Default().SetHandlerOptions(ogOpts)
46+
47+
Default().SetHandlerOptions(nil)
48+
}
49+
tt.stdout = true
50+
tt.Run(t)
51+
})
3352
}
3453
}

0 commit comments

Comments
 (0)