This repository has been archived by the owner on Jan 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogrusen.go
176 lines (144 loc) · 4.13 KB
/
logrusen.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package logrusen
import (
"fmt"
"os"
"runtime"
"github.com/evalphobia/logrus_sentry"
"github.com/rifflock/lfshook"
log "github.com/sirupsen/logrus"
)
type StandardLogger interface {
Debug(message string, fields Fields)
Info(message string, fields Fields)
Warn(message string, err error, fields Fields)
Error(message string, err error, fields Fields)
Fatal(message string, err error, fields Fields)
Panic(message string, err error, fields Fields)
Setup() error
SetupWithSentry(dsn string) error
}
type standardLogger struct {
*log.Logger
}
type Fields log.Fields
func New() StandardLogger {
baseLogger := log.New()
standardLogger := &standardLogger{baseLogger}
return standardLogger
}
func (l *standardLogger) Setup() error {
setDefault()
return nil
}
func (l *standardLogger) SetupWithSentry(dsn string) error {
if dsn != "" {
err := setDefaultWithSentry(dsn)
if err != nil {
return err
}
} else {
setDefault()
}
return nil
}
func setDefault() {
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
ForceColors: true,
})
}
func setDefaultWithSentry(dsn string) error {
log.SetOutput(os.Stdout)
log.SetLevel(log.DebugLevel)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
ForceColors: true,
})
log.AddHook(lfshook.NewHook(
lfshook.PathMap{
log.InfoLevel: "logs.log",
log.ErrorLevel: "logs.log",
log.DebugLevel: "logs.log",
log.FatalLevel: "logs.log",
log.PanicLevel: "logs.log",
log.TraceLevel: "logs.log",
log.WarnLevel: "logs.log",
},
&log.JSONFormatter{},
))
sentryHook, err := logrus_sentry.NewSentryHook(dsn, []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
})
if err != nil {
return err
}
log.AddHook(sentryHook)
return nil
}
// Const Log Variables
func constFields(fields Fields) Fields {
if fields == nil {
fields = Fields{}
}
pc, _, line, _ := runtime.Caller(2)
fields["caller"] = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), line)
return fields
}
// Error Log Variables
func errorFields(fields Fields) Fields {
if fields == nil {
fields = Fields{}
}
pc, _, line, _ := runtime.Caller(2)
fields["caller"] = fmt.Sprintf("%s:%d", runtime.FuncForPC(pc).Name(), line)
return fields
}
// DEBUG:
// message* user friendly error messega
// fields* can be nil or can be env and system status variables
func (l *standardLogger) Debug(message string, fields Fields) {
fields = errorFields(fields)
log.WithFields(log.Fields(fields)).Debug(message)
}
// INFO:
// message* user friendly error messega
// fields* can be nil or can be env and system status variables
func (l *standardLogger) Info(message string, fields Fields) {
fields = constFields(fields)
log.WithFields(log.Fields(fields)).Info(message)
}
// Warn:
// message* user friendly error message
// err (error): An error obtained from a failed call to a previous method or function
// fields* can be nil or can be env and system status variables
func (l *standardLogger) Warn(message string, err error, fields Fields) {
fields = errorFields(fields)
fields["error"] = err
log.WithFields(log.Fields(fields)).Warn(message)
}
// Error writes a message to the log of Error level status.
// message* user friendly error message
// err (error): An error obtained from a failed call to a previous method or function
// fields* can be nil or can be env and system status variables
func (l *standardLogger) Error(message string, err error, fields Fields) {
fields = errorFields(fields)
fields["error"] = err
log.WithFields(log.Fields(fields)).Error(message)
}
// Fatal writes a message to the log of Fatal level status.
// Note: Calling a Fatal() error will exit execution of the current program. Goroutines will not
// execute on deferral. Only call Fatal() if you are sure that the program should exit as well.
func (l *standardLogger) Fatal(message string, err error, fields Fields) {
fields = errorFields(fields)
fields["error"] = err
log.WithFields(log.Fields(fields)).Fatal(message)
}
// DONT PANIC
// so, i use fatal function
func (l *standardLogger) Panic(message string, err error, fields Fields) {
l.Fatal(message, err, fields)
}