-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
122 lines (95 loc) · 2.56 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
package go_logger
import (
"errors"
"os"
"sync"
"github.com/uniplaces/go-logger/internal"
)
const logType = "app"
var instance Logger
var once sync.Once
var defaultFields []defaultField
type defaultField struct {
key string
value interface{}
isContextField bool
}
// Init initializes logger instance
func Init(config Config) error {
if instance != nil {
return errors.New("logger cannot be initialized more than once")
}
addMandatoryDefaultFields()
once.Do(func() {
instance = internal.NewLogrusLogger(
config.level,
config.environment,
os.Stdout,
Builder().getDefaultFields(defaultFields),
)
})
return nil
}
// InitWithInstance sets logger to an instance (for testing purposes)
func InitWithInstance(newInstance Logger) error {
if instance != nil {
return errors.New("logger cannot be initialized more than once")
}
instance = newInstance
addMandatoryDefaultFields()
return nil
}
// AddDefaultField adds data to be set as a field (either normal or context) on all logs
func AddDefaultField(key string, value interface{}, isContextField bool) {
defaultFields = append(defaultFields, defaultField{key: key, value: value, isContextField: isContextField})
}
// Error logs a error message
func Error(err error) {
Builder().Error(err)
}
// Error logs a error message with fields
func (builder builder) Error(err error) {
if instance == nil {
return
}
instance.ErrorWithFields(err, builder.getDefaultFields(defaultFields))
}
// Warning logs a warning message
func Warning(message string) {
Builder().Warning(message)
}
// Warning logs a warning message with fields
func (builder builder) Warning(message string) {
if instance == nil {
return
}
instance.WarningWithFields(message, builder.getDefaultFields(defaultFields))
}
// Info logs a info message
func Info(message string) {
Builder().Info(message)
}
// Info logs a info message with fields
func (builder builder) Info(message string) {
if instance == nil {
return
}
instance.InfoWithFields(message, builder.getDefaultFields(defaultFields))
}
// Debug logs a debug message
func Debug(message string) {
Builder().Debug(message)
}
// Debug logs a debug message with fields
func (builder builder) Debug(message string) {
if instance == nil {
return
}
instance.DebugWithFields(message, builder.getDefaultFields(defaultFields))
}
func addMandatoryDefaultFields() {
AddDefaultField("type", logType, false)
AddDefaultField("app-id", os.Getenv("APPID"), false)
AddDefaultField("env", os.Getenv("GOENV"), false)
AddDefaultField("git-hash", os.Getenv("GITHASH"), false)
}