-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
181 lines (162 loc) · 5.82 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package cloudlog
import (
"go.uber.org/zap"
"golang.org/x/xerrors"
)
// Logger provides structured logging in Go / Zap.
type Logger struct {
zapLogger *zap.Logger
sugar *zap.SugaredLogger
errorZapLogger *zap.Logger
errorSugar *zap.SugaredLogger
}
// NewCloudLogger is logger constructor in Cloud Environment.
// It has optional arguments about logger options written on options.go.
// It has been set Error Reporting and Cloud Logging in GCP by default.
func NewCloudLogger(options ...Option) (logger *Logger, err error) {
needErrorReporting, serviceName, logLevel := getOptions(options)
config := NewCloudZapConfig(logLevel, "json")
option := GetCloudServiceContextOption(serviceName)
zapLogger, err := config.Build(option)
if err != nil {
return nil, xerrors.Errorf("init logging cloud configs error: %w", err)
}
var errorZapLogger *zap.Logger
if needErrorReporting {
errorZapLogger = AddCloudErrorReportingOption(zapLogger)
} else {
errorZapLogger = zapLogger
}
logger = &Logger{
zapLogger: zapLogger,
sugar: zapLogger.Sugar(),
errorZapLogger: errorZapLogger,
errorSugar: errorZapLogger.Sugar(),
}
return
}
// NewLocalLogger is logger constructor in Local Environment.
// It has optional arguments about logger options written on options.go.
func NewLocalLogger(options ...Option) (logger *Logger, err error) {
_, _, logLevel := getOptions(options)
config := NewLocalZapConfig(logLevel, "console")
zapLogger, err := config.Build()
if err != nil {
return nil, xerrors.Errorf("init logging local configs error: %w", err)
}
errorZapLogger := zapLogger
logger = &Logger{
zapLogger: zapLogger,
sugar: zapLogger.Sugar(),
errorZapLogger: errorZapLogger,
errorSugar: errorZapLogger.Sugar(),
}
return
}
// Debugf uses fmt.Sprintf to log a templated message.
func (l *Logger) Debugf(template string, args ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.sugar.Debugf(template, args)
}
// Infof uses fmt.Sprintf to log a templated message.
func (l *Logger) Infof(template string, args ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.sugar.Infof(template, args)
}
// Warnf uses fmt.Sprintf to log a templated message.
// e.g. Warnf("something went wrong: %+v", err)
// If you don't need these wrapper methods, you can override them by parent interface defined on your own.
func (l *Logger) Warnf(template string, args ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.sugar.Warnf(template, args)
}
// Errorf uses fmt.Sprintf to log a templated message.
// e.g. Errorf("something went wrong: %+v", err)
// If you don't need these wrapper methods, you can override them by parent interface defined on your own.
func (l *Logger) Errorf(template string, args ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.errorSugar.Errorf(template, args)
}
// Fatalf uses fmt.Sprintf to log a templated message.
func (l *Logger) Fatalf(template string, args ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.errorSugar.Fatalf(template, args)
}
// Debugw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *Logger) Debugw(msg string, keysAndValues ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.sugar.Debugw(msg, keysAndValues)
}
// Infow logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *Logger) Infow(msg string, keysAndValues ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.sugar.Infow(msg, keysAndValues)
}
// Warnw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
// e.g. WarnW("something went wrong", "key", "value", "sum", 10)
// If you don't need these wrapper methods, you can override them by parent interface defined on your own.
func (l *Logger) Warnw(msg string, keysAndValues ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.sugar.Warnw(msg, keysAndValues)
}
// Errorw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
// e.g. WarnW("something went wrong", "key", "value", "sum", 10)
// If you don't need these wrapper methods, you can override them by parent interface defined on your own.
func (l *Logger) Errorw(msg string, keysAndValues ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.errorSugar.Errorw(msg, keysAndValues)
}
// Fatalw logs a message with some additional context. The variadic key-value
// pairs are treated as they are in With.
func (l *Logger) Fatalw(msg string, keysAndValues ...interface{}) {
defer func() {
_ = l.sugar.Sync()
}()
l.errorSugar.Fatalw(msg, keysAndValues)
}
// Debug logs a message at DebugLevel.
func (l *Logger) Debug(msg string, field ...zap.Field) {
l.zapLogger.Debug(msg, field...)
}
// Info logs a message at InfoLevel.
func (l *Logger) Info(msg string, field ...zap.Field) {
l.zapLogger.Info(msg, field...)
}
// Warn logs a message at WarnLevel.
// If you don't need these wrapper methods, you can override them by parent interface defined on your own.
// You shoud use this method when you want faster logging, but if you don't want to use zap.Field and depend them, you can use Warnf or Warnw.
func (l *Logger) Warn(msg string, field ...zap.Field) {
l.zapLogger.Warn(msg, field...)
}
// Error logs a message at ErrorLevel.
// If you don't need these wrapper methods, you can override them by parent interface defined on your own.
// You shoud use this method when you want faster logging, but if you don't want to use zap.Field and depend them, you can use Warnf or Warnw.
func (l *Logger) Error(msg string, field ...zap.Field) {
l.errorZapLogger.Error(msg, field...)
}
// Fatal logs a message at FatalLevel.
func (l *Logger) Fatal(msg string, field ...zap.Field) {
l.errorZapLogger.Fatal(msg, field...)
}