-
Notifications
You must be signed in to change notification settings - Fork 1
/
slog.go
151 lines (122 loc) · 4.07 KB
/
slog.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package slog
import (
"fmt"
"github.com/Sellsuki/sellsuki-go-logger/v2/config"
"github.com/Sellsuki/sellsuki-go-logger/v2/level"
"github.com/Sellsuki/sellsuki-go-logger/v2/log"
"github.com/Sellsuki/sellsuki-go-logger/v2/zap_logger"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"sync"
"time"
)
// SukiLogger is a basically a Singleton wrapper for slog/log/zap_logger
type SukiLogger struct {
config config.Config
zapInstance *zap.Logger
}
var sukiLoggerOnce sync.Once
var sukiLogger *SukiLogger
// Init initialize the logger
// Do not run this function in parallel
func Init(c ...config.Config) {
sukiLoggerOnce.Do(func() {
cfg := config.Config{
LogLevel: level.Info,
AppName: "unknown",
Version: "v0.0.0",
MaxBodySize: 1048576,
}
if len(c) > 0 {
cfg = c[0]
}
zCfg := zap.Config{
Level: zap.NewAtomicLevelAt(level.ToZap(cfg.LogLevel)),
Development: false,
Sampling: &zap.SamplingConfig{
Initial: 100,
Thereafter: 100,
},
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
TimeKey: "timestamp",
LevelKey: "level",
NameKey: "logger",
CallerKey: "caller",
FunctionKey: zapcore.OmitKey,
MessageKey: "message",
StacktraceKey: "stacktrace",
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: zapcore.LowercaseLevelEncoder,
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeDuration: zapcore.SecondsDurationEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stdout"},
}
if cfg.Readable {
zCfg.Encoding = "console"
zCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
}
if cfg.HardCodedTime != "" {
zCfg.EncoderConfig.EncodeTime = func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(cfg.HardCodedTime)
}
}
logger, err := zCfg.Build(zap.AddCallerSkip(1))
if err != nil {
panic(fmt.Errorf("failed to init logger: %w", err))
}
defer logger.Sync()
sukiLogger = &SukiLogger{zapInstance: logger, config: cfg}
})
}
func Debug(msg string) log.Log {
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Debug, zap_logger.TypeApplication, msg)
}
func Info(msg string) log.Log {
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Info, zap_logger.TypeApplication, msg)
}
func Warn(msg string) log.Log {
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Warn, zap_logger.TypeApplication, msg)
}
func Error(msg string) log.Log {
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Error, zap_logger.TypeApplication, msg)
}
func Panic(msg string) log.Log {
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Panic, zap_logger.TypeApplication, msg)
}
func Fatal(msg string) log.Log {
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Fatal, zap_logger.TypeApplication, msg)
}
func Event(msg string, payload log.EventPayload) log.Log {
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Info, zap_logger.TypeEvent, msg).
WithField("event", payload)
}
func Audit(msg string, payload log.AuditPayload) log.Log {
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Info, zap_logger.TypeAudit, msg).
WithField("audit", payload)
}
func Kafka(msg string, kMsg *log.KafkaMessagePayload, kRes *log.KafkaResultPayload) log.Log {
payload := map[string]any{}
if kMsg != nil {
payload["kafka_message"] = kMsg
}
if kRes != nil {
payload["kafka_result"] = kRes
}
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Info, zap_logger.TypeHandlerKafka, msg).
WithFields(payload)
}
func HTTP(msg string, req *log.HTTPRequestPayload, res *log.HTTPResponsePayload) log.Log {
payload := map[string]any{}
if req != nil {
payload["http_request"] = req
}
if res != nil {
payload["http_response"] = res
}
return zap_logger.New(sukiLogger.zapInstance, sukiLogger.config, level.Info, zap_logger.TypeHandlerHTTP, msg).
WithFields(payload)
}