/
logging.go
78 lines (68 loc) · 1.78 KB
/
logging.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package logging
import (
"fmt"
"log/slog"
"os"
"runtime/debug"
"strconv"
"time"
"github.com/dsbferris/new-traefik-forward-auth/internal/types"
"github.com/lmittmann/tint"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
)
func NewLogger(format types.LogFormat, level types.LogLevel) (*slog.Logger, error) {
var log *slog.Logger
switch format {
case types.FORMAT_PRETTY:
log = getPrettyLogger(level)
case types.FORMAT_TEXT:
log = getTextLogger(level)
case types.FORMAT_JSON:
log = getJsonLogger(level)
default:
return nil, fmt.Errorf("unkown log format: %d", format)
}
log.Debug("PID: " + strconv.Itoa(os.Getpid()))
buildInfo, ok := debug.ReadBuildInfo()
if !ok {
log.Debug("call to debug.ReadBuildInfo() failed")
} else {
log.Debug("go version: " + buildInfo.GoVersion)
}
cwd, err := os.Getwd()
if err != nil {
log.Debug("call to os.Getwd() failed")
} else {
log.Debug("current working directoy: " + cwd)
}
return log, nil
}
func getPrettyLogger(level types.LogLevel) *slog.Logger {
w := os.Stderr
opts := &tint.Options{
TimeFormat: time.RFC3339,
Level: slog.Level(level),
//isatty checks if current terminal supports colors
NoColor: !isatty.IsTerminal(w.Fd()),
AddSource: true,
}
// tint allows for nice colorized output
// colorable adds support for windows
return slog.New(tint.NewHandler(colorable.NewColorable(w), opts))
}
func getTextLogger(level types.LogLevel) *slog.Logger {
w := os.Stderr
opts := &tint.Options{
TimeFormat: time.RFC3339,
Level: slog.Level(level),
NoColor: true,
AddSource: true,
}
return slog.New(tint.NewHandler(w, opts))
}
func getJsonLogger(level types.LogLevel) *slog.Logger {
return slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.Level(level),
}))
}