-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
161 lines (134 loc) · 2.99 KB
/
logger.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
152
153
154
155
156
157
158
159
160
161
package gosdk
import (
"github.com/2110336-2565-2/cu-freelance-library/pkg/log"
"github.com/2110336-2565-2/cu-freelance-library/pkg/sentry"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"time"
)
var sentryService sentry.Sentry
var logLevel zapcore.Level
type Logger interface {
SetName(name string)
Fatal(err error) Logger
Error(err error) Logger
Warn() Logger
Info() Logger
Debug() Logger
Keyword(key string, val any) Logger
Msg(msg string)
}
type logger struct {
keyword map[string]any
level log.Level
serviceName string
err error
*zap.Logger
}
func SetLogLevel(level string) error {
l, err := zapcore.ParseLevel(level)
if err != nil {
return err
}
logLevel = l
return nil
}
func NewLogger(serviceName string) Logger {
config := zap.NewProductionConfig()
config.Level = zap.NewAtomicLevelAt(logLevel)
config.EncoderConfig.EncodeTime = zapcore.TimeEncoderOfLayout(time.RFC3339Nano)
config.EncoderConfig.TimeKey = "time"
lg, _ := config.Build()
keyword := map[string]any{}
return &logger{
keyword,
log.Unknown,
serviceName,
nil,
lg,
}
}
func SetSentryDSN(newDSN string) error {
sentrySrv, err := sentry.NewService(newDSN)
if err != nil {
return err
}
sentryService = sentrySrv
return nil
}
func (s *logger) SetName(name string) {
s.serviceName = name
}
func (s *logger) Info() Logger {
s.level = log.Info
return s
}
func (s *logger) Error(err error) Logger {
s.level = log.Error
s.err = err
return s
}
func (s *logger) Warn() Logger {
s.level = log.Warn
return s
}
func (s *logger) Debug() Logger {
s.level = log.Debug
return s
}
func (s *logger) Fatal(err error) Logger {
s.level = log.Fatal
s.err = err
return s
}
func (s *logger) Keyword(key string, val any) Logger {
s.keyword[key] = val
return s
}
func (s *logger) Msg(msg string) {
defer s.Logger.Sync()
var keywordList []zap.Field
s.createKeywordList(&keywordList)
switch s.level {
case log.Debug:
s.Logger.Debug(msg, keywordList...)
case log.Info:
s.Logger.Info(msg, keywordList...)
case log.Warn:
s.Logger.Warn(msg, keywordList...)
case log.Error:
if sentryService != nil {
sentryService.CaptureException(s.err)
}
s.Logger.Error(msg, keywordList...)
case log.Fatal:
if sentryService != nil {
sentryService.CaptureException(s.err)
}
s.Logger.Fatal(msg, keywordList...)
default:
return
}
s.keyword = map[string]any{}
s.err = nil
}
func (s *logger) createKeywordList(keywords *[]zap.Field) {
*keywords = append(*keywords, zap.String("service", s.serviceName))
if s.err != nil {
*keywords = append(*keywords, zap.Error(s.err))
}
for k, v := range s.keyword {
switch v.(type) {
case string:
*keywords = append(*keywords, zap.String(k, v.(string)))
case int:
*keywords = append(*keywords, zap.Int(k, v.(int)))
case time.Duration:
*keywords = append(*keywords, zap.Duration(k, v.(time.Duration)))
case bool:
*keywords = append(*keywords, zap.Bool(k, v.(bool)))
default:
*keywords = append(*keywords, zap.Any(k, v.(any)))
}
}
}