-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathlogging.go
More file actions
47 lines (40 loc) · 816 Bytes
/
logging.go
File metadata and controls
47 lines (40 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package kilonova
import (
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"io"
"log/slog"
"os"
"time"
)
func logColors(w io.Writer) bool {
f, ok := w.(*os.File)
if !ok {
return false
}
if os.Getenv("NO_COLOR") != "" {
return false
}
if !isatty.IsTerminal(f.Fd()) {
return false
}
return os.Getenv("TERM") != "dumb"
}
func GetSlogHandler(debug bool, out io.Writer) slog.Handler {
level := slog.LevelInfo
if debug {
level = slog.LevelDebug
}
return tint.NewHandler(out, &tint.Options{
AddSource: true,
Level: level,
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
if _, ok := attr.Value.Any().(error); attr.Key == "err" || ok {
return tint.Attr(9, attr)
}
return attr
},
TimeFormat: time.RFC3339,
NoColor: !logColors(out),
})
}