Skip to content

Commit 0fc6846

Browse files
committed
improve logger
Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 41dd130 commit 0fc6846

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

log/slogger/logger.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import (
44
"fmt"
55
"io"
66
"log/slog"
7-
"os"
87
"strings"
98

109
"github.com/lmittmann/tint"
11-
isatty "github.com/mattn/go-isatty"
1210
)
1311

1412
type LoggerOptionFunc func(*Options)
@@ -60,7 +58,7 @@ func WithSourceMode(mode SourceMode) LoggerOptionFunc {
6058
return func(opt *Options) {
6159
opt.SourceMode = mode
6260
if mode != SourceModeNone {
63-
opt.HandlerOptions.AddSource = true
61+
opt.AddSource = true
6462
}
6563
}
6664
}
@@ -80,6 +78,12 @@ func WithFormat(mode FormatMode) LoggerOptionFunc {
8078
}
8179
}
8280

81+
func WithColor(mode ColorMode) LoggerOptionFunc {
82+
return func(opt *Options) {
83+
opt.SetColorMode(mode)
84+
}
85+
}
86+
8387
func WithTime(v bool) LoggerOptionFunc {
8488
return func(opt *Options) {
8589
opt.ShowTime = v
@@ -101,7 +105,7 @@ func NewCliLogger(w io.Writer, opts ...LoggerOptionFunc) *Logger {
101105
SetSourceMode(SourceModeNone).
102106
SetFormat(FormatModeLogfmt).
103107
SetShowTime(false).
104-
SetColor(isatty.IsTerminal(os.Stdout.Fd()))
108+
SetColorMode(ColorModeAuto)
105109

106110
return newLoggerHandler(w, loggerOptions, opts...)
107111
}
@@ -121,6 +125,7 @@ func newLoggerHandler(w io.Writer, handlerOpts *Options, opts ...LoggerOptionFun
121125
ReplaceAttr: handlerOpts.ReplaceAttr,
122126
Level: handlerOpts.Level,
123127
AddSource: handlerOpts.SourceMode != SourceModeNone,
128+
NoColor: false,
124129
}
125130

126131
if !handlerOpts.ShowTime {

log/slogger/options.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ package slogger
33
import (
44
"fmt"
55
"log/slog"
6+
"os"
67
"strings"
8+
9+
"github.com/lmittmann/tint"
10+
isatty "github.com/mattn/go-isatty"
711
)
812

913
type (
@@ -47,8 +51,15 @@ func (o *Options) SetShowTime(mode bool) *Options {
4751
return o
4852
}
4953

50-
func (o *Options) SetColor(mode bool) *Options {
51-
o.Color = mode
54+
func (o *Options) SetColorMode(mode ColorMode) *Options {
55+
o.Color = false
56+
57+
switch strings.ToLower(string(mode)) {
58+
case "", "auto":
59+
o.Color = isatty.IsTerminal(os.Stderr.Fd())
60+
case "1", "y", "yes", "true", "enable", "enabled", "on":
61+
o.Color = true
62+
}
5263
return o
5364
}
5465

@@ -60,6 +71,10 @@ func NewReplaceAttr(opts *Options, callback func(groups []string, a slog.Attr) s
6071
level := a.Value.Any().(slog.Level)
6172
if l, exists := levelNames[level]; exists {
6273
a.Value = slog.StringValue(l.text)
74+
75+
if opts.Color && l.color > 0 {
76+
a = tint.Attr(l.color, a)
77+
}
6378
} else {
6479
a.Value = slog.StringValue(level.String())
6580
}
@@ -85,6 +100,13 @@ func NewReplaceAttr(opts *Options, callback func(groups []string, a slog.Attr) s
85100
}
86101
}
87102

103+
if opts.Color {
104+
// if val is error, color it
105+
if _, isErr := a.Value.Any().(error); isErr {
106+
a = tint.Attr(ColorError, a)
107+
}
108+
}
109+
88110
if callback != nil {
89111
a = callback(groups, a)
90112
}

log/slogger/slogger.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
type (
1212
FormatMode string
1313
SourceMode string
14+
ColorMode string
1415
)
1516

1617
const (
@@ -22,13 +23,22 @@ const (
2223
SourceModeFile SourceMode = "file"
2324
SourceModeFull SourceMode = "full"
2425

26+
ColorModeDisabled ColorMode = "disabled"
27+
ColorModeEnabled ColorMode = "enabled"
28+
ColorModeAuto ColorMode = "auto"
29+
2530
LevelTrace = slog.Level(-8)
2631
LevelDebug = slog.LevelDebug
2732
LevelInfo = slog.LevelInfo
2833
LevelWarn = slog.LevelWarn
2934
LevelError = slog.LevelError
3035
LevelFatal = slog.Level(12)
3136
LevelPanic = slog.Level(255)
37+
38+
ColorError = 1
39+
ColorWarning = 3
40+
ColorInfo = 2
41+
ColorDebug = 5
3242
)
3343

3444
type (
@@ -37,19 +47,20 @@ type (
3747
}
3848

3949
logLevel struct {
40-
text string
50+
text string
51+
color uint8
4152
}
4253
)
4354

4455
var (
4556
levelNames = map[slog.Leveler]logLevel{
46-
LevelTrace: {text: "TRACE"},
47-
LevelDebug: {text: "DEBUG"},
48-
LevelInfo: {text: "INFO"},
49-
LevelWarn: {text: "WARN"},
50-
LevelError: {text: "ERROR"},
51-
LevelFatal: {text: "FATAL"},
52-
LevelPanic: {text: "PANIC"},
57+
LevelTrace: {text: "TRACE", color: ColorDebug},
58+
LevelDebug: {text: "DEBUG", color: ColorDebug},
59+
LevelInfo: {text: "INFO", color: ColorInfo},
60+
LevelWarn: {text: "WARN", color: ColorWarning},
61+
LevelError: {text: "ERROR", color: ColorError},
62+
LevelFatal: {text: "FATAL", color: ColorError},
63+
LevelPanic: {text: "PANIC", color: ColorError},
5364
}
5465
)
5566

0 commit comments

Comments
 (0)