Skip to content

Commit e57694f

Browse files
committed
Fix Level constant names and Level option
1 parent ca4adce commit e57694f

File tree

3 files changed

+31
-27
lines changed

3 files changed

+31
-27
lines changed

internal/humanfmt/humanfmt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ const timestampMilli = "Jan 02 15:04:05.000"
7979

8080
func levelColor(level slog.Level) color.Attribute {
8181
switch level {
82-
case slog.Debug, slog.Info:
82+
case slog.LevelDebug, slog.LevelInfo:
8383
return color.FgBlue
84-
case slog.Warn:
84+
case slog.LevelWarn:
8585
return color.FgYellow
86-
case slog.LevelError, slog.Critical, slog.Fatal:
86+
case slog.LevelError, slog.LevelCritical, slog.LevelFatal:
8787
return color.FgRed
8888
}
8989
panic("humanfmt: unexpected level: " + string(level))

slog.go

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,21 @@ type Entry struct {
116116
type Level int
117117

118118
const (
119-
Debug Level = iota
120-
Info
121-
Warn
119+
LevelDebug Level = iota
120+
LevelInfo
121+
LevelWarn
122122
LevelError
123-
Critical
124-
Fatal
123+
LevelCritical
124+
LevelFatal
125125
)
126126

127127
var levelStrings = map[Level]string{
128-
Debug: "DEBUG",
129-
Info: "INFO",
130-
Warn: "WARN",
131-
LevelError: "ERROR",
132-
Critical: "CRITICAL",
133-
Fatal: "FATAL",
128+
LevelDebug: "DEBUG",
129+
LevelInfo: "INFO",
130+
LevelWarn: "WARN",
131+
LevelError: "ERROR",
132+
LevelCritical: "CRITICAL",
133+
LevelFatal: "FATAL",
134134
}
135135

136136
func (l Level) String() string {
@@ -149,14 +149,18 @@ type Sink interface {
149149
// Make creates a logger that writes logs to sink.
150150
func Make(s Sink, opts *Options) Logger {
151151
if opts == nil {
152-
opts = &Options{}
152+
opts = &Options{
153+
Level: func() Level {
154+
return LevelDebug
155+
},
156+
}
153157
}
154158

155159
l := Logger{
156160
sinks: []sink{
157161
{
158162
sink: s,
159-
level: opts.Level,
163+
level: opts.Level(),
160164
},
161165
},
162166
testingHelper: func() {},
@@ -184,17 +188,17 @@ type Logger struct {
184188

185189
func (l Logger) Debug(ctx context.Context, msg string, fields ...Field) {
186190
l.testingHelper()
187-
l.log(ctx, Debug, msg, fields)
191+
l.log(ctx, LevelDebug, msg, fields)
188192
}
189193

190194
func (l Logger) Info(ctx context.Context, msg string, fields ...Field) {
191195
l.testingHelper()
192-
l.log(ctx, Info, msg, fields)
196+
l.log(ctx, LevelInfo, msg, fields)
193197
}
194198

195199
func (l Logger) Warn(ctx context.Context, msg string, fields ...Field) {
196200
l.testingHelper()
197-
l.log(ctx, Warn, msg, fields)
201+
l.log(ctx, LevelWarn, msg, fields)
198202
}
199203

200204
func (l Logger) Error(ctx context.Context, msg string, fields ...Field) {
@@ -204,12 +208,12 @@ func (l Logger) Error(ctx context.Context, msg string, fields ...Field) {
204208

205209
func (l Logger) Critical(ctx context.Context, msg string, fields ...Field) {
206210
l.testingHelper()
207-
l.log(ctx, Critical, msg, fields)
211+
l.log(ctx, LevelCritical, msg, fields)
208212
}
209213

210214
func (l Logger) Fatal(ctx context.Context, msg string, fields ...Field) {
211215
l.testingHelper()
212-
l.log(ctx, Fatal, msg, fields)
216+
l.log(ctx, LevelFatal, msg, fields)
213217
}
214218

215219
func (l Logger) With(fields ...Field) Logger {
@@ -240,15 +244,15 @@ func (l Logger) log(ctx context.Context, level Level, msg string, fields []Field
240244
s.sink.LogEntry(ctx, ent)
241245
}
242246

243-
if level == Fatal {
247+
if level == LevelFatal {
244248
os.Exit(1)
245249
}
246250
}
247251

248252
// The base options for every Logger.
249253
type Options struct {
250-
// Level to log at, defaults to LevelDebug.
251-
Level Level
254+
// Level returns to log at, defaults to LevelDebug.
255+
Level func() Level
252256
}
253257

254258
type parsedFields struct {

sloggers/slogtest/test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ func (ts testSink) LogEntry(ctx context.Context, ent slog.Entry) {
5858
s := humanfmt.Entry(ent, stderrColor)
5959

6060
switch ent.Level {
61-
case slog.Debug, slog.Info, slog.Warn:
61+
case slog.LevelDebug, slog.LevelInfo, slog.LevelWarn:
6262
ts.tb.Log(s)
63-
case slog.LevelError, slog.Critical:
63+
case slog.LevelError, slog.LevelCritical:
6464
if ts.opts.IgnoreErrors {
6565
ts.tb.Log(s)
6666
} else {
6767
ts.tb.Error(s)
6868
}
69-
case slog.Fatal:
69+
case slog.LevelFatal:
7070
if ts.opts.IgnoreErrors {
7171
panic("slogtest: cannot fatal in tests when IgnoreErrors option is set")
7272
}

0 commit comments

Comments
 (0)