about summary refs log tree commit diff stats
path: root/internal/log/log.go
blob: a0f5ba736ae10d85dd701a3050805754e433f69c (plain)
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
package log

import (
	"os"

	zaplogfmt "github.com/sykesm/zap-logfmt"
	prettyconsole "github.com/thessem/zap-prettyconsole"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var logger *zap.SugaredLogger

func DPanic(msg string, rest ...any) {
	logger.DPanicw(msg, rest...)
}
func Debug(msg string, rest ...any) {
	logger.Debugw(msg, rest...)
}
func Info(msg string, rest ...any) {
	logger.Infow(msg, rest...)
}
func Warn(msg string, rest ...any) {
	logger.Warnw(msg, rest...)
}
func Error(msg string, rest ...any) {
	logger.Errorw(msg, rest...)
}
func Panic(msg string, rest ...any) {
	logger.Panicw(msg, rest...)
}
func Fatal(msg string, rest ...any) {
	logger.Fatalw(msg, rest...)
}

func getLevelFromEnv() (zapcore.Level, error) {
	if str, found := os.LookupEnv("LOG_LEVEL"); found {
		l, err := zap.ParseAtomicLevel(str)

		return l.Level(), err
	}

	return zap.InfoLevel, nil
}

func Configure(isProduction bool) {
	var l *zap.Logger
	level, err := getLevelFromEnv()
	if err != nil {
		panic(err)
	}
	if isProduction {
		cfg := zap.NewProductionEncoderConfig()
		cfg.TimeKey = ""
		l = zap.New(zapcore.NewCore(zaplogfmt.NewEncoder(cfg), os.Stderr, level))
	} else {
		cfg := prettyconsole.NewConfig()
		cfg.EncoderConfig.TimeKey = ""
		cfg.Level.SetLevel(level)
		l = zap.Must(cfg.Build())
	}
	logger = l.WithOptions(zap.AddCallerSkip(1)).Sugar()
}