-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
147 lines (119 loc) · 3.16 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
package servicefoundation
import (
"fmt"
"os"
"strings"
"github.com/Travix-International/logger"
)
const (
minDebugLevel = 1
minInfoLevel = 2
minWarnLevel = 3
defaultLevel = 3 // Warning
)
type (
// Logger is a wrapper around the Logger package and extending it with log level filtering and simplified formatting.
Logger interface {
Debug(event, formatOrMsg string, a ...interface{}) error
Info(event, formatOrMsg string, a ...interface{}) error
Warn(event, formatOrMsg string, a ...interface{}) error
Error(event, formatOrMsg string, a ...interface{}) error
GetLogger() *logger.Logger
}
loggerImpl struct {
logMinLevel int
logger *logger.Logger
}
// LogFactory can be used to instantiate a new logger
LogFactory interface {
NewLogger(meta map[string]string) Logger
}
logFactoryImpl struct {
baseMeta map[string]string
logFilter string
logLevel int
}
)
var levels = []string{"debug", "info", "warning", "error"}
// NewLogFactory instantiates a new LogFactory implementation.
func NewLogFactory(logFilter string, baseMeta map[string]string) LogFactory {
logLevel := 0
levelFound := false
lcLogFilter := strings.ToLower(logFilter)
for i := 0; i < len(levels); i++ {
logLevel = i + 1
if lcLogFilter == levels[i] {
levelFound = true
break
}
}
if !levelFound {
logLevel = defaultLevel
}
return &logFactoryImpl{
baseMeta: baseMeta,
logFilter: logFilter,
logLevel: logLevel,
}
}
/* LogFactory implementation */
// NewLogger instantiates a new Logger implementation.
func (f *logFactoryImpl) NewLogger(meta map[string]string) Logger {
logMeta := combineMetas(meta, f.baseMeta)
log, _ := logger.New(logMeta)
formatter := NewLogFormatter()
consoleTransport := logger.NewTransport(os.Stdout, formatter)
log.AddTransport(consoleTransport)
return &loggerImpl{
logger: log,
logMinLevel: f.logLevel,
}
}
func combineMetas(meta1, meta2 map[string]string) map[string]string {
meta := make(map[string]string)
for key, value := range meta1 {
meta[key] = value
}
for key, value := range meta2 {
meta[key] = value
}
return meta
}
/* Logger implementation */
func (l *loggerImpl) Debug(event, formatOrMsg string, a ...interface{}) error {
if l.logMinLevel > minDebugLevel {
return nil
}
if len(a) == 0 {
return l.logger.Debug(event, formatOrMsg)
}
return l.logger.Debug(event, fmt.Sprintf(formatOrMsg, a...))
}
func (l *loggerImpl) Info(event, formatOrMsg string, a ...interface{}) error {
if l.logMinLevel > minInfoLevel {
return nil
}
if len(a) == 0 {
return l.logger.Info(event, formatOrMsg)
}
msg := fmt.Sprintf(formatOrMsg, a...)
return l.logger.Info(event, msg)
}
func (l *loggerImpl) Warn(event, formatOrMsg string, a ...interface{}) error {
if l.logMinLevel > minWarnLevel {
return nil
}
if len(a) == 0 {
return l.logger.Warn(event, formatOrMsg)
}
return l.logger.Warn(event, fmt.Sprintf(formatOrMsg, a...))
}
func (l *loggerImpl) Error(event, formatOrMsg string, a ...interface{}) error {
if len(a) == 0 {
return l.logger.Error(event, formatOrMsg)
}
return l.logger.Error(event, fmt.Sprintf(formatOrMsg, a...))
}
func (l *loggerImpl) GetLogger() *logger.Logger {
return l.logger
}