forked from sreeram-narayanan/gosip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
85 lines (63 loc) · 1.63 KB
/
log.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
package log
import (
"fmt"
"strings"
)
// Logger interface used as base logger throughout the library.
type Logger interface {
Print(args ...interface{})
Printf(format string, args ...interface{})
Trace(args ...interface{})
Tracef(format string, args ...interface{})
Debug(args ...interface{})
Debugf(format string, args ...interface{})
Info(args ...interface{})
Infof(format string, args ...interface{})
Warn(args ...interface{})
Warnf(format string, args ...interface{})
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Panic(args ...interface{})
Panicf(format string, args ...interface{})
WithPrefix(prefix string) Logger
Prefix() string
WithFields(fields Fields) Logger
Fields() Fields
SetLevel(level Level)
}
type Loggable interface {
Log() Logger
}
type Fields map[string]interface{}
func (fields Fields) String() string {
str := make([]string, 0)
for k, v := range fields {
str = append(str, fmt.Sprintf("%s=%+v", k, v))
}
return strings.Join(str, " ")
}
func (fields Fields) WithFields(newFields Fields) Fields {
allFields := make(Fields)
for k, v := range fields {
allFields[k] = v
}
for k, v := range newFields {
allFields[k] = v
}
return allFields
}
func AddFieldsFrom(logger Logger, values ...interface{}) Logger {
for _, value := range values {
switch v := value.(type) {
case Logger:
logger = logger.WithFields(v.Fields())
case Loggable:
logger = logger.WithFields(v.Log().Fields())
case interface{ Fields() Fields }:
logger = logger.WithFields(v.Fields())
}
}
return logger
}