-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
156 lines (130 loc) · 3.07 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
package logger
import (
"fmt"
"log"
"os"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
)
type LogLevel int
const (
LogLevelDebug LogLevel = iota
LogLevelInfo
LogLevelWarning
LogLevelError
)
type LoggerOptions struct {
ModuleName string
SentryEnabled bool
SentryDsn string
SentryEnv string
SentryLevel LogLevel
SentryTraces float64
}
var initialized bool
var options LoggerOptions
func init() {
initialized = false
}
func Init(loggerOptions LoggerOptions) error {
options = loggerOptions
if options.SentryEnabled {
err := sentry.Init(sentry.ClientOptions{
Dsn: options.SentryDsn,
Environment: options.SentryEnv,
Release: options.ModuleName,
TracesSampleRate: options.SentryTraces,
AttachStacktrace: true,
Debug: false,
})
if err != nil {
return err
}
}
initialized = true
return nil
}
func IsSentryEnabled() bool {
if !initialized {
log.Fatal(`Initialize logger before calling "logger.IsSentryEnabled()"`)
}
return options.SentryEnabled
}
func GetSentryGin() gin.HandlerFunc {
if !initialized || !options.SentryEnabled {
log.Fatal(`Initialize logger with Sentry enabled before calling "logger.GetSentryGin()"`)
}
return sentrygin.New(sentrygin.Options{
Repanic: true,
})
}
func Debugf(format string, v ...any) {
Debug(fmt.Sprintf(format, v...))
}
func Infof(format string, v ...any) {
Info(fmt.Sprintf(format, v...))
}
func Warningf(format string, v ...any) {
Warning(fmt.Sprintf(format, v...))
}
func Errorf(format string, v ...any) {
Error(fmt.Errorf(format, v...))
}
func Panicf(format string, v ...any) {
Error(fmt.Errorf(format, v...))
panic(fmt.Sprintf(format, v...))
}
func Fatalf(format string, v ...any) {
Error(fmt.Errorf(format, v...))
os.Exit(1)
}
func Panic(err error) {
Error(err)
panic(err.Error())
}
func Fatal(err error) {
Error(err)
os.Exit(1)
}
func Debug(message string) {
if !initialized {
log.Print(`Initialize logger before calling "logger.Debug()"`)
}
formatted := fmt.Sprintf("Debug: %v", message)
sendMessageToSentry(LogLevelDebug, formatted)
log.Printf(formatted)
}
func Info(message string) {
if !initialized {
log.Print(`Initialize logger before calling "logger.Info()"`)
}
formatted := fmt.Sprintf("Info: %v", message)
sendMessageToSentry(LogLevelInfo, formatted)
log.Printf(formatted)
}
func Warning(message string) {
if !initialized {
log.Print(`Initialize logger before calling "logger.Warning()"`)
}
formatted := fmt.Sprintf("Warning: %v", message)
sendMessageToSentry(LogLevelWarning, formatted)
log.Printf(formatted)
}
func Error(err error) {
if !initialized {
log.Print(`Initialize logger before calling "logger.Error()"`)
}
sendErrorToSentry(err)
log.Printf("Error: %v", err)
}
func sendMessageToSentry(level LogLevel, message string) {
if initialized && options.SentryEnabled && level >= options.SentryLevel {
sentry.CaptureMessage(message)
}
}
func sendErrorToSentry(err error) {
if initialized && options.SentryEnabled {
sentry.CaptureException(err)
}
}